2017-03-31 93 views
0

我試圖確定圍繞在contourf情節特定的輪廓水平邊框的邊框:確定周圍的輪廓

  1. 繪製數據的contourf情節
  2. 情節與0.075的水平輪廓在同一個情節。
  3. 提取從輪廓水平
  4. 輪廓數據計算每個輪廓數據
  5. Do循環爲質心X Y的每個元素的質心,並計算到其它 質心x和y距離。合併輪廓數據的距離小於閾值5, 並將其放置在區域中。
  6. 重新繪製數據並在同一圖上創建每個區域周圍區域x y數據的框。

這裏是生成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

+0

試圖理解你得到的結果,使用[MATLAB調試器(https://nl.mathworks.com/help/matlab/matlab_prog/debugging-process-和-features.html)可能會非常有幫助。如果你明白你爲什麼得到這個結果,你可能會自己解決問題。爲我們提供示例圖像,對我們也可能非常有幫助。 – m7913d

回答

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