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 rect Step A in code
Opencv rect Step B in code
Opencv rect Step C in code
Opencv rect Step D in code
Opencv rect Step E in code
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, RectangleToDraw.tl(), RectangleToDraw.br(),Scalar(0, 0, 255), 2, 8, 0);imshow("DrawRectangle", Picture);int key4 = waitKey(2000);// saveimwrite("1.jpg", Picture);//B Rectangle defined by 2 pointsPoint A(10, 10);Point B(100, 100);Rect RectangleToDraw2(A, B);rectangle(Picture, RectangleToDraw2.tl(), RectangleToDraw2.br(),Scalar(0, 255, 255), 1, 8, 0);imwrite("2.jpg", Picture);//C x=100, y=100, w=300, h=300Rect 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 0Rect 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 lineRect 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 typerectangle(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;}
its good idea but the code on first kline doesnt accepted in a machine what that............
Code is correct and works. I generate all the picture here by this code. Maybe the minor mistakes should be here. But works