OpenCV image recognition from zero to proficient----dot-line round rectangles and mouse events

Source: Internet
Author: User
Tags scalar

The elements in the image are points, lines, circles, ellipses, rectangles, and polygons, which are also the characteristics of the object, which is necessary in the image recognition. So first to know how this element is defined and used, while the mouse is the window of the computer, we have a lot of processing will use the mouse. This article mainly has the following three parts:

(1) Basic application of point, line, Circle, ellipse and Rectangle

(2) advanced application of point, line, Circle, ellipse and Rectangle

(3) Mouse events

Basic application of point, line, Circle, ellipse and Rectangle

function for drawing points:

Point A = Point (600,600);

literal function puttext () function

void Puttext (cvarr* img, const char* text, Cvpoint org, const cvfont* font,cvscalar color); IMG: Input Image text: The string to be displayed org: the coordinate of the lower-left corner of the first letter font: Pointer to the font structure COLOR:[1]  The color of the text.
Draw a line

Cv_exports_w void Line (Cv_in_out mat&img, point pt1, point pt2, const scalar& color,                     int Thickness=1, intlinet ype=8, int shift=0);

    • IMG: The image to draw the segment.
    • PT1: The beginning of the line segment.
    • PT2: The end point of the segment.
    • Color: The colour of the segment, defined by a scalar object.
    • Thickness: The width of the line.
    • Linetype: The type of the segment. Values 8, 4, and Cv_aa represent 8 adjacent connectors, 4 adjacent connectors, and anti-aliasing cables. The default value is 8 adjacency. For better results, CV_AA (Gaussian filtering) can be chosen.
    • Shift: The number of decimal places in the coordinate point.

Draw Ellipse

Cv_exports_w void Ellipse (cv_in_outmat& img, point Center, Size axes,                        double angle, doublestartangle, double ENDA Ngle,                        const scalar&color, int thickness=1,                        int linetype=8, intshift=0);

    • IMG: An image to draw an ellipse.
    • Center: The coordinate of the ellipse centre point.
    • Axes: The ellipse is within the rectangle determined by the size. (That is, define the long and short axes).
    • Angle: elliptical rotation angle.
    • StartAngle: The angle at which the ellipse starts drawing.
    • Endangle: The angle at which the ellipse draws at the end. (If a complete ellipse is drawn, startangle=0, Endangle = 360).
    • Color: The colors of the ellipse.
    • Thickness: Draws the oval line thickness. A negative number indicates full padding.
    • Linetype,shift: Ibid.

Draw a rectangle

cv_exports_w void Rectangle (cv_in_outmat& img, point pt1, point pt2,                          const scalar&color, int Thickness=1,
   int linetype=8, intshift=0);

    • PT1: The coordinate of the upper-left corner of the rectangle.
    • PT2: The coordinate of the lower-right corner of the matrix.

Draw a Circle

Cv_exports_w void Circle (cv_in_out mat&img, point Center, int radius,                       const scalar& Color,int thickness=1,< C6/>int linetype=8, int shift=0);

    • Center: Center coordinate.
    • Radius: Radius.
    • The remaining ibid.

Fill polygon

cv_exports void Fillpoly (mat& img,const point** pts,                         const int* npts, intncontours,                         const Scalar&color, int linetype=8, int shift=0, point                         offset=point ());

PTS: A polygon fixed-point set.

NPTs: The number of vertices of the polygon.

Ncontours: The number of polygons to draw.

Offset: An optional offset for all point outlines.

#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp>using namespace Cv;int main () {Mat SRC (500,500,cv_8uc3,scalar (0,0,255));  System ("Color 5F");
String words= "A simple example";  Puttext (SRC, words, point (SRC.ROWS/2,SRC.COLS/4), Cv_font_hershey_complex, 1, Scalar (255, 0, 0));  Namedwindow ("Display word"); Imshow ("Display word", SRC);  Point Center = point (300,200);   int r = N;  Circle (Src,center,r,scalar (0,0,0));   Ellipse (src,center,size (+), 0,30,360,scalar (0,0,0));   Point A = Point (600,600);  Line (Src,a,center,scalar (255,0,0));  Rectangle (Src,a,center,scalar (255,0,0)); Imshow ("baseplate", SRC); Waitkey (0); return 0;}


Advanced application of point, line, Circle, ellipse and Rectangle

