确定轮廓周围的边界框

时间:2017-03-31 17:44:00

标签: matlab

我正在尝试确定contourf图中特定轮廓水平周围的边界框:

  1. 绘制数据的contourf图
  2. 在同一图表上绘制等级为0.075的轮廓。
  3. 从轮廓水平提取轮廓数据
  4. 计算每个轮廓数据的质心
  5. 为质心x y的每个元素执行循环并计算到其他元素的距离 质心x y。将距离小于阈值5的轮廓数据合并, 并将其放置在区域内。
  6. 重新绘制数据并创建围绕区域的框x x每个区域的数据 同样的情节。
  7. 以下是生成contourf图并尝试创建边界框的代码:

    axes(handles.axes1);
    cla(handles.axes1);
    
    contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
    colormap(handles.axes1,'jet');
    colorbar(handles.axes1);
    axis equal
    caxis([cMin cMax]);
    hold on
    
    
    [Cm,hm] = contour(xC(1,:),yC(:,1),zDiff,[0.075 0.075],'k');
    axis equal
    
    
    Sm = contourdata(Cm);
    
    hwb = waitbar(0.1, 'Processing')
    
    [p,q] = size(Sm);
    
    
    for i = 1:q
    
        pts = centroid(Sm(1,i).xdata,Sm(1,i).ydata);
        format long
        centCt(i,1) = pts(1);
        centCt(i,2) = pts(2);
    end
    
    
    [centCxs,indxs] = sort(centCt(:,1));
    centCys = centCt(indxs,2);
    
    
    Se = Sm;
    
    for i = 1:q
    
    Se(1,i).flag = 0;
    
    
    
    end
    
    
    ct = 1;
    
    for j = 1:q
    datax = [];
    datay = [];
     ct2 = 1;
        for i = 1:q
    
            rcentd  = sqrt((centCxs(i,1) - centCxs(j,1)).^2 + (centCys(i,1) - centCys(j,1)).^2);
    
            if(rcentd < 5)
                if(Se(1,i).flag == 0)
                  datax = [datax;Se(1,i).xdata];
                  datay = [datay;Se(1,i).ydata];
    
                  Se(1,i).flag = 1;
    
    
                  ct2 = ct2 + 1;
    
                end
    
            end
        end
    
    if(ct2 > 5)
    
         area(ct).xdat = datax;
         area(ct).ydat = datay;
         ct = ct + 1;
    end
    
    
    end
    
    
    close(hwb);
    
    
    
    cla(handles.axes1);    
    contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
    colormap(handles.axes1,'jet');
    colorbar(handles.axes1);
    axis equal
    caxis([cMin cMax]);
    hold on
    
    hold(handles.axes1,'on');
    
    
    for i = 1:length(area)
        [rectx,recty] = minboundrect(area(i).xdat,area(i).ydat);
        plot(rectx,recty,'-k');   
    end
    

    初始轮廓图:

    enter image description here

    我得到的结果:

    enter image description here

    期望的结果:

    enter image description here

1 个答案:

答案 0 :(得分:1)

你唯一需要改变的是你计算边界框的最后一部分:

for i = 1:length(area)
    minMaxX = [min(area(i).xdat),max(area(i).xdat)];
    minMaxY = [min(area(i).ydat),max(area(i).ydat)];
    rect = [minMaxX(1),minMaxY(1),diff(minMaxX),diff(minMaxY)];
    plot(area(i).xdat,area(i).ydat,'.')
    rectangle('Position',rect,'EdgeColor','k')
end

如果您将运行整个修改过的脚本(需要contourdata):

[xC,yC] = meshgrid(-5:0.1:5);
zDiff = abs(peaks(xC,yC));
subplot(121);
contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
colormap('jet');
colorbar();
axis equal
hold on

[Cm,hm] = contour(xC(1,:),yC(:,1),zDiff,3 + [0 0],'k');
axis equal

Sm = contourdata(Cm);
[p,q] = size(Sm);
for i = 1:q
    pts = centroid(Sm(1,i).xdata,Sm(1,i).ydata);
    format long
    centCt(i,1) = pts(1);
    centCt(i,2) = pts(2);
end
area = struct([]);
[centCxs,indxs] = sort(centCt(:,1));
centCys = centCt(indxs,2);
Se = Sm;
for i = 1:q
    Se(1,i).flag = 0;
end
ct = 1;
for j = 1:q
    datax = [];
    datay = [];
    ct2 = 1;
    for i = 1:q
        rcentd  = sqrt((centCxs(i,1) - centCxs(j,1)).^2 + (centCys(i,1) - centCys(j,1)).^2);
        if(rcentd < 1.5)
            if(Se(1,i).flag == 0)
                datax = [datax;Se(1,i).xdata];
                datay = [datay;Se(1,i).ydata];

                Se(1,i).flag = 1;
                ct2 = ct2 + 1;
            end
        end
    end

    if(ct2 > 1)
        area(ct).xdat = datax;
        area(ct).ydat = datay;
        ct = ct + 1;
    end
end

%
subplot(122);
contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
colormap('jet');
colorbar();
axis equal
hold on

for i = 1:length(area)
    minMaxX = [min(area(i).xdat),max(area(i).xdat)];
    minMaxY = [min(area(i).ydat),max(area(i).ydat)];
    rect = [minMaxX(1),minMaxY(1),diff(minMaxX),diff(minMaxY)];
    rectangle('Position',rect,'EdgeColor','k')
end

你会得到:

enter image description here