Ask Your Question
0

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

asked 2016-01-14 18:26:38 -0600

tenkeason gravatar image

updated 2016-01-15 20:34:04 -0600

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

image description

main,cpp : http://paste.ofcode.org/36TG5U8WTViFN...

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

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-01-14 18:53:20 -0600

Tetragramm gravatar image

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.

edit flag offensive delete link more

Comments

may i know what should i write in condition if?

if ( what should i type here )

{

112 ... 132

}

tenkeason gravatar imagetenkeason ( 2016-01-14 19:29:46 -0600 )edit
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?

Tetragramm gravatar imageTetragramm ( 2016-01-14 20:10:07 -0600 )edit

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

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-15 03:54:17 -0600 )edit

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 ]) );

tenkeason gravatar imagetenkeason ( 2016-01-15 09:18:54 -0600 )edit

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.

Tetragramm gravatar imageTetragramm ( 2016-01-15 16:54:20 -0600 )edit

this is my code, is that true?

http://paste.ofcode.org/w3cZgTE7kkyM9...

tenkeason gravatar imagetenkeason ( 2016-01-15 20:43:56 -0600 )edit

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.

Tetragramm gravatar imageTetragramm ( 2016-01-15 21:10:14 -0600 )edit

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

tenkeason gravatar imagetenkeason ( 2016-01-15 21:59:40 -0600 )edit

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.

Tetragramm gravatar imageTetragramm ( 2016-01-15 22:18:33 -0600 )edit

oh i see, thanks for reminding.

tenkeason gravatar imagetenkeason ( 2016-01-15 22:38:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-14 18:26:38 -0600

Seen: 3,102 times

Last updated: Jan 15 '16