zoukankan      html  css  js  c++  java
  • 飞龙绣球的颜色追踪与最小外边框选择

    这是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;    
    }
  • 相关阅读:
    动态规划-矩阵链乘法
    钢条切割问题
    代码着色
    Sublime配置C和C++编译运行环境
    Guava中集合类的简单实用
    Junit单元测试入门
    Sublime Text 快捷键
    Editplus 的配色方案
    利用Wireshark任意获取QQ好友IP实施精准定位
    linux下实现定时执行php脚本
  • 原文地址:https://www.cnblogs.com/Loving-Q/p/11919547.html
Copyright © 2011-2022 走看看