How to find pixel connect in column matlab
1 view (last 30 days)
Show older comments
I have line picture. and I want to sum column.
if value in column connected same column 3 and 4. count 1
if is not connected -> plus
Ex. pic
pic = [
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0]
result = 1 1 1 2 2 0 0 0 0 0
Thank you.
5 Comments
Image Analyst
on 12 May 2019
The code below in my Answer also works for your pic2, giving you the results you stated. Are you looking below in the "Answers" section, or did you not scroll down and are just looking at the "Comments" section up here?
Accepted Answer
Image Analyst
on 12 May 2019
This will give you
result =
1 1 1 2 2 0 0 0 0 0
exactly as you wanted (in your current version of the question):
pic = [
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0]
[rows, columns] = size(pic);
for col = 1 : columns
[~, result(col)] = bwlabel(pic(:, col))
end
1 Comment
Image Analyst
on 12 May 2019
If you want to count the total number of lines in the image, instead of how many lines are in each column, you can just use connected component labeling, like
[labeledLines, totalNumberOfLines] = bwlabel(pic);
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!