2014-04-29 5 views
-1

두 벡터, 즉 x 좌표를 저장하는 벡터와 y 좌표를 저장하는 벡터가 있습니다. 루프에서 액세스하려고하면 루프의 모든 반복에서 동일한 좌표가 반환됩니다.Matlab, 벡터 요소에 접근하기

내 루프 수정을 도와 줄 수있는 사람이 있습니까? 당신의 중첩 for 루프 두 가지 문제가 있습니다

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

: 당신은 루프 내에서 가장 바깥 쪽 루프 인덱스의 값을 재설정하는, 그래서 당신은 두 번 같은 인덱스 i를 사용

  1. 는 결코 좋은 생각
  2. 가장 안쪽의 루프는 coordsX에 있지만 내부 루프는 coordsY입니다. 여기에 coordsXcoordsY의 크기가 같아서 운이 좋았습니다. 대부분의 경우 오류가 발생합니다.

수정 방법 : 나는 보통 MATLAB에서 복잡한 번호를 사용하는 대신 ij의 지표로 iijj을 사용했다

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 

참고.

+0

고마워,이 수정 프로그램을 시도했지만 하나의 사각형 만 이미지에 나타납니다. 이미지는 매번 같은 위치에 사각형으로 여러 번 열립니다. 각 좌표에 대해 하나의 이미지에 여러 개의 직사각형이있는 이미지를 어떻게 표시 할 수 있는지 알고 있습니까? – user1853871

+0

아마도 'hold on'을 사용하여 이전 직사각형이 플롯에 남아 있는지 확인해야 할 것입니다. – am304

+0

감사합니다. 그러나 배열의 모든 x와 y 좌표는 같은 값입니다. – user1853871