How to compare bounding box coordinates?

I have a number of bounding boxes for one greyimage, with one box for each connected component in the image. I need to figure out whichever boxes do not overlap with the rest and remove those pixels from the image.
For that, I want to be able to compare if there are overlaps between the bounding boxes, by comparing the corner coordinates or by using any function that can figure out any bit of overlap with other bounding boxes. After that, I need to set all the pixels in the boxes that do not overlap with any other to be black.
I need help with comparing and keeping track of the boxes and finally making the changes to the original image, before cropping into connected components.
Thank you very much in advance for your help! Any bit of help is very much appreciated! (:

Answers (3)

Tell me what the next step is, because I don't see why this strange thing would be necessary. I have never done that and I analyze regions all the time. Tell me what you really want to do and I'll tell you how to do it and most likely erasing blobs with non-overlapping bounding boxes won't be necessary. Upload your image to assist in your description.
One method would be to use VERT2LCON and LCON2VERT
Suppose that V1 contains the vertices of box 1 and V2 contains the vertices of box 2. If the following lines return an empty "result", then the boxes do not overlap. Otherwise, they do.
[A1,b1]=vert2lcon(V1);
[A2,b2]=vert2lcon(V2);
result=lcon2vert([A1;A2],[b1;b2]);

7 Comments

You could also use INPOLYGON
overlaptrue = any(inpolygon(V1(:,1),V1(:,2), V2(:,1),V2(:,2) ));
You'd also have to do the converse of that: find out if V2 is inside of V1 and then OR the two results together, as this illustrates:
v1x = [1 9 9 1 1];
v1y = [5 5 3 3 5];
plot(v1x, v1y, 'b-');
xlim([0 10]);
ylim([0 10]);
v2x = [3 5 5 3 3];
v2y = [8 8 4 4 8];
hold on;
plot(v2x, v2y, 'b-');
V1 = [v1x', v1y']
V2 = [v2x', v2y']
inpolygon(V1(:,1),V1(:,2), V2(:,1),V2(:,2)) % Show which points are inside.
inpolygon(V2(:,1),V2(:,2), V1(:,1),V1(:,2)) % Show which points are inside.
V1inV2 = any(inpolygon(V1(:,1),V1(:,2), V2(:,1),V2(:,2) ))
V2inV1 = any(inpolygon(V2(:,1),V2(:,2), V1(:,1),V1(:,2) ))
thereIsOverlap = V1inV2 | V2inV1
In general this won't work to determine overlapping polygons since it only detects if a polygon has its vertices inside the other one. For example it wouldn't work for two boxes that completely cross in like a + shape with no vertex of any box inside the other box. However it will always work for muffin's situation where the polygons are bounding boxes of separate regions because the crossing situation with no vertices inside could never happen in that case - there will always be at least one vertex inside the bounding box of the other, you can never have two boxes cross totally like in my demo above.
However I still stand by my answer that though she thinks she wants to do this I doubt if she needs to do this, and I'd like to know the actual situation why she thinks she needs this.
Good point. Well, then I fall back to the vert2lcon/lcon2vert solution. It will work regardless of the overlap configuration.
As for whether the OP ultimately needs this, your guess is as good as mine.
Hi there!
Thank you so much for your help!
I would like to remove the non-overlapping components so as to clean my data before carrying out further steps. I'm working on reconstructing some textual characters retrieved from videos for a project.
I have attached an image to make it clearer. I need to reconstruct the letter 'A' in the image to make it a single connected component so that it can be fed to OCR. Before that, I want to clean the data by removing some obvious unnecessary segments like that little segment in the top left of the image.
For that, I thought of the bounding box method, where I can draw bounding boxes for each connected component in the image and then remove those segments that do not overlap with any other. I thought of doing the removal by setting those pixels to black.
Hope you understand my problem more clearer now. I need to know how to deduce and keep track of those bounding boxes that do not overlap with any other. Please help me out. I really appreciate your help a lot! Thank you!
Matt J
Matt J on 4 Nov 2012
Edited: Matt J on 4 Nov 2012
Except that the bounding box of the upper bounary of the 'A' looks like it would overlap the stray bright pixels in the upper left of the image. I also see other stray pixels inside the array that you might want to erase and which don't have an isolated bounding box..
In any case, help has been given! Try the recommendations given to you so far.
How do you have the bounding box for the A? If you do, then you must have labeled it. But then there are other small regions inside the A, and those would also have labels and bounding boxes. And of course there is the large triangle in the A which would be its own region. Plus it looks like you may have breaks in the edges, so that the A may really be 2 or 3 or 4 regions, not just one, and would thus have bounding boxes that don't enclose the whole A. But let's just assume you didn't have any of those problems. Let's assume you had a nice A that had contiguous, non-broken outlines and you did an imfill on it so that you had a solid region with just one bounding box. Again, it's labeled so all you need to do is to extract that one label from the image using ismember(). That would get rid of any regions with a different label number that intruded into the bounding box of the A. So I'm glad you explained the situation because now we can see that what you thought you wanted to do is actually not necessary at all. You can do it easily and simply with ismember() and not have to worry at all with binary images or inspecting corner coordinates.
By the way, I don't really think finding edges to identify characters is the way to go. I think a background flattening followed by thresholding would be the way to go, but I'm not really familiar with OCR methods and if edge finding is a required step in some successful pubished method that you have seen, then maybe it's alright, though it seems fraught with additional problems that thresholding would not have (internal outlines, broken outlines, etc.).
There is no bounding box for A. Its just that the character is already segmented in the input data. You mentioned "Let's assume you had a nice A that had contiguous, non-broken outlines and you did an imfill on it so that you had a solid region with just one bounding box." I cannot do that assumption as I have to work with characters that have broken contour lines.
So i have to find a way to compare the bounding coordinates of the different connected components in the image then remove the non-overlapping regions. How do I do that?

Sign in to comment.

ratio = bboxOverlapRatio(bbox1, bbox2)
This can actually give you a matrix which shows how much overlap exists between a pair of bboxes. Take a look at their documentation https://in.mathworks.com/help/vision/ref/bboxoverlapratio.html
I wasn't able to understand your objective completely but I am sure this is the answer.

Asked:

on 2 Nov 2012

Answered:

on 20 Oct 2018

Community Treasure Hunt

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

Start Hunting!