简化了识别的过程,但是基本的原理没有改变,加入了轨迹条,更加方便的调节参数
Mat img; //灰度值归一化 Mat bgr; //HSV图像 Mat hsv; //输出图像 Mat mask; Mat picture; int Hm = 0, Sm = 43, Vm = 46; int Hx = 10, Sx = 255, Vx = 255; void CALL_BACK(int,void*) { inRange(hsv, Scalar(Hm, Sm, Vm), Scalar(Hx, Sx,Vx), mask); imshow("颜色筛选", mask); } 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)); bgr = img.clone(); //对输出图像大小的限制 Automatic size cvtColor(bgr, hsv, CV_BGR2HSV); createTrackbar("Hmin","颜色筛选",&Hm,255, CALL_BACK); createTrackbar("Smin", "颜色筛选", &Sm, 255, CALL_BACK); createTrackbar("Vmin", "颜色筛选", &Vm, 255, CALL_BACK); createTrackbar("Hx", "颜色筛选", &Hx, 255, CALL_BACK); createTrackbar("Sx", "颜色筛选", &Sx, 255, CALL_BACK); createTrackbar("Vx", "颜色筛选", &Vx, 255, CALL_BACK); CALL_BACK(0, 0);
Mat element = getStructuringElement(MORPH_RECT, Size(9, 9)); //图像膨胀
dilate(mask, mask, element, Point(-1, -1), 2);
namedWindow("DEALED", 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("DEALED", img); waitKey(10); } return 0; }
后续会继续的更新的