bwlabeln problem.*
조회 수: 1 (최근 30일)
이전 댓글 표시
*Hello to everyone on matlab community!
I m trying to use bwlabeln to find the connected pixels tha construct a gaussian spot. So i have my table Z =
0.0000 0.0001 0.0007 0.0035 0.0111 0.0236 0.0337 0.0323
0.0000 0.0003 0.0020 0.0095 0.0301 0.0640 0.0915 0.0877
0.0001 0.0007 0.0046 0.0217 0.0686 0.1459 0.2084 0.1999
0.0001 0.0013 0.0088 0.0414 0.1311 0.2788 0.3982 0.3820
0.0002 0.0020 0.0141 0.0664 0.2101 0.4469 0.6382 0.6121
0.0003 0.0027 0.0189 0.0892 0.2824 0.6005 0.8577 0.8226
0.0003 0.0030 0.0213 0.1005 0.3183 0.6769 0.9666 0.9272
0.0003 0.0029 0.0202 0.0950 0.3009 0.6398 0.9137 0
and i write
the following code but it doesn't return all the connected pixels of my table.
BW=im2bw(Z); [L,NUM] = bwlabeln(BW) for i=1:NUM r=0; c=0; [r,c]=find(L==i) size(r) temp=0; temp(r,c)=Z(r,c) end
i take temp =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0.4469 0.6382 0.6121
0 0 0 0 0 0.6005 0.8577 0.8226
0 0 0 0 0 0.6769 0.9666 0.9272
0 0 0 0 0 0.6398 0.9137 0
that is'n right!!Because i have and other connectd components to the spot *
댓글 수: 0
채택된 답변
Walter Roberson
2011년 2월 12일
The output is correct according to what your code requests.
When you use im2bw(Z) without specifying the level, the default level used is 0.5. All of the pixels with level >= 0.5 are in the lower right corner. If you examine your output table you will see that it has included them all, and as well has included 0.4469 . That level, being less than 0.5, would be taken to 0 by im2bw and so would not be identified as a connected component. The bug in your code (if we take the first statement as being correct) is not that too few pixels are included, but rather that too many pixels are included.
To see why extra pixels are included, we need to look further.
The pixels in the lower right, value >= 0.5, are all together and will be identified as a single component with label 1. NUM will thus be 1, and the for loop over "i" will only execute for i=1 . The find(L==i) will then be find(L==1) which will be the locations of those lower-right pixels with value >= 0.5 . The [r,c] version of find() produces a row and column list, L(r(K),c(K)) == 1 for K = 1 : length(r) .
You then take that row and column list and use it as indices of temp and Z. However, each of r and c is a list, and the result of using a list as a subscript is to use all the subscript pairs (r(J),c(K)) for J = 1:length(r) and K = 1:length(c), rather than using the subscript pairs (r(K),c(K)) only (the identified pixels). Thus what will be copied in your code is always a rectangular block, not just the identified pixels; it so happens that the one element that is in that rectangle that was not individually identified was the 0.4469 pixel.
The easiest way to fix your copy logic is to use idx = find(L==i); and then temp(idx) = Z(idx) . If you do that, the pesky 0.4469 will disappear.
If you believe that you have other connected pixels, then you are either expecting im2bw() to use a different (lower) threshold, or you want BW = logical(Z) which is the same as BW = Z ~= 0
As you are working with a 2D array and default connectivity, you might as well be less confusing by using bwlabel(Z) instead of bwlabeln(Z) -- though apparently if your software is new enough, bwconncomp() is recommended as being less memory-intensive and probably faster.
댓글 수: 0
추가 답변 (2개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!