我有两个向量,一个存储x坐标,另一个存储y坐标。当我尝试在循环中访问它们时,它在循环的每次迭代中返回相同的坐标。
有人可以帮我解决我的问题吗?
N2=8;
info2 = repmat(struct, ceil(size(Z, 1) / N2), ceil(size(Z, 2) / N2)); %creates another array of structs consisting of an m-by-n tiling
for row1 = 1:N2:size(Z, 1)%loop through each pixel in the 8x8 window
for col1 = 1:N2:size(Z, 2)
x = (row1 - 1) / N2 + 1;
y = (col1 - 1) / N2 + 1;
imgWindow2 = Z(row1:min(end,row1+N2-1), col1:min(end,col1+N2-1));
average2 = mean(imgWindow2(:));
window2(x,y).average=average2;
% if the intensity of the 8x8 window is greater than
% 210 then considered suspicious- calculate GLCM-
if average2>100
% display('greater than 100');
%best direction is 0
offsets0 = [0 1];
glcms = graycomatrix(imgWindow2,'Offset',offsets0);
stats = graycoprops(glcms,'all'); %normalize GLCM so that values are between 0 and 1
correlation=[stats.Correlation];
contrast=[stats.Contrast];
homogeneity=[stats.Homogeneity];
energy=[stats.Energy];
%if these conditions are met then this window
%contains an ROI
if (homogeneity > 0.9)
if (contrast<0.2)
if (energy>0.6)
for i=1:length(coordsX)
coordsX(i)=row1;
for j=1:length(coordsY)
coordsY(j)=col1;
end
end
for ii=1:length(coordsX)
coX=coordsX(ii);
for jj=1: length(coordsY)
coY=coordsY(jj)
Z1 = insertShape(Z, 'rectangle', [coX coY 8 8]);
figure(2);
end
end
end
end
end
答案 0 :(得分:0)
嵌套for
循环有两个问题:
i
两次,因此您将重置循环中最外层循环索引的值,从来不是一个好主意coordsX
,但在循环内部您访问coordsY
。在此,您很幸运coordsX
和coordsY
似乎具有相同的尺寸。大多数情况下,你会收到一个错误。建议修复:
for ii=1:length(coordsX)
coX=coordsX(ii);
for jj=1: length(coordsY)
coY=coordsY(jj)
Z1 = insertShape(Z, 'rectangle', [coX coY 8 8]);
figure(2);
end
end
请注意,我使用ii
和jj
作为索引,而不是i
和j
,它们通常用于MATLAB中的复数。