OpenCV (C++) 颜色跟随

  1 #include <opencv2/opencv.hpp>
  2 #include <iostream>
  3 #include <math.h>
  4 
  5 using namespace cv;
  6 using namespace std;
  7 
  8 const char* input_title = "Input Image";
  9 const char* Contours = "Contours Image";
 10 const char* output_title = "Output Image";
 11 Mat src,src_gray;
 12 int threshold_value = 50;
 13 Rect bbox;
 14 void FindROI(int, void*);
 15 void Check_Skew(int, void*);
 16 
 17 int main(int argc, char** argv) {
 18     src = imread("F:/火狐下载/timg.jpg");
 19     if (src.empty()) {
 20         printf("could not find image");
 21         return -1;
 22     }
 23     namedWindow(input_title, WINDOW_AUTOSIZE);
 24     imshow(input_title, src);
 25     Check_Skew(0, 0);
 26     /*namedWindow(Contours, WINDOW_AUTOSIZE);    
 27     createTrackbar("threshold", Contours, &threshold_value, 255, FindROI);
 28     FindROI(0, 0);*///直接切边
 29 
 30     waitKey(0);
 31     return 0;
 32 }
 33 
 34 void Check_Skew(int, void*) {
 35     cvtColor(src, src_gray, COLOR_BGR2BGRA);
 36     Mat canny_output;
 37     Canny(src_gray, canny_output, threshold_value, threshold_value * 2, 3, false);
 38 
 39     vector<vector<Point>> contours;
 40     vector<Vec4i> hierachy;
 41     findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
 42     Mat DrawImage = Mat::zeros(src.size(), CV_8UC3);
 43     float maxw = 0;
 44     float maxh = 0;
 45     double degree;
 46     for (size_t t = 0; t < contours.size(); t++) {
 47         RotatedRect minRect = minAreaRect(contours[t]);//取得外界矩形
 48         degree =abs( minRect.angle);
 49         if (degree > 0) {
 50             maxw = max(maxw, minRect.size.width);
 51             maxh = max(maxh, minRect.size.height);
 52         }
 53     }
 54     RNG rng(12345);
 55     for (size_t t = 0; t<contours.size(); t++) {
 56         RotatedRect minRect = minAreaRect(contours[t]);//取得外界矩形
 57         float degree = abs(minRect.angle);
 58         printf("current angle is :%f\n", degree);
 59         if (minRect.size.width==maxw && minRect.size.height==maxh) {
 60             Point2f pts[4];
 61             minRect.points(pts);
 62             //bbox = minRect.boundingRect();
 63             Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
 64             for (int i = 0; i < 4; i++) {
 65                 line(DrawImage, pts[i], pts[(i + 1) % 4], color, 2, 8, 0);
 66             }
 67 
 68         }
 69     }
 70     imshow(Contours, DrawImage);
 71     Point2f center(src.cols / 2, src.rows / 2);
 72     Mat rotm = getRotationMatrix2D(center, degree, 1.0);
 73     Mat dst;
 74     warpAffine(src, dst, rotm, src.size(), INTER_LINEAR, 0, Scalar(255, 255, 255));
 75     imshow("correct", dst);
 76 }
 77 
 78 
 79 void FindROI(int, void*) {
 80     cvtColor(src, src_gray, COLOR_BGR2BGRA);
 81     Mat canny_output;
 82     Canny(src_gray, canny_output, threshold_value, threshold_value * 2, 3, false);
 83 
 84     vector<vector<Point>> contours;
 85     vector<Vec4i> hierachy;
 86     findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0));
 87 
 88     int minw = src.cols*0.75;
 89     int minh = src.rows*0.75;
 90     RNG rng(12345);
 91     Mat DrawImage = Mat::zeros(src.size(), CV_8UC1);
 92     for(size_t t=0; t<contours.size();t++){
 93         RotatedRect minRect = minAreaRect(contours[t]);//取得外界矩形
 94         float degree = abs(minRect.angle);
 95         printf("current angle is :%f\n", degree);
 96         if (minRect.size.width > minw && minRect.size.height > minh && minRect.size.width < (src.cols - 5)) {
 97             Point2f pts[4];
 98             minRect.points(pts);
 99             bbox = minRect.boundingRect();
100             Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
101             for (int i = 0; i < 4; i++) {
102                 line(DrawImage, pts[i], pts[(i + 1)%4], color, 2, 8, 0);
103             }
104 
105         }
106     }
107     imshow(Contours, DrawImage);
108     if (bbox.height>0) {
109         Mat ROIImage = src(bbox);//用bbox的范围截取src
110         imshow(output_title, ROIImage);
111     }
112     return;
113 }

猜你喜欢

转载自www.cnblogs.com/long5683/p/9290147.html