我正在尝试计算范围内的像素。我计算了范围,但我不能计算包含0的像素。
我的代码:
first_value_of_i = zeros(M,1);
first_value_of_j = zeros(N,1);
second_value_of_i = zeros(M,1);
second_value_of_j =zeros(N,1);
for i = 2:M-1
for j = 2:N-1
Pix = [image(i,j) image(i-1,j) image(i-1,j+1) image(i,j+1) image(i+1,j+1) image(i+1,j) image(i+1,j-1) image(i,j-1) image(i-1,j-1) image(i-1,j)];
if((Pix(1)==0) && ( (Pix(2)*Pix(3)*Pix(4)*Pix(5)*Pix(6)==0) || (Pix(2)*Pix(6)*Pix(7)*Pix(8)*Pix(9)==0)))
if (((Pix(2)+Pix(6)+Pix(7)+Pix(8)+Pix(9))>=5) || ((Pix(2)+Pix(3)+Pix(4)+Pix(5)+Pix(6))>=5))
first_value_of_i(i,1) = i;
first_value_of_j(j,1) = j;
end
if (((Pix(2)+Pix(3)+Pix(4)+Pix(8)+Pix(9))>=5) || ((Pix(4)+Pix(5)+Pix(6)+Pix(7)+Pix(8))>=5))
second_value_of_i(i,1) = i;
second_value_of_j(j,1) = j;
end
end % if
end %for
end %for
rest_stroke = zeros(M,2);
for p = first_value_of_i : second_value_of_i
count_rest=0;
for q = first_value_of_j:second_value_of_j
if((image(p,q)==0))
count_rest=count_rest+1;
end
end
rest_stroke(i,1)=i;
rest_stroke(i,2)=count_rest;
end
但输出显示:
下标索引必须是实数正整数或逻辑。
尝试时出错(第173行)
如果((图像(P,Q)== 0))
根据我的计算,它应该没问题,但显然不是。谁能帮我吗?
答案 0 :(得分:2)
for i=2:M-1
for j = 2:N-1
i
从2开始。first_value_of_i(1,1)
初始化为0且未更改。