How to write object connectivity code?
显示 更早的评论
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
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 个评论
Image Analyst
2014-8-8
Not sure how you define "my own code". I see nothing wrong with using regionprops() and that's not your own code. Likewise if you look over my example and adapt it, it will be at least somewhat your own code, depending on how much you adapt it. Are you saying now that you actually don't want any help because if you got it, you would not be able to use it?
Image Analyst
2014-8-9
You can try to edit the function
>> edit bwconncomp.m
but usually that just shows some of the code in a wrapper and much of the code is hidden in a DLL. If you have to use all open source code, then you'll have to write it in some other language, like C++ and the OpenCV library.
tabw
2014-8-9
Image Analyst
2014-8-9
Looks like it. You'll have to change them. New in R2014a (I believe) is that you can use %{ and %} to enclose large blocks:
%{
This line is commented out
So is this line.
%}
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().
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!