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

本章内容:


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

3. 凸包

4.最小外接矩形

5.最小外接圆

6.最小外接椭圆

 

1.搜索轮廓

2.绘制轮廓

输出结果

3.凸包

输出结果

4.最小外接矩形

输出结果

5.最小外接圆

输出结果:

6. 最小外接椭圆

输出结果

 

代码


#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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值