1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std;
#define WINDOW_NAME1 "Original" #define WINDOW_NAME2 "Result"
Mat g_srcImage; Mat g_grayImage; int g_nThresh = 50; int g_maxThresh = 255; RNG g_rng(12345); Mat srcImage_copy = g_srcImage.clone(); Mat g_thresholdImage_output; vector<vector<Point>> g_vContours; vector<Vec4i> g_vHierarchy;
void on_ThreshChange(int, void *);
int main() { g_srcImage = imread("1.jpg", 1);
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY); blur(g_grayImage, g_grayImage, Size(3, 3));
namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); imshow(WINDOW_NAME1, g_srcImage);
createTrackbar(" 阈值:", WINDOW_NAME1, &g_nThresh, g_maxThresh, on_ThreshChange); on_ThreshChange(0, 0);
waitKey(0); return (0); }
void on_ThreshChange(int, void *) { threshold(g_grayImage, g_thresholdImage_output, g_nThresh, 255, THRESH_BINARY);
findContours(g_thresholdImage_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point>> hull(g_vContours.size()); for (unsigned int i = 0; i < g_vContours.size(); i++) { convexHull(Mat(g_vContours[ i ]), hull[ i ], false); }
Mat drawing = Mat::zeros(g_thresholdImage_output.size(), CV_8UC3); for (unsigned int i = 0; i < g_vContours.size(); i++) { Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); drawContours(drawing, g_vContours, i, color, 1, 8, vector<Vec4i>(), 0, Point()); drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point()); }
imshow(WINDOW_NAME2, drawing); }
|