How to write object connectivity code?
1 view (last 30 days)
Show older comments
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 ?
0 Comments
Answers (1)
Image Analyst
on 8 Aug 2014
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 Comments
Image Analyst
on 10 Aug 2014
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().
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!