飞龙绣球的颜色追踪与最小外边框选择

这是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;    
}

 

posted @ 2019-11-23 20:32  Loving-Q  阅读(357)  评论(0编辑  收藏  举报