opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

本章内容:


1. 轮廓查找
2. 绘制轮廓

3. 凸包

4.最小外接矩形

5.最小外接圆

6.最小外接椭圆

 

1.搜索轮廓

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

2.绘制轮廓

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

输出结果

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

3.凸包

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

输出结果

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

4.最小外接矩形

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

输出结果

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

5.最小外接圆

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

输出结果:

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

6. 最小外接椭圆

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

输出结果

opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

 

代码


#include <ostream>
#include <opencv.hpp>
#include <math.h>

int main(int argc, char *argv[])
{
    /*
     本章内容:
        1. 轮廓查找
        2. 绘制轮廓
        3. 凸包
        4.最小外接矩形
        5.最小外接圆
        6.最小外接椭圆
    */
    cv::String fileName = "/home/wang/dev/Image/QT.jpg";
    cv::String fileName1 = "/home/wang/dev/Image/hei.png";
    cv::Mat src = cv::imread(fileName);
    cv::Mat src1 = cv::imread(fileName1);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return -1;
    }
    /* 轮廓查找
     * api接口: CV_EXPORTS_W void findContours( InputArray image, OutputArrayOfArrays contours,
                              OutputArray hierarchy, int mode,
                              int method, Point offset = Point());
        参数分析:
            @param contours. std::vector<std::vector<cv::Point> >
            @param hierarchy Optional output vector
            @param mode Contour retrieval mode, see #RetrievalModes
            @param method Contour approximation method, see #ContourApproximationModes
        enum RetrievalModes{
                RETR_EXTERNAL  = 0,
                RETR_LIST      = 1,
                RETR_CCOMP     = 2,
                RETR_TREE      = 3,
                RETR_FLOODFILL = 4
            }
        enum ContourApproximationModes{
        enum ContourApproximationModes {
                CHAIN_APPROX_NONE      = 1,
                CHAIN_APPROX_SIMPLE    = 2,
                CHAIN_APPROX_TC89_L1   = 3,
                CHAIN_APPROX_TC89_KCOS = 4
            }
    */
    cv::imshow("src",src);
    cv::Mat dstCany;
    cv::Mat gray;
    cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
    cv::Canny(gray,dstCany,50,150);
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;
    cv::findContours(dstCany,contours,hierarchy,cv::RETR_TREE,cv::CHAIN_APPROX_SIMPLE,cv::Point(0,0));
    cv::RNG rng(1234);
    cv::Mat dst(src.size(),src.type());
    /* 绘制轮廓
        api接口: CV_EXPORTS_W void drawContours( InputOutputArray image, InputArrayOfArrays contours,
                                      int contourIdx, const Scalar& color,
                                      int thickness = 1, int lineType = LINE_8,
                                      InputArray hierarchy = noArray(),
                                      int maxLevel = INT_MAX, Point offset = Point() );
        参数分析:
            @param contourIdx Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
            @param color Color of the contours.
    */
    for(int i=0; i <contours.size();i++){
        cv::Scalar color = cv::Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
        cv::drawContours(dst,contours,i,color,4);
    }

    /* 3.凸包
     * api接口:CV_EXPORTS_W void convexHull( InputArray points, OutputArray hull,
                              bool clockwise = false, bool returnPoints = true );
            @param points 二维点集,vector
            @param hull Output convex hull. vector<Point>
    */
    std::vector<cv::Point> hull;
    cv::convexHull(contours[0],hull);
    for(int i=0;i<hull.size();i++){
        cv::line(dst,hull[i],hull[(i+1)%hull.size()],cv::Scalar(0,255,0),4);
    }


    /* 4.最小外接矩形
        apie接口: CV_EXPORTS_W RotatedRect minAreaRect( InputArray points );
    */
    cv::RotatedRect rect = cv::minAreaRect(contours[1]);
    cv::Point2f Ps[4];
    rect.points(Ps);
    for(int i=0;i<4;i++) cv::line(dst,Ps[i],Ps[(i+1)%4],cv::Scalar(0,0,255),4);

    /* 5.最小外接圆
     * api接口: CV_EXPORTS_W void minEnclosingCircle( InputArray points,
                                      CV_OUT Point2f& center, CV_OUT float& radius );
    */
    cv::Point2f pc;
    float radius;
    cv::minEnclosingCircle(contours[2],pc,radius);
    cv::circle(dst,pc,radius,cv::Scalar(255,0,0),4);

    /*6.最小外接椭圆
     * api接口:CV_EXPORTS_W RotatedRect fitEllipse( InputArray points );
    */
    cv::RotatedRect ell = cv::fitEllipse(contours[1]);
    cv::ellipse(dst,ell,cv::Scalar(0,255,255),4);

    cv::imshow("dst",dst);
    cv::waitKey(0);
    return 1;
}