Opencv rectangle drawing tutorial C++


My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • 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 definition and redtagle drawing function just show you various way 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 commends inside c++ code // just describe the Rect definition to define rectangle to draw inside the function rectangle. This function just draw the defined rectangle inside  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 way. This is not 2 much 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.. 
    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 now 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 type of the line. For me is this parameter always 8. I dont care about the dashed and solid line type.. The last is 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 argc, char** argv) { Mat Picture; Picture = imread("22.JPG"); resize(Picture, Picture, Size(800, 600)); //A Parameters x (start in x axes horizontal) y (start in vertical) // w (vertical lenght) h (Horizontal lenght) Rect RectangleToDraw(10, 10, 100, 100); rectangle(Picture,,, Scalar(0, 0, 255), 2, 8, 0); imshow("DrawRectangle", Picture); int key4 = waitKey(2000); // save imwrite("1.jpg", Picture); //B Rectangle defined by 2 points Point A(10, 10); Point B(100, 100); Rect RectangleToDraw2(A, B); rectangle(Picture,,, Scalar(0, 255, 255), 1, 8, 0); imwrite("2.jpg", Picture); //C x=100, y=100, w=300, h=300 Rect RectangleToDraw3(100,100,200,200); rectangle(Picture, RectangleToDraw3, Scalar(0, 250, 0), 2, 8, 0); imwrite("3.jpg", Picture); //D Scalar(255, 0, 0) Color parameter // Blue 255, Green 0, Red 0 Rect RectangleToDraw4(300, 300, 100, 100); rectangle(Picture, RectangleToDraw4, Scalar(255, 0, 0), 2, 8, 0); imwrite("4.jpg", Picture); //E 10 value, int is thickness of the line Rect RectangleToDraw5(300, 300, 100, 100); rectangle(Picture, RectangleToDraw5, Scalar(255, 0, 255), 10, 8, 0); imwrite("5.jpg", Picture); //F Rect defined inside drawinf function // 4 value is line type rectangle(Picture, Rect(400,400,50,50), Scalar(255, 255, 255), 2, 4, 0); imwrite("6.jpg", Picture); imshow("DrawRectangle",Picture); int key7 = waitKey(20); return 0; }


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


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