1#include <opencv2/opencv.hpp>2#include <iostream>3#include <vector>4 5 using namespaceCV;6 using namespacestd;7 8 intg_nminthred = -, g_nmaxthred =255;9 intG_nthick =0;Ten One intMain () A { -Mat srcimage = Imread ("group.jpg"); - theNamedwindow (""Original"",0); -Imshow (""Original"", srcimage); - -Createtrackbar ("minthred",""Original"", &g_nminthred,255,0); +Createtrackbar ("maxthred",""Original"", &g_nmaxthred,255,0); -Createtrackbar ("Thick",""Original"", &g_nthick, -,0); + A //first, the image is converted into space (in order to extract the binary image later) at Mat grayimage; - Cvtcolor (Srcimage, Grayimage, Cv_bgr2gray); - //filtering the image to achieve better results -Gaussianblur (Grayimage, Grayimage, Size (3,3),0,0); -Imshow (""Filtered image"", grayimage); - in Charkey; - while(1) to { + //obtaining binary images by Edge detection - Mat cannyimage; theCanny (Grayimage, Cannyimage, g_nminthred, g_nmaxthred,3); * $ //extracting outlines in a binary imagePanax NotoginsengVector<vector<point>>contours; -Vector<vec4i>hierarchy; theFindcontours (cannyimage, contours, hierarchy, Retr_tree, Chain_approx_simple, point (0,0)); + A //find the approximate polygon for each contour point set theVector<vector<point>>Approxpoint (Contours.size ()); + for(inti =0; I < (int) Contours.size (); i++) - { $ //The four vertex coordinates of the rectangle are obtained from the rectpoint variable. $Rotatedrect Rectpoint =Minarearect (Contours[i]); - //define a variable that stores the coordinates of the above four points -POINT2F fourpoint2f[4]; the //Place the coordinate values stored in the Rectpoint variable into an array of Fourpoint - rectpoint.points (fourpoint2f);Wuyi the //draws a rectangle based on the coordinates of the resulting four points - for(inti =0; I <3; i++) Wu { -Line (Cannyimage, Fourpoint2f[i], Fourpoint2f[i +1] About, Scalar (255,255,255), G_nthick); $ } -Line (Cannyimage, fourpoint2f[0], fourpoint2f[3] -, Scalar (255,255,255), G_nthick); - } A +Imshow (""Image after drawing"", cannyimage); the -Key = Waitkey (1); $ if(Key = = -) the Break; the } the the return 0; -}
-
Top
-
0
-
Step
Opencv3 the application of the minimum bounding rectangle in the image-scroll bar