function ssimg = showSeg(imgref,seg,dimfactor)

% Generates a dimmed image showing segment boundaries.
% If no output argument is specified, displays instead.
% The image will be approximately twice as large as the original.

if (length(imgref) == 1)
    img = imgData(imgref);
    if (nargin < 2)
        seg = midSegData(imgref);
        %seg = jsegData(imgref);
     elseif ischar(seg)
        seg = segData(imgref,seg);
    end;
else
    img = imgref;
end;
if (nargin < 3) dimfactor = 0.8; end;
%if strcmp(class(img),'uint8')
%    img = double(img)/255;
%end;

[nrow,ncol,nclr] = size(img);
ssimg = zeros(2*nrow-1,2*ncol-1,nclr);
if strcmp(class(img),'uint8')
   ssimg(1:2:end,1:2:end,:) = double(img)/255*dimfactor;
else
   ssimg(1:2:end,1:2:end,:) = img*dimfactor;
end;
ssimg(2:2:end,:,:) = (ssimg(1:2:end-2,:,:)+ssimg(3:2:end,:,:))/2;
ssimg(:,2:2:end,:) = (ssimg(:,1:2:end-2,:)+ssimg(:,3:2:end,:))/2;

% add boundaries
top = seg(1:end-1,:);
bot = seg(2:end,:);
lft = seg(:,1:end-1);
rgt = seg(:,2:end);
hedge = (top ~= bot);
vedge = (lft ~= rgt);
lhedge = hedge(:,1:end-1);
rhedge = hedge(:,2:end);
tvedge = vedge(1:end-1,:);
bvedge = vedge(2:end,:);
hfill = ssimg(2:2:end,1:2:end,:);
vfill = ssimg(1:2:end,2:2:end,:);
jfill = ssimg(2:2:end,2:2:end,:);
hfill(logical(repmat(double(hedge),[1,1,nclr]))) = 1;
vfill(logical(repmat(double(vedge),[1,1,nclr]))) = 1;
jfill(logical(repmat(double(lhedge+rhedge+tvedge+bvedge > 0),[1,1,nclr]))) = 1;
%hfill(logical(tile(double(hedge),ones(1,1,nclr)))) = 1;
%vfill(logical(tile(double(vedge),ones(1,1,nclr)))) = 1;
%jfill(logical(tile(double(lhedge+rhedge+tvedge+bvedge > 0),ones(1,1,nclr)))) = 1;
ssimg(2:2:end,1:2:end,:) = hfill;
ssimg(1:2:end,2:2:end,:) = vfill;
ssimg(2:2:end,2:2:end,:) = jfill;

if (nargout == 0)
    figure
    imagesc(ssimg);
    axis image
    colormap gray
    clear ssimg
end;
