How to find pixel connect in column matlab

1 view (last 30 days)
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
Jassy
Jassy on 12 May 2019
Sorry. I don't see reply. I see did after i edited.
I need this because I need to count line in pic.
Ex 1 line
pic1 = [
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 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]
result1 = 1 1 1 1 1 1 1 0 0 0
Ex 2 line
pic2 = [
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 1
0 0 0 1 0 0 0 0 1 0
0 0 0 1 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 1 0 0 0 0
0 1 0 0 1 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0]
result = 1 1 1 2 2 2 2 1 1 1
Image Analyst
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?

Sign in to comment.

Accepted Answer

Image Analyst
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
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);

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 12 May 2019
+any(pic)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!