20

I want to draw a rectangle in OpenCV by using this function:

rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

But when I use it I am facing some errors. My question is: can anyone explain the function with an example? I found some examples but with another function:

rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

This example on the second function:

rectangle(image, pt2, pt1, Scalar(0, 255, 0), 2, 8, 0);

This function I understand, but with the first function I'm facing a problem in parameter Rect. I don't know how I can deadlier it?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Adel Khatem
  • 319
  • 1
  • 2
  • 8

2 Answers2

24

The cv::rectangle function that accepts two cv::Point's takes both the top left and the bottom right corner of a rectangle (pt1 and pt2 respectively in the documentation). If that rectangle is used with the cv::rectangle function that accepts a cv::Rect, then you will get the same result.

// just some valid rectangle arguments
int x = 0;
int y = 0;
int width = 10;
int height = 20;
// our rectangle...
cv::Rect rect(x, y, width, height);
// and its top left corner...
cv::Point pt1(x, y);
// and its bottom right corner.
cv::Point pt2(x + width, y + height);
// These two calls...
cv::rectangle(img, pt1, pt2, cv::Scalar(0, 255, 0));
// essentially do the same thing
cv::rectangle(img, rect, cv::Scalar(0, 255, 0))
Robert Prévost
  • 1,667
  • 16
  • 22
  • I did as you gave me the example but the function didn't draw the rectangle on the image but was drawn at the corner. do you want me to join my code to take a look – Adel Khatem Oct 19 '16 at 03:45
  • As it stands above, the top left corner of the rectangle is the top left corner of the image. Maybe you can somehow post the image you are using or show what's going wrong? – Robert Prévost Oct 19 '16 at 03:47
  • /* Global variables */ CascadeClassifier pedestrians_cascade; Mat image1; int x = 0; int y = 0; int w = 10; int h = 20; int main( ) { // Load Face cascade (.xml file) pedestrians_cascade = CascadeClassifier("hogcascade_pedestrians.xml"); // Read an images image5 = imread("ped5.jpg"); Rect rect(x,y,w,h); std::vector faces1; pedestrians_cascade.detectMultiScale( image1, faces1, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) ); rectangle(image1, rect, cv::Scalar(0, 255, 0)); imshow( "Window1", image1); waitKey(0); return 0; } – Adel Khatem Oct 19 '16 at 03:50
  • Yeah, it looks fine, but those values for your rectangle are probably not what you want. Try playing around with the values for x, y, width, and height. – Robert Prévost Oct 19 '16 at 03:54
  • how I can get the values that I want – Adel Khatem Oct 19 '16 at 03:56
  • I guess something like `rectangle(image1, faces1[0], cv::Scalar(0, 255, 0));` would plot the first detection from `pedestrians_cascade` if there were any detections. – Robert Prévost Oct 19 '16 at 04:36
8

Here's a simple example of drawing a pre-defined rectangle on an image

using namespace cv;

int main(){
Mat img=imread("myImage.jpg");

Rect r=Rect(10,20,40,60);
//create a Rect with top-left vertex at (10,20), of width 40 and height 60 pixels.

rectangle(img,r,Scalar(255,0,0),1,8,0);
//draw the rect defined by r with line thickness 1 and Blue color

imwrite("myImageWithRect.jpg",img);


return 0;
}
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39