#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp>using namespace CV; #include < opencv2/imgproc/imgproc.hpp> #define WINDOW_NAME1 "" Draw figure 1 "//Macro defined for window caption #define WINDOW_NAME2" "Draw Figure 2"//For Window The caption defines the macro #define Window_width 600//defines the window size of the macro void DrawEllipse (Mat img, double angle);//Draw ellipse void Drawfilledcircle (Mat img, P Oint center);//Draw circle void DrawPolygon (Mat img);//Draw polygon void DrawLine (Mat img, point start, point end)//Draw line segment int main (voi d) {Mat atomimage = Mat::zeros (Window_width, Window_width, CV_8UC3); Mat rookimage = Mat::zeros (Window_width, Window_width, CV_8UC3);D rawellipse (Atomimage,;D rawellipse (atomImage, 0) ;D rawellipse (atomimage);D rawellipse (Atomimage, -45);D rawfilledcircle (Atomimage, point (WINDOW_WIDTH/2, WINDOW_ WIDTH/2));D Rawpolygon (rookimage); Rectangle (rookimage,point (0, 7*WINDOW_WIDTH/8), point (Window_width, window_width ), Scalar (0, 255, 255), -1,8);D rawline (rookimage, point (0, 15*WINDOW_WIDTH/16), point ( Window_width, 15*WINDOW_WIDTH/16));D rawline (Rookimage, point (WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8), point (WINDOW_WIDTH  /4, window_width));D rawline (Rookimage, point (WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8), point (WINDOW_WIDTH/2, window_width) );D Rawline (Rookimage, point (3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8), point (3*WINDOW_WIDTH/4, window_width)), Imshow (WIN Dow_name1, Atomimage); MoveWindow (window_name1, 0, N); Imshow (window_name2, rookimage); MoveWindow (WINDOW_NAME2, WIN Dow_width, Waitkey (0); return (0);} void DrawEllipse (Mat img, double angle) {int thickness = 2;int Linetype = 8;ellipse (Img,point (WINDOW_WIDTH/2, Window_wi DTH/2), Size (WINDOW_WIDTH/4, WINDOW_WIDTH/16), Angle,0,360,scalar (255, 129, 0), thickness,linetype);} void Drawfilledcircle (Mat img, point center) {int thickness = -1;int Linetype = 8;circle (img,center,window_width/32,scal AR (0, 0, 255), thickness,linetype);} void DrawPolygon (Mat img) {int linetype = 8;//Create some point points rookpoints[1][20];rookpoints[0][0] = Point (WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8); rookpoints[0][1] = Point (3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8) ; rookpoints[0][2] = Point (3*WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16); rookpoints[0][3] = Point (11*WINDOW_WIDTH/16, 13*WIN DOW_WIDTH/16); rookpoints[0][4] = Point (19*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8); rookpoints[0][5] = Point (3*WINDOW_WID TH/4, 3*WINDOW_WIDTH/8); rookpoints[0][6] = Point (3*WINDOW_WIDTH/4, WINDOW_WIDTH/8); rookpoints[0][7] = Point (2   6*WINDOW_WIDTH/40, WINDOW_WIDTH/8); rookpoints[0][8] = Point (26*WINDOW_WIDTH/40, WINDOW_WIDTH/4); RookPoints[0][9] = Point (22*window_width/40, WINDOW_WIDTH/4); rookpoints[0][10] = Point (22*WINDOW_WIDTH/40, WINDOW_WIDTH/8); rook POINTS[0][11] = Point (18*WINDOW_WIDTH/40, WINDOW_WIDTH/8); rookpoints[0][12] = Point (18*WINDOW_WIDTH/40, WINDOW_WI    DTH/4); rookpoints[0][13] = Point (14*WINDOW_WIDTH/40, WINDOW_WIDTH/4); rookpoints[0][14] = Point (14*WINDOW_WIDTH/40, WINDOW_WIDTH/8); rookPOINTS[0][15] = Point (WINDOW_WIDTH/4, WINDOW_WIDTH/8); rookpoints[0][16] = Point (WINDOW_WIDTH/4, 3*window_wi  DTH/8); rookpoints[0][17] = Point (13*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8); rookpoints[0][18] = Point (5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16); rookpoints[0][19] = Point (WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16); const point* Ppt[1] = {RookPoin Ts[0]};int npt[] = {};fillpoly (img,ppt,npt,1,scalar (255, 255, 255), linetype);} void DrawLine (Mat img, point-Start, point end) {int thickness = 2;int Linetype = 8;line (img,start,end,scalar (0, 0, 0), Thickness,linetype);}



This cat must be a very caring blogger painting, it is like, so recommend it, of course, the example given, the picture is, but the above cat more concise let people absorb the block, the official example is too long


Third, mouse events

The Setmousecallback () function explains:

function prototypes for void Setmousecallback (conststring& winname, Mousecallback onmouse, void* userdata = 0);

The explanation of this function is shown below:


#include <opencv2/opencv.hpp>using namespace CV; #define WINDOW_NAME "" program Window "void On_mousehandle (int event, int x, int y, int flags, void* param), void DrawRectangle (cv::mat& img, cv::rect box); Rect G_rectangle;bool G_bdrawingbox = false;//whether to draw rng g_rng (12345); int main (int argc, char** argv) {System ("color 9F") ; G_rectangle = Rect ( -1,-1,0,0); Mat Srcimage (800,CV_8UC3), Tempimage;srcimage.copyto (tempimage); g_rectangle = Rect ( -1,-1,0,0); srcimage = Scalar:        : All (0); Namedwindow (Window_name); Setmousecallback (Window_name,on_mousehandle, (void*) &srcimage); while (1) {Srcimage.copyto (tempImage); if (g_ Bdrawingbox) DrawRectangle (Tempimage, G_rectangle); Imshow (Window_name, tempimage); if (waitkey () = =) break; return 0;} void On_mousehandle (int event, int x, int y, int flags, void* param) {mat& image = * (cv::mat*) Param;switch (event) {CAs E Event_mousemove: {if (g_bdrawingbox) {g_rectangle.width = X-g_rectangle.x;g_rectangle.height = Y-g_rectangle.y;}} Break;//left Button Press the message case Event_lbuttondown: {g_bdrawingbox = True;g_rectangle =rect (x, y, 0, 0);//record start point}break;//left button lift message case EVE Nt_lbuttonup: {g_bdrawingbox = false;//identifier is false//to width and height less than 0 of processing if (G_rectangle.width < 0) {g_rectangle.x + = G_ Rectangle.width;g_rectangle.width *=-1;} if (G_rectangle.height < 0) {g_rectangle.y + = G_rectangle.height;g_rectangle.height *=-1;} Call function to draw DrawRectangle (image, G_rectangle);} Break;}} void DrawRectangle (cv::mat& img, cv::rect box) {Cv::rectangle (img,box.tl (), box.br (), Cv::scalar (g_rng.uniform (0, 255), G_rng.uniform (0,255), G_rng.uniform (0,255)));//Random color}




OpenCV image recognition from zero to proficient----dot-line round rectangles and mouse events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.