Identifying the dragon Hydrangea contour found, and the rotation of a rectangular frame with minimal

#include <opencv2/opencv.hpp>
#include<iostream>
#include<string>
using namespace cv;
using namespace std;
//输入图像
Mat img;
//灰度值归一化
Mat bgr;
//HSV图像
Mat hsv;
//色相

//输出图像
Mat dst;
Mat mask;
Mat img1;
//int frame_width;
//回调函数
int main()
{

    VideoCapture capture(0);


    //循环显示每一帧
    while (1)
    {
    
        capture >>img1;//读取当前帧


        /*frame_width = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH);*/
        
        GaussianBlur(img1, img, Size(5, 5), 0, 0);
        /*dst = Mat::zeros(img.size(), img.type());*/
        bgr = img.clone();
        //颜色空间转换
        cvtColor(bgr, hsv, CV_BGR2HSV);
        inRange(hsv, Scalar(26, 43, 46), Scalar(32, 255, 255), mask);
        //掩模到原图的转换
    /*  for (int r = 0; r < bgr.rows; r++)
        {
            for (int c = 0; c < bgr.cols; c++)
            {
                if (mask.at<uchar>(r, c) == 255)
                {
                    dst.at<Vec3b>(r, c) = bgr.at<Vec3b>(r, c);
                }
            }
        }*/
        

        /*Mat gay;

        cvtColor(gay, gay, COLOR_RGB2GRAY);*/

        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;
        findContours(mask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
                RotatedRect box;
        double area = 0;
        for (int j = 0; j < contours.size(); j++) 
                {
                    if (contourArea(contours[j]) > area)
                    {
                        box = minAreaRect(contours[j]);
                        area = contourArea(contours[j]);
                        
                    }
                }

        Point2f vertex[4];
        box.points(vertex);
        for (int i = 0; i < 4; i++)
            line(img, vertex[i], vertex[(i + 1) % 4], Scalar(100, 200, 211), 2, LINE_AA);
        
    
    imshow("bug", img);
    
    if (waitKey(30) >= 0)break;//延时30ms

    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xingkongcanghai/p/11220163.html