How to write object connectivity code?

2 次查看(过去 30 天)
Actually I am writing a code for detecting a line and its size.But I encountered some problems.
Can=im2bw(imread('Ims.png'));
[X Y]= size(Can);
result=[];
region=[];
for j= 1:Y;
i=1; % initialize the while loop
while i<299
if (i~= 1)
if (find(CellOutline(j,i)==0)) % check pixel itself is black
if (CellOutline(i-1,j) ==0)&& (CellOutline(i+1,j)==0) %check left pixel and right pixel of pixel(i) s black
region(i+1,j)=CellOutline(i+1,j); % write pixel(i+1,j) value into region
end
end
elseif (CellOutline(i,j)==0)&&(CellOutline(i+1,j)==0) %When i=1, check the forward point and point itself.
region(i,j)=CellOutline(i,j); %write the value into region.
region(i+1,j)=CellOutline(i+1,j); %\/
else
end
i=i+1;
end
end
I know lots of flaws in this code. Can this code even detect a continuous black line on binary image ?

回答(1 个)

Image Analyst
Image Analyst 2014-8-8
Not sure what that code does. To detect a black line in a gray scale image, do
blackLine = (grayImage == 0);
This actually detects all pure black pixels. You might have to filter it to find line-like region shapes. See my Image Segmentation Tutorial.
If that doesn't work for you then attach your Ims.png image so we can try your code or other code of our own.
  8 个评论
tabw
tabw 2014-8-10
编辑:tabw 2014-8-10
DO you understand the above code "gsize = 100 ; # Predicted number of slices" ? it is a 2D image. Where the slices come from? Or it partition 2D image to 100 regions by 100 slices?
Besides, the algorithm of connected object is so different from what I thought. I thought It is doing checking on every pixel and its neighbour.
BTW, I wanna ask Is there any method can calculate volume of every discrete object on stacks image? I just need how many pixels each object contains.
Image Analyst
Image Analyst 2014-8-10
No I don't. I'd guess that they know in advance how many slices there are.
To calculate region volumes in 3D call bwconncomp() followed by regionprops().

请先登录,再进行评论。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by