Opencv rectangle drawing tutorial C++

Opencv rectangle drawing tutorial by example in C++. Simple steps let you draw the rectangle inside the pictures and video sample. Several rectangle definitions and rectangle drawing functions show you various ways to draw the rectangles inside the picture and video. It is simple and easy. 

Steps in descriptions rectangle drawing 

All the steps below the picture (A B ... F) are also marked inside the code. The comments inside C++ code // just describe the Rect definition to define a rectangle to draw inside the function rectangle. This function just draws the defined rectangle inside the Picture.  Rectangle(Picture is the first parameter. This is just Mat where to put the second parameter. Defined Rect (rectangle). You can choose to put the rectangle to that function in several ways. This is not 2 very important. They just do the same job for you.. I am using just one of them. 

Try to figure out the steps and compare the code with images.. 

The third parameter is most likely the color of the Rectangle. Color is described by 3 numbers Scalar(B,G,R). B for Blue, G for green, and R for the last one. You know the color very well 

There is 3 more parameters. After the color, there is the thickness of the line. Use whatever you want. Another one is the type of line. For me is this parameter always 8. I don't care about the dashed and solid line types. The last is a shift. What the hell is that? I know but do not care. 

Try the code and compare the steps and code. Enjoy 

Opencv draw rectangle
Opencv rect Step A in code

Opencv draw rectangle
Opencv rect Step B in code

Opencv draw rectangle
Opencv rect Step C in code

Opencv draw rectangle
Opencv rect Step D in code

Opencv draw rectangle
Opencv rect Step E in code

Opencv draw rectangle
Opencv rect Step F in code

Opencv c++ tutorial draw Rectangle code

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argcchar** argv)
      Mat Picture; 
      Picture = imread("22.JPG");
      resize(Picture, Picture, Size(800600)); 
 //A Parameters x (start in x axes horizontal) y (start in vertical)
 // w (vertical lenght)  h (Horizontal lenght)
  Rect RectangleToDraw(1010100100);
Scalar(00255), 280);
  imshow("DrawRectangle", Picture);
  int key4 = waitKey(2000);
// save
  imwrite("1.jpg", Picture);
      //B Rectangle defined by 2 points
 Point A(1010);
  Point B(100100);
  Rect RectangleToDraw2(A, B);
Scalar(0255255), 180);
  imwrite("2.jpg", Picture);
      //C x=100, y=100, w=300, h=300
  Rect RectangleToDraw3(100,100,200,200);
  rectangle(Picture, RectangleToDraw3, Scalar(02500), 280);
  imwrite("3.jpg", Picture);
      //D Scalar(255, 0, 0) Color parameter 
      // Blue 255, Green 0, Red 0 
  Rect RectangleToDraw4(300300100100);
  rectangle(Picture, RectangleToDraw4, Scalar(25500), 280);
  imwrite("4.jpg", Picture);
               //E 10 value, int is thickness of the line 
  Rect RectangleToDraw5(300300100100);
  rectangle(Picture, RectangleToDraw5, Scalar(2550255), 1080);
  imwrite("5.jpg", Picture);
      //F Rect defined inside drawinf function
      // 4 value is line type
 rectangle(Picture, Rect(400,400,50,50), Scalar(255255255), 240);
  imwrite("6.jpg", Picture);
     int key7 = waitKey(20);
  return 0;

Next Post Previous Post
  • walter kessy
    walter kessy December 16, 2016 at 9:58 AM

    its good idea but the code on first kline doesnt accepted in a machine what that............

    • Vl
      Vl December 16, 2016 at 1:33 PM

      Code is correct and works. I generate all the picture here by this code. Maybe the minor mistakes should be here. But works

Add Comment
comment url