How can i condense a binary matrix?

9 views (last 30 days)
Dan
Dan on 23 Nov 2016
Commented: Image Analyst on 30 Nov 2016
The image attatched is what I'm working with (I've added the grids and numbers to help me explain).
I'm hoping to use MATLAB to help me achieve a table that logs every time the category changes, and at what value. e.g at 1 the category is 3, at 1.6 the category goes to 5, at 2 the category goes to 8.
The way i have gone about this so far is to export the binary image so i have the pixel matrix.
I've then extracted 20 columns out of the pixel matrix to correspond to the 20 categories:
BW1 is my binary image. 25, 74 etc have come from the pixel coordinate in the centre of each category column.
>>BW1(:,[25 74 123 172 221 270 319 368 417 466 515 564 613 662 711 760 809 858 907 956])
So now I'm left with a table which has the columns i need, and is as deep as the pixel count.I then need to add a column in this table which uses a conversion factor to convert the pixels to my scale (6/886px).
However, I'm struggling to find a way to condense my table down to just the tops now (basically whenever there is a change in category).
I was wondering if i could somehow set an increment and then get a return only at these increments?
Can anybody help me, or point me in the right direction of some further reading.
Alternatively, if there is a more efficient way of doing this id be pleased to hear it, like i say im new to this, and its a little overwhelming trying to find a solution because there seem to be so many ways to do things!
Many thanks for your time.
  4 Comments
Jan
Jan on 24 Nov 2016
Edited: Image Analyst on 25 Nov 2016
@Dan: I simply used the "Image" button to let your images appear directly in your question. Alternatively include the image's URL in << and >> .

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 25 Nov 2016
I'm not exactly sure what you need. I'd just call imclearborder to get rid of white stuff touching the border. Then I'd call bwareafilt() or bwareaopen() to extract just the large blobs. Then you can get an average vertical profile along each column (band of column pixels)
verticalProfile = mean(binaryImage(:, col1:col2), 2) > 128;
then use strfind() to look for patterns of [0, 1] and [1, 0] to find out where the block(s) start and stop in the vertical direction.
  4 Comments
Dan
Dan on 30 Nov 2016
That's great, it's really helping, thank you.
The only problem occurs when there is more than one blob in the same column. In the example above, col 4, it takes the top of the first blob and the base of the second. I need to know the top of the second blob as well.
Image Analyst
Image Analyst on 30 Nov 2016
If you're going to have more than one blob per column, then you're going to have to use strfind():
>> a=[0 1 1 1 0 1 1 0 1 1]
a =
0 1 1 1 0 1 1 0 1 1
>> strfind(a, [0, 1])
ans =
1 5 8

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!