LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fit rectangle into Binary Particle

Hello all,

 

I have a problem with a function I am trying to implement: I would like to get the biggest Rectangle that fits into a region in my image. By biggest I mean the biggest area. Here's a graphic to illustrate:

Best fit rectangle.png

 

My form will look something like the black one (inmy Image the entire form is filled with white) and I would like my result to look something like the green rectangle.

So far I have tried the following: Detect falling edge from top to bottom gives me the lower line. I then rotate the image so the lower line is horizontal to avoid using math. Then I use the base line to detect intersects with the side from where I draw up to get the upper points. Then I select the correct point and draw the rectangle.

This works fine for some images, but others are a rotated or a corner might be missing which is when it gives me bad results.

 

Does anyone have a pointer as to how to better implement this? Any help is greatly appreciated!



Remember Cunningham's Law
0 Kudos
Message 1 of 9
(2,240 Views)

Do you need just the size/area, or you need the coordinates too?

0 Kudos
Message 2 of 9
(2,206 Views)

Hi Stockson,

 

that really depends on the difficulty. In general the size with width and height would be enough. I would also like to visualize the rectangle, but that's more a nice to have.



Remember Cunningham's Law
0 Kudos
Message 3 of 9
(2,192 Views)

So, you are looking for the maximum rectangle within the black border?

 

How fast does your solution need to be?

0 Kudos
Message 4 of 9
(2,186 Views)

Yes, I'm looking for the rectangle with the biggest area within the black shape (in my sketch).

The solution does not have to be very fast. If it completes within a couple of seconds it's OK.



Remember Cunningham's Law
0 Kudos
Message 5 of 9
(2,172 Views)

Could you provide some real data?

 

e.g. is the object always  symmetric?

 

0 Kudos
Message 6 of 9
(2,155 Views)

So here's my suggestion:

 

I would try to calculate the two eigenvectors of the binary image, taking only the object-pixels in consideration.

This will give you a direction and a length for each of the two image-eigenvectors.

 

Furthermore, it might be helpful to calculate the center of mass, which is the sum of the objects row indices diveded by the number of object pixels and the sum of the objects column indices  diveded by the number of object pixels

 

I would use this Information as a starting point of finding the maximum rectangle within the object.

 

 

0 Kudos
Message 7 of 9
(2,130 Views)

Hi Alex,

 

thanks for the suggestion.

To be totally honest I didn't get much of that. What use will the eigenvectors be towards finding the rectangle? And how would I calculate them?

Because I understand so little of that it seems a quicker solution to just refine my other algorithm, so it works even for heavily distorted images.

 

As for the center of mass, I can just get that from the particle anylsis, can't I?



Remember Cunningham's Law
0 Kudos
Message 8 of 9
(2,115 Views)

@PeterFoerster wrote:

Hi Alex,

 

thanks for the suggestion.

To be totally honest I didn't get much of that. And how would I calculate them?

Because I understand so little of that it seems a quicker solution to just refine my other algorithm, so it works even for heavily distorted images.

 

As for the center of mass, I can just get that from the particle anylsis, can't I?


What use will the eigenvectors be towards finding the rectangle?

I thought, maybe there is a connection between the length of the eigenvector and the length of the rectangle you are looking for.

It's a guess, I might be completly wrong...

If one of the rectangle's sides -we are looking for- is parallel to the axis of rotation, the greater one of the two eigenvector will be parallel towards this axis of rotation.

 

And how would I calculate them?

It can be done using the built-in LabView function

http://zone.ni.com/reference/en-XX/help/371361J-01/gmath/eigenvalues_and_vectors/

 

...in combination with:

http://zone.ni.com/reference/en-XX/help/371361J-01/gmath/covariance_matrix/

and:

http://zone.ni.com/reference/en-XX/help/371361H-01/gmath/a_x_b/

 

here's another approach: http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/OWENS/LECT2/node3.html

 

As for the center of mass, I can just get that from the particle anylsis, can't I?

I don't know if the particle analysis is capable of doing this, but in general, calculating the (optical) center of mass,

is quite straight forward:

 

2016-11-16_201004_center of mass.png

0 Kudos
Message 9 of 9
(2,100 Views)