Ask Your Question

Finding largest rectangles in matrix

asked 2018-11-20 07:20:57 -0500

ejeczmionek gravatar image

updated 2018-11-20 07:53:46 -0500

Hello :)
I'm new to opencv. Having 2d occupancy matrix (box status is occupied, free or unknown) is there an algorithm covering whole matrix with possibly largest status rectangles?
Thank you for your help


  • white box = free
  • red box = occupied
  • green boxes are expected answers (green boxes should touch, but I draw it this way for clarity) e.g. coordinates of down left and top right corner
    image description
edit retag flag offensive close merge delete



thanks for the image update !

hmmm, why do you expect rectangles here ? e.g. there is nothing seperating the small one top/left from the large one bottom/left and the one on top/right

i don't think, opencv has any kind of magic for this kind of "self-organization"

however, have a look at connectedComponents (it will work for the red boxes, bzut return all connected white space as a single blob)

berak gravatar imageberak ( 2018-11-20 07:56:13 -0500 )edit

I expect rectangles, because of the further processing to custom octree. Thank you for your answer :)

ejeczmionek gravatar imageejeczmionek ( 2018-11-20 08:10:54 -0500 )edit

ahh, ok. unfortunately, i think, you 'll have to do your own "partitioning", then.

berak gravatar imageberak ( 2018-11-20 08:12:26 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-20 17:21:51 -0500

nick_leo gravatar image

updated 2018-11-20 17:23:11 -0500


I'm not sure the blue matrix is really in the target image or you have used it as a position reference.Things are bit tricky if the blue matrix is also part of the image.But you can try following approach,

  1. Use an edge detector in the gray scale image(Laplacian is preferred)
  2. Apply OpenCV contour analysis contour
  3. Use boundingRect to find rectangle with the maximum size.
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-11-20 07:20:57 -0500

Seen: 414 times

Last updated: Nov 20 '18