Home > Software engineering >  How do OpenCV from left to right, from top to bottom of sequential search profile
How do OpenCV from left to right, from top to bottom of sequential search profile

Time:09-16

O great god teach findContours () implementation

CodePudding user response:

Fyi:
 # include "opencv2/highgui/highgui. HPP" 
# include "opencv2/imgproc/imgproc HPP"
# include "opencv2/imgproc/imgproc_c. H"
using namespace std;
using namespace cv;
Mat img, smallImg, gray, bw;
VectorVector Contours.
Int threshval=128;
The Rect r;
The Rect maxrect brect;
Int independence idx, n;
Const static Scalar colors [15]={
CV_RGB (0, 0128),
CV_RGB (0128, 0),
CV_RGB (0128128),
CV_RGB (128, 0, 0),
CV_RGB (128, 0128),
CV_RGB (128128, 0),
CV_RGB (128128128),
CV_RGB (160160160),
CV_RGB (0, 0255),
CV_RGB (0255, 0),
CV_RGB (0255255),
CV_RGB (255, 0, 0),
CV_RGB (255, 0255),
CV_RGB (255255, 0),
CV_RGB (255255255),
};
Scalar color;
Void gamma_correct (Mat& Img, Mat& DST, double gamma) {
Mat temp.
CvMat TMP.

Img. ConvertTo (temp, CV_32FC1, 1.0/255.0, 0.0);
TMP=temp;
CvPow (& amp; TMP, & amp; TMP, gamma);
Temp. ConvertTo (DST, CV_8UC1, 255.0, 0.0);
}
Int main () {
CvNamedWindow (" display ", 1);
Img=imread (" image. JPG ", 1);
R.x=img. Cols/10;
R.y=img. Rows/3;
Truly idth=img. Cols * 8/10;
R.h. eight=img. Rows * 2/3;
SmallImg=img (r);
CvtColor (smallImg, gray, CV_BGR2GRAY);
//medianBlur (gray, gray, 5);
EqualizeHist (gray, gray);
Gamma_correct (gray, gray, 4.0);
Imshow (" display ", gray);
waitKey(0);

Bw=(gray> Threshval);
Imshow (" display ", bw);
waitKey(0);

Mat Structure0=getStructuringElement (MORPH_RECT, Size (3, 3));
Erode (bw, bw, Structure0, Point (1, 1));
Mat Structure1=getStructuringElement (MORPH_RECT, Size (6, 6));
Dilate (bw, bw, Structure1, Point (1, 1));
Imshow (" display ", bw);
waitKey(0);

FindContours (bw, contours, hierarchy, RETR_EXTERNAL CHAIN_APPROX_SIMPLE);
if (! Contours. The empty () & amp; & ! Hierarchy. The empty ()) {
Independence idx=0;
n=0;
Vector Approx.
For (; Idx>=0; Independence idx=hierarchy [independence idx] [0]) {
Color=colors [15] independence idx %;
//drawContours (smallImg, contours, independence idx, color, and 1, 8, hierarchy);
ApproxPolyDP (Mat (contours [independence idx]), approx, arcLength (Mat (contours [independence idx]), true) * 0.005, true);//0.005 will flash straightening coefficient
Const Point * p=& amp; Approx [0].
Int m=(int) approx. The size ();
Polylines (smallImg, & amp; P, & amp; M, 1, true, color);
Circle (smallImg, Point (p [0]. X, p [0]. Y), 3, color);
Circle (smallImg, Point (p [1]. X, p [1]. Y), 2, color);
For (int I=2; In++;
If (1==n) {
Maxrect=boundingRect (Mat (contours [independence idx]));
} else {
Brect=boundingRect (Mat (contours [independence idx]));
CvRect Mr (maxrect), br (brect);
Maxrect=cvMaxRect (& amp; Mr, & amp; Br);
}
}
Circle (smallImg, Point (maxrect. X + maxrect. Width/2, maxrect. Y + maxrect. Height/2), 2, CV_RGB (0, 255));
}
Imshow (" display ", smallImg);
waitKey(0);
CvDestroyWindow (" display ");
return 0;
}

CodePudding user response:

Find, after their sort

CodePudding user response:

reference 1/f, zhao teacher reply:
are for reference only:
 # include "opencv2/highgui/highgui. HPP" 
# include "opencv2/imgproc/imgproc HPP"
# include "opencv2/imgproc/imgproc_c. H"
using namespace std;
using namespace cv;
Mat img, smallImg, gray, bw;
VectorVector Contours.
Int threshval=128;
The Rect r;
The Rect maxrect brect;
Int independence idx, n;
Const static Scalar colors [15]={
CV_RGB (0, 0128),
CV_RGB (0128, 0),
CV_RGB (0128128),
CV_RGB (128, 0, 0),
CV_RGB (128, 0128),
CV_RGB (128128, 0),
CV_RGB (128128128),
CV_RGB (160160160),
CV_RGB (0, 0255),
CV_RGB (0255, 0),
CV_RGB (0255255),
CV_RGB (255, 0, 0),
CV_RGB (255, 0255),
CV_RGB (255255, 0),
CV_RGB (255255255),
};
Scalar color;
Void gamma_correct (Mat& Img, Mat& DST, double gamma) {
Mat temp.
CvMat TMP.

Img. ConvertTo (temp, CV_32FC1, 1.0/255.0, 0.0);
TMP=temp;
CvPow (& amp; TMP, & amp; TMP, gamma);
Temp. ConvertTo (DST, CV_8UC1, 255.0, 0.0);
}
Int main () {
CvNamedWindow (" display ", 1);
Img=imread (" image. JPG ", 1);
R.x=img. Cols/10;
R.y=img. Rows/3;
Truly idth=img. Cols * 8/10;
R.h. eight=img. Rows * 2/3;
SmallImg=img (r);
CvtColor (smallImg, gray, CV_BGR2GRAY);
//medianBlur (gray, gray, 5);
EqualizeHist (gray, gray);
Gamma_correct (gray, gray, 4.0);
Imshow (" display ", gray);
waitKey(0);

Bw=(gray> Threshval);
Imshow (" display ", bw);
waitKey(0);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related