English 中文(简体)
材料库中的矩阵?
原标题:Matrix in matlab?

我正致力于“联接部件”标签,我的矩阵是:

 1     1     0     2     2     2     0     3
 1     1     0     2     0     2     0     3
 1     1     1     1     0     0     0     3
 0     0     0     0     0     0     0     3
 4     4     4     4     0     5     0     3
 0     0     0     4     0     5     0     3
 6     6     0     4     0     0     0     3
 6     6     0     4     0     7     7     7

并且现在,我想对第二点进行扫描,因为这样一来,我就做了以下编码:

for i=1:1:r
    for j=1:1:c
        if (bw(i,j)>=1) 

            if (i-1>0 & i+1<=r) 
                % if ( bw(i,j)~= bw(i-1,j) | bw(i,j)~= bw(i+1,j))
                if ( (bw(i,j)~= bw(i-1,j) & bw(i-1,j)>0))
                    bw(i,j)= min (bw(i-1,j),bw(i,j))
                elseif ((bw(i,j)~= bw(i+1,j) & bw(i+1,j)>0))
                    bw(i,j) = min(bw(i+1,j),bw(i,j));
                end
            end

            if (j-1>0 & j+1<=c) 
                if ( (bw(i,j)~= bw(i,j-1) & bw(i,j-1)>0))
                    bw(i,j) = min (bw(i,j-1),bw(i,j));
                elseif((bw(i,j)~= bw(i,j+1) & bw(i,j+1)>0))
                    bw(i,j) = min (bw(i,j+1),bw(i,j))  ; 
                end
            end

        end
    end
end
disp(bw);

但问题是,在实行这一法典时,即取得以下产出:

 1     1     0     2     2     2     0     3
 1     1     0     1     0     2     0     3
 1     1     1     1     0     0     0     3
 0     0     0     0     0     0     0     3
 4     4     4     4     0     5     0     3
 0     0     0     4     0     5     0     3
 6     6     0     4     0     0     0     3
 6     6     0     4     0     7     7     7

only one value changes (2nd row, 4 col) in my result, whereas, I want:

     1     1     0     1     1     1     0     3
     1     1     0     1     0     1     0     3
     1     1     1     1     0     0     0     3
     0     0     0     0     0     0     0     3
     4     4     4     4     0     5     0     3
     0     0     0     4     0     5     0     3
     6     6     0     4     0     0     0     3
     6     6     0     4     0     3     3     3

有些人可以帮助? 什么地方会犯错?

最佳回答

Jonas的尼斯解决办法。 由于我看到这番话,我也想到我的解决办法。 希望这比你原来的法典更加相似。

你所犯的一个错误是假定这一联系,只能用一个通行证。 一般来说,由于对一些“snake分子”的探测取决于你如何通过矩阵进行检索。 除此之外,我外加一.。

 bw=[  1     1     0     2     2     2     0     3;
        1     1     0     2     0     2     0     3;
        1     1     1     1     0     0     0     3;
        0     0     0     0     0     0     0     3;
        4     4     4     4     0     5     0     3;
        0     0     0     4     0     5     0     3;
        6     6     0     4     0     0     0     3;
        6     6     0     4     0     7     7     7 ];


 %Set up matrix   
 [r,c] = size(bw);   

 %Zero pad border
 bwZ = [zeros(1,c+2);[zeros(r,1) bw zeros(r,1)];zeros(1,c+2)];

 %Iterate over all elements within zero padded border
 done=0;%Done flag 
 cc=1;  %Infinite loop protection
 while not(done) && cc<r*c
     done=1;cc=cc+1;
     for i=2:r+1
         for j=2:c+1
            %Point should be evaluated
            p = bwZ(i,j);
            if p >= 1
                %Pick out elements around active elements
                if bwZ(i-1,j)==0;ue=inf;else ue = bwZ(i-1,j); end;%Up element
                if bwZ(i+1,j)==0;de=inf;else de = bwZ(i+1,j); end;%Down element
                if bwZ(i,j-1)==0;le=inf;else le = bwZ(i,j-1); end;%Left element
                if bwZ(i,j+1)==0;re=inf;else re = bwZ(i,j+1); end;%Right element

                bwZ(i,j) = min([ue de le re]);
                %Set flag, if something has changed 
               if bwZ(i,j) ~= p
                    done = 0;
               end
            end
         end
     end
 end

 %Remove zero padding
 bw= bwZ(2:end-1,2:end-1)

Output:

bw=

 1     1     0     1     1     1     0     3
 1     1     0     1     0     1     0     3
 1     1     1     1     0     0     0     3
 0     0     0     0     0     0     0     3
 4     4     4     4     0     5     0     3
 0     0     0     4     0     5     0     3
 6     6     0     4     0     0     0     3
 6     6     0     4     0     3     3     3
问题回答

如果数字不需要保留,你可以简单地把<条码>bwlabel<>/条码>放在你原来的形象上:

newImage = bwlabel(originalImage>0);

http://www.ohchr.org。

Here s another version. It checks each connected component to see whether there s another connected component touching it. If yes, that connected component is relabelled.

%# nCC: number of connected components
nCC = max(originalImage(:));

for cc = 1:nCC
%# check whether the component exists
myCC = originalImage==cc;
if any(any(myCC))

   %# create a mask to check for neighbors
   %# by creating a border of 1 pixel
   %# around the original label
   msk = imdilate(myCC,true(3)) & ~myCC;

   %# read all the pixel values under the mask
   neighbours = originalImage(msk);

   %# we re not interested in zeros, remove them
   neighbours = neighbours(neighbours > 0);

   if ~isempty(neighbours)
   %# set the label of all neighbours to cc
   originalImage( ismember(originalImage,neighbours) ) = cc;
   end
end
end




相关问题
Resources for Image Recognition

I am looking for a recommendation for an introduction to image processing algorithms (face and shape recognition, etc.) and wondered if anyone had an good recommendations, either for books, ...

Good reference book for digital image processing? [closed]

I am learning digital image processing on my own and would like recomendations on good reference books. If you know of books to definately stay away from that would be useful as well. Thanks

Python Tesseract can t recognize this font

I have this image: I want to read it to a string using python, which I didn t think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract. So I started ...

What s the quickest way to parallelize code?

I have an image processing routine that I believe could be made very parallel very quickly. Each pixel needs to have roughly 2k operations done on it in a way that doesn t depend on the operations ...

Computing object statistics from the second central moments

I m currently working on writing a version of the MATLAB RegionProps function for GNU Octave. I have most of it implemented, but I m still struggling with the implementation of a few parts. I had ...

Viola-Jones face detection claims 180k features

I ve been implementing an adaptation of Viola-Jones face detection algorithm. The technique relies upon placing a subframe of 24x24 pixels within an image, and subsequently placing rectangular ...

热门标签