跟随边缘的影像轮廓

时间:2018-11-30 15:37:47

标签: image matlab plot contour threshold

如何在由阈值定义但实际上跟随图像边缘的图像上绘制轮廓,而不是默认情况下轮廓是什么?

通过运行以下内容,

pos

我得到以下信息, enter image description here

但我想拥有

enter image description here

question及其答案非常接近并且非常有帮助,但是仍然有些混乱。我想几乎有内置的解决方案?

1 个答案:

答案 0 :(得分:1)

我没有找到任何内置的解决方案,因此,我将发布我的版本,该版本不是特别优雅也不高效,但可能会对其他人有所帮助。

function contourEdges(x,y,u,varargin)
    %CONTOUREDGES Contour plot following pixel edges.
    %   CONTOUREDGES(X,Y,Z,V) draw a contour line for each
    %   level specified in vector V.  Use CONTOUREDGES(X,Y,Z,[v v]) to 
    %   draw contours for the single level v.
    %   X and Y must be rows (or column) vecors and Z must be a MxN matrix.
    %
    %   Example:
    %       
    %       data = gauss2d(23,37);
    %       x = linspace(0,1,37);
    %       y = linspace(5,20,23);
    %       imagesc(x,y,data)
    %       m = mean(mean(data));        
    %       %To plot only the level m
    %       contourEdges(x,y,data,[m m])
    %       %To plot more levels: contourEdges(x,y,data,[m/2 m 2*m])
if isempty(varargin) || numel(varargin{1}) < 2
    message = ['Automatic selection of levels is not supported yet. A'...
        'simple workaround is to run [~,C] = contour(x,y,Z,{n}) and then'...
        'use C.LevelList as the fourth parameter here.'];
    error(message);
else
    contourLevels = unique(varargin{1});
end
x = [x(1)-(x(2)-x(1)); x(:); x(end)+(x(2)-x(1))];
y = [y(1)-(y(2)-y(1)); y(:); y(end)+(y(2)-y(1))];
edgesx = mean([x(2:end)';x(1:end-1)']);
edgesy = mean([y(2:end)';y(1:end-1)']);
inputExist = find(cellfun(@(x) strcmpi(x, 'linewidth') , varargin));
if isempty(inputExist)
  varargin{end+1} = 'linewidth';
  varargin{end+1} = 3;
end
if length(varargin{2})>4 && isempty(inputExist)
    varargin{end+1} = 'color';
    varargin{end+1} = [1 0 0];
end
ax = gca;
for contIdx=1:length(contourLevels)
    idx = u > contourLevels(contIdx);
    [a,b] = find(idx);
    for i=1:length(a)
            if b(i) == 1
                ax.ColorOrderIndex = 1;
                hold on;plot([edgesx(b(i)) edgesx(b(i))],[edgesy(a(i)) edgesy(a(i)+1)],varargin{2:end});
            end
            if b(i) ~= size(idx,2)
                if isempty(find(a==a(i) & b==(b(i)+1),1))
                    ax.ColorOrderIndex = 1;
                    hold on;plot([edgesx(b(i)+1) edgesx(b(i)+1)],[edgesy(a(i)) edgesy(a(i)+1)],varargin{2:end});
                end
            end
            if b(i) == size(idx,2)
                ax.ColorOrderIndex = 1;
                hold on;plot([edgesx(b(i)+1) edgesx(b(i)+1)],[edgesy(a(i)) edgesy(a(i)+1)],varargin{2:end});
            end
            if b(i) ~= 1
                if isempty(find(a==a(i) & b==(b(i)-1),1))
                    ax.ColorOrderIndex = 1;
                    hold on;plot([edgesx(b(i)) edgesx(b(i))],[edgesy(a(i)) edgesy(a(i)+1)],varargin{2:end});
                end
            end

            if a(i) == 1
                ax.ColorOrderIndex = 1;
                hold on;plot([edgesx(b(i)) edgesx(b(i)+1)],[edgesy(a(i)) edgesy(a(i))],varargin{2:end});
            end
            if a(i) ~= size(idx,1)
                if isempty(find(b==b(i) & a==(a(i)+1),1))
                    ax.ColorOrderIndex = 1;
                    hold on;plot([edgesx(b(i)) edgesx(b(i)+1)] ,[edgesy(a(i)+1) edgesy(a(i)+1)],varargin{2:end});
                end
            end
            if a(i) == size(idx,1)
                ax.ColorOrderIndex = 1;
                hold on;plot([edgesx(b(i)) edgesx(b(i)+1)],[edgesy(a(i)+1) edgesy(a(i)+1)],varargin{2:end});
            end
            if a(i) ~= 1
                if isempty(find(b==b(i) & a==(a(i)-1),1))
                    ax.ColorOrderIndex = 1;
                    hold on;plot([edgesx(b(i)) edgesx(b(i)+1)],[edgesy(a(i)) edgesy(a(i))],varargin{2:end});
                end
            end
    end
end