我正在尝试确定contourf图中特定轮廓水平周围的边界框:
以下是生成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
初始轮廓图:
我得到的结果:
期望的结果:
答案 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
你会得到: