zoukankan      html  css  js  c++  java
  • OpenCV代码:画出轮廓的外接矩形,和中心点

    #include <opencv2/highgui/highgui.hpp>  
    #include <opencv2/imgproc/imgproc.hpp>  
    #include <iostream>  
    #include <stdio.h>  
    using namespace std;
    using namespace cv;
    
    void find_centrepoint(Mat image, Point &centrepoint, RotatedRect &max_Rect)
    {
        
        Mat element = getStructuringElement(MORPH_ELLIPSE, Size(9, 9));
        morphologyEx(image, image, MORPH_OPEN, element);
        morphologyEx(image, image, MORPH_CLOSE, element);
        Mat cannyImage;
        Canny(image, cannyImage, 125, 250, 3);
        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;
        findContours(cannyImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    
        //vector<vector<Point> > contours_poly(contours.size());
        vector<RotatedRect> boundRect(contours.size());
        
        for (size_t i = 0; i < contours.size(); i++)
        {
            //approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
            boundRect[i] = minAreaRect(Mat(contours[i]));
        }
    
        vector<Point> max_contour;
        int area = 0;
        int idx;
        for (size_t i = 0; i < contours.size(); i++)
        {
            
            int t_area = boundRect[i].size.area();
            if (t_area>area)
            {
                max_contour = contours[i];
                max_Rect = boundRect[i];
                area = t_area;
                idx = i;
            }
        }
        centrepoint.x = max_Rect.center.x;
        centrepoint.y = max_Rect.center.y;
        Point2f P[4];
        max_Rect.points(P);
        for (int j = 0; j <= 3; j++)
        {
            line(image, P[j], P[(j + 1) % 4], Scalar(255), 2);
        }
        //rectangle(image, max_Rect.tl(), max_Rect.br(), Scalar(255), 4);
        //drawContours(image, contours, idx, Scalar(255), 2, 8);
    }
    int main(int args, char** argv)
    {
        Mat srcImage = imread("D:\Documents\BasedCam2 Files\Picture\贴膜机\镜头2\2018-03-07_15-47-48_030.bmp");
        if (!srcImage.data)
        {
            cout << "读取图像失败" << endl;
            return -1;
        }
        cvtColor(srcImage, srcImage, CV_BGR2GRAY);
        namedWindow("原图像", 0);
        resizeWindow("原图像", srcImage.cols / 4, srcImage.rows / 4);
        imshow("原图像", srcImage);
    
        Mat T = Mat(srcImage.size(), srcImage.type());
        adaptiveThreshold(srcImage, T, 255, ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 25, 5);
    
        Point centrePoint1;
        RotatedRect max_Rect1;
        find_centrepoint(T, centrePoint1, max_Rect1);
    
        Mat medImage;
        medianBlur(srcImage, medImage, 9);
        Mat binaryImage;
        threshold(medImage, binaryImage, 128, 255, THRESH_BINARY);
        Point centrePoint2;
        RotatedRect max_Rect2;
        find_centrepoint(binaryImage, centrePoint2, max_Rect2);
    
        namedWindow("圆外接矩形", 0);
        resizeWindow("圆外接矩形", srcImage.cols / 4, srcImage.rows / 4);
        imshow("圆外接矩形", T);
        namedWindow("膜接矩形", 0);
        resizeWindow("膜接矩形", srcImage.cols / 4, srcImage.rows / 4);
        imshow("膜接矩形", binaryImage);
    
        waitKey(0);
        return 0;
    
    }
    //Mat midImage, dstImage;
        //double fScale = 0.5;
        //Size dsize = Size(srcImage.cols*fScale, srcImage.rows*fScale);
        //resize(srcImage, srcImage,dsize);
        ////imshow("【原始图】", srcImage);
    
        //cvtColor(srcImage, midImage, CV_BGR2GRAY);//转化边缘检测后的图为灰度图  
        ////medianBlur(midImage, midImage, 9);
        ////bilateralFilter(midImage, midImage, 5, 10,10);
        //imshow("【原始图】", midImage);
        //vector<Vec3f> circles;
        //HoughCircles(midImage, circles, CV_HOUGH_GRADIENT, 1, 50, 100, 50, 200, 0);
        ////依次在图中绘制出圆  
        //for (size_t i = 0; i < circles.size(); i++)
        //{
        //    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
        //    int radius = cvRound(circles[i][2]);
        //    //绘制圆心  
        //    circle(srcImage, center, 3, Scalar(0, 255, 0), -1, 8, 0);
        //    //绘制圆轮廓  
        //    circle(srcImage, center, radius, Scalar(155, 50, 255), 3, 8, 0);
        //}
    
    //for (size_t i = 0; i< contours.size(); i++)
    //{
    //    //        drawContours(drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point());
    //    rectangle(T, boundRect[i].tl(), boundRect[i].br(), Scalar(255), 4, 8, 0);
    //    //        circle(drawing, center[i], (int)radius[i], color, 2, 8, 0);
    //    printf("%d - (%d,%d),(%d,%d)
    ", i, boundRect[i].x, boundRect[i].y, boundRect[i].width, boundRect[i].height);
    //}
    //for (size_t i = 0; i < contours.size(); i++)
    //{
    //    drawContours(T, contours, i, Scalar(255), 1, 8);
    //}
    
    
    //for (int i = 0; i < srcImage.rows; ++i)
    //    {
    //        for (int j = 0; j < srcImage.cols; ++j)
    //        {
    //            if (srcImage.at<uchar>(i, j) <= meanImage.at<uchar>(i, j) - 5)
    //            {
    //                T.at<uchar>(i, j) = 255;
    //            }
    //            else
    //            {
    //                T.at<uchar>(i, j) = 0;
    //            }
    //        }
    //    }
  • 相关阅读:
    wpf 打开win8系统软件盘
    wpf DATAgrid模板中button 命令绑定以及命令参数绑定
    <转>WPF 中的绑定
    代码方式给控件添加背景图片(WPF)
    WPF 枚举使用
    wpf 窗体添加背景图片
    c# 读取txt文件中文乱码解决方法
    Kali linux-信息收集-dmitry
    等保2.0所需设备
    Microsoft Dynamics 2013 --Social Pane
  • 原文地址:https://www.cnblogs.com/supercxm/p/8571483.html
Copyright © 2011-2022 走看看