Search code examples
c++objective-crectangles

rectangle function use in c++ objective-c


Hi I have been trying to draw a second rectangle after the face detection rectangle, but the parameters x, y, h, w are confusing me. I tried different values inside my variables but I do not understand what they exactly mean. I know that x, y should move to the desired position and h, w, should re-size it.

- (void)processImage:(cv::Mat &)image {
Mat gray;
std::vector<cv::Rect> faces;
Scalar color = Scalar(0, 255, 0);
cvtColor(image, gray, COLOR_BGR2GRAY);
_faceDetector.detectMultiScale(gray, faces, 1.1, 2, 0, cv::Size(60, 60));
for (int i = 0; i < faces.size(); i++) {
    rectangle(image, faces[i], color, 1);

    // x, y should move the rectangle to the desired position
    int x = faces[i].x;

    int y = faces[i].y;
    x=x+y;
    y=y+x;
    // h, w should resize the rectangle
    int he = y+faces[i].height*0.1 ;
    int we = x+faces[i].width*0.1;

    rectangle(image, cv::Point (x, y), cv::Point (we, he), Scalar(255,0,255),2,8,0);

}
}

Can anyone provide me a schematics (graph) with the rectangle and place the values of x, y, h, w please. Thank you


Solution

  • x is X-position, y is Y-position. h and w stand for height and width, respectively.

    Consider the following rectangle:

    A --------- B
    |           |
    |           |
    C --------- D
    

    Its position (origin) is defined by point A, and the other corners are defined relative to A.

    A = (x, y)

    B = (x + width, y)

    C = (x, y + height)

    D = (x + width, y + height)


    You should probably also revisit

    x=x+y;
    y=y+x;
    

    Why would it make sense to add coordinates across different axes?

    Also, it is most likely problematic to be using a value of x that was overwritten on the previous line. Compared to the initial values, your computation comes out to

    finalX = x + y
    finalY = y + (x + y) // because you're using the modified value of x