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

2022-10-13,,

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

 

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

下载本文的Word格式文档,以方便收藏与打印。