开发者

Changes in CvRect values not being reflected in image

The minimum bounding rectangle of a contour is returned by OpenCv as a CvRect structure. I'm trying to have an m pixel thick margin/border around the sub-image that corresponds to this CvRect on the original image, i.e. instead of the exact bounding rectangle of a component(contour) we get something larger containing the component and also its immediately surrounding pixels.

Taking care of the image bounds, I'm using the following code to increase size of the CvRect which is later applied with CvSetImageROI() to get the sub-image:

//making m px border around subimg
CvRect box_swt = box1;
box_swt.x = (box1.x)开发者_开发百科>m ? box1.x-m : box1.x;
box_swt.y = (box1.y)>m ? box1.y-m : box1.y;
box_swt.width = (box1.x + box1.width) < img->width-m ? box1.width+m : box1.width;
box_swt.height = (box1.y + box1.height) < img->height-m ? box1.height+m : box1.height;

Here, box1 stores the return from cvBoundingRect(ptr, 0) where ptr is a pointer to detected contours.

The problem is that the resulting sub-image is larger only in the upper and left borders, the others remain unchanged. I'm not getting a sub-image with a uniform m pixel thick border around all its sides. This happens for all cases, not just for those on the boundary. Is there any logical error?


When you are modifying the x and y of the rectangle you are not increasing it's size, just moving it, and then you increase the width and the height by just m. I think what you want is to increase it by 2m to have the desired effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