# How to draw a rectangle contours only if largest than specific size

This is i get just now, it will detect and draw rectangle at the 2 largest object, the problem now is it will draw a rectangle when the object is small than the size i want but is the first/second largest in the camera.

Requirement : It will draw rectangles at the 1st & 2nd largest object only if when the object largest than 200 * 300 (frame size : 640*480). If the 1st & 2nd object is small than the minimum size so it will not be draw.

may i know what should i write in condition if?

if ( what should i type here )

{

112 ... 132 line

}

The codes are in below link main,cpp : http://paste.ofcode.org/36TG5U8WTViFN...

Color.h : http://paste.ofcode.org/WDy2xPwYnaZxL...

Color.cpp : http://paste.ofcode.org/c4yeiyC2tv2t8...

edit retag close merge delete

Sort by » oldest newest most voted Well, at lines 112 and 132 you have the bounding rectangle. At those points, add an if statement that only returns true if that rectangle is larger than 200*300, and wraps the parts where it does the drawing.

One thing to note. The criteria you are using to sort is the length of the contour. The largest contour may be the largest, but be less than the threshold size, while the second largest may be larger than it. I think. I honestly haven't used contours much.

more

may i know what should i write in condition if?

if ( what should i type here )

{

112 ... 132

}

2

I'm reluctant to just tell you, since you'll learn better if you figure it out yourself. But you have a Rect, with the functions and variables you can see here: http://docs.opencv.org/master/d2/d44/classcv_1_1Rect__.html#gsc.tab=0

If you want a minimum width of 200 and a minimum height of 300, what sort of if statement would that look like?

A small hint, look at a combination of the x y width and height attributes of the rectangle ;)

Sorry, i'm just start learn opencv few month and i still confuse about the code below. Would you help me explain about the below code please?

Rect minRect = boundingRect( Mat(contours[ largestIndex ]) );

What that does, is find the rectangle that completely contains the contour. So, in your example picture, no part of the larger contour is farther left than x=74, farther up than y=323, farther right than x=337, or farther down than y=479.

The Rectangle is described by four number, x and y, which are the x and y pixels of the top left corner ((74,323) for the example image), and the width and height of the rectangle.

That almost works. First, that is only one of your rectangles. You'll need to add the second one inside a second if statement.

Secondly, when do you show the image? Make sure it happens when you actually want it to be shown.

Lastly, double check the width and height. They aren't the same numbers you used here. Perhaps you meant to do that, perhaps it's a typo.

May i know what means the width and height aren't the same numbers used here?

Well, your if statement uses 100300, when your example was 200300. That's all. I wasn't sure if you meant to do that, or if it was a typo.

Official site

GitHub

Wiki

Documentation