简体   繁体   English

了解OpenCV C ++中的Rect

[英]Understanding Rect in OpenCV C++

I am trying to mark pixels inside a rectangle. 我正在尝试标记矩形内的像素。 The rectangle to be marked is decided in an if-else construct. 要标记的矩形在if-else结构中确定。 I am getting the following error on using rectangles which are defined in either of the if-else blocks: "Assertion failed (0 <= roi.x && 0 < = roi.width && roi.x + roi.width <=m.cols && 0 <=roi.height && roi.y +roi.height <= m.rows)" 使用在if-else任一块中定义的矩形时,出现以下错误:“断言失败(0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m。 cols && 0 <= roi.height && roi.y + roi.height <= m.rows)”

Here is the code snippet I am using: 这是我正在使用的代码片段:

if (faces.size() != 0){r = faces[0];}
if (eyes.size()!=0){r2 = eyes[0];}

markers(image.rows,image.cols)
if(faces.size() == 0){
    cout << "No Face found";
    rectangle_face = rectangle_old;
    rectangle_inner = rectangle_inner_old;
    rectangle_outer = rectangle_outer_old;
}
else {
    pt1.x = r.x;
    pt1.y = r.y;
    pt2.x = pt1.x + r.width;
    pt2.y = pt2.y + r.height;

    pt1_inner.x = r.x + (r.width)/3;
    pt1_inner.y = r.y + (r.height)/3;
    pt1_outer.x = pt1.x;
    pt1_outer.y = pt1.y;

    pt2_inner.x = pt2.x - (r.width)/3;
    pt2_inner.y = pt2.y - (r.height)/3;
    pt2_outer.x = pt2.x;
    pt2_outer.y = image.rows;

    rectangle_face = r; 
    rectangle_inner = Rect(pt1_inner,pt2_inner); 
    rectangle_outer = Rect(pt2_outer,pt2_outer);
}


//rectangle_inner = Rect(pt1_inner,pt2_inner); 
//rectangle_outer = Rect(pt2_outer,pt2_outer);

rectangle_old = rectangle_face;
rectangle_outer_old = rectangle_outer;
rectangle_inner_old = rectangle_inner;



// Setting all pixels to possible background first
markers.setTo(cv::GC_PR_BGD);

//It get stuck at the following two lines
cv::Mat1b fg_seed_inside_face = markers(rectangle_inner);
//Marking pixels in order. Note: Order is important here.
cv::Mat1b Prfg_seed_FaceExtended = markers(rectangle_outer);

'faces' is vector of rectangles returned from the detectMultiScale. 'faces'是detectMultiScale返回的矩形的向量。 What baffles me is that if I declare the rectangle_inner and rectangle_outer outside of the if-else blocks (as is commented out in the code-snippet, right below the else condition), the code works fine for the case where the 'if' condition is not true. 令我感到困惑的是,如果我在if-else块之外声明长方体(inner)和长方体(outer_outer)(如代码段中else条件下面的注释所示),则该代码在'if'条件的情况下可以正常工作是不正确的。 So basically I am expected to declare my rectangle_inner and rectangle_outer outside the if-else blocks, which doesn't make sense. 因此,基本上我应该在if-else块外声明我的rectangle_inner和rectangular_outer,这没有意义。 Is there a work-around? 有解决方法吗?

When you compute the (x,y) coordinates of your inner and outer points in order to build your rectangles, you have to check that the coordinates you get by adding or subtracting to pt1 or pt2 r.weight or r.height don't get out of the image. 在计算内部点和外部点的(x,y)坐标以构建矩形时,必须检查通过添加或减去pt1或pt2所获得的坐标,r.weight或r.height不摆脱图像。 This is a fairly common occurrence when your face is close to the edge of your image. 当您的脸靠近图像边缘时,这是相当常见的情况。

It can be done easily by doing something like 通过执行类似的操作可以轻松完成

pt1.x = r.x;
pt1.y = r.y;
pt2.x = pt1.x + r.width;
if(pt2.x >= image.cols) //check that coordinate pt2.x doesn't get out of the image
    pt2.x = image.cols;

pt2.y = pt2.y + r.height;
if(pt2.y >= image.rows) //check that coordinate pt2.y doesn't get out of the image
    pt2.y = image.rows;

And repeat the check for each coordinate you compute. 并对您计算的每个坐标重复检查。 When you subtract just check that coordinates are >= 0 and set them to 0 if they aren't. 当您减去时,只需检查坐标> = 0,然后将其设置为0即可。

Hope it helps.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM