這是209.11.23的博客 下麵進入正題 Opencv的內容 各位都是大佬 HSV的顏色表和計算方法就不用說了吧 #include <opencv2/opencv.hpp> #include<iostream> #include<string> using namespace cv; using ...
這是209.11.23的博客
下麵進入正題 Opencv的內容
各位都是大佬
HSV的顏色表和計算方法就不用說了吧
#include <opencv2/opencv.hpp> #include<iostream> #include<string> using namespace cv; using namespace std; //輸入圖像 Mat img; //灰度值歸一化 Mat bgr; //HSV圖像 Mat hsv; //色相 string windowName = "src"; //輸出圖像的顯示視窗 string dstName = "dst"; //輸出圖像 Mat dst; Mat mask; //回調函數 Mat picture; int main(int argc, char** argv) { system("color 02"); cout << "尋找黃色飛龍繡球得到最小外邊框" << endl; VideoCapture capture(0); while (1) { //幀轉變為圖像 capture >> picture;//imread("D:\\4.jpg"); //方框濾波處理 boxFilter(picture, img, -1, Size(5, 2)); if (!img.data || img.channels() != 3) return -1; dst = Mat::zeros(img.size(), img.type()); bgr = img.clone(); //對輸出圖像大小的限制 Automatic size //HSV轉換 cvtColor(bgr, hsv, CV_BGR2HSV); inRange(hsv, Scalar(14, 43, 44), Scalar(33, 255 , 255), mask); Mat element = getStructuringElement(MORPH_RECT, Size(9, 11)); //圖像腐蝕 erode(mask, mask, element, Point(-1, -1), 2); morphologyEx(mask, mask, MORPH_DILATE, getStructuringElement(MORPH_DILATE, Size(25, 25)));//形態學膨脹 dilate(mask, mask, element);//圖像膨脹 imshow("66666", mask); namedWindow(dstName, 0); namedWindow(windowName, 0); vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(mask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE); RotatedRect box; double area = 0; for (int i = 0; i < contours.size(); i++) { if (contourArea(contours[i]) > area) { area = contourArea(contours[i]); box = minAreaRect(contours[i]); } } Point2f vertex[4]; box.points(vertex); for(int i=0;i<4;i++) line(img, vertex[i], vertex[(i + 1) % 4], Scalar(100, 200, 300), 2, LINE_AA); imshow(windowName, img); imshow(dstName, dst); waitKey(10); } return 0; }