轮廓-threshold边缘检测(灰度图)

注:使用彩色图测试结果失败了,灰度图就可以

double threshold( InputArray src, OutputArray dst,double thresh, double maxval, int type );

    src:8位原始图像
    dst:输出图像
    thresh:阀值
    maxval:灰度图的最大值,输出图像的最大像素值
    type: 检测方式
    THRESH_BINARY     = 0,
    THRESH_BINARY_INV = 1,
    THRESH_TRUNC      = 2,
    THRESH_TOZERO     = 3,
    THRESH_TOZERO_INV = 4,
    THRESH_MASK       = 7,
    THRESH_OTSU       = 8,
    THRESH_TRIANGLE   = 16

     //m_Mat原始图像转灰度图
    m_MatGray = Mat::zeros(m_Mat.size(),m_Mat.type()) ;
    cvtColor(m_Mat, m_MatGray,COLOR_BGR2GRAY);


    //边缘检测:m_Mat_OutPut接收轮廓点的图
    threshold(m_MatGray,m_Mat_OutPut,150,200,THRESH_BINARY);


    //轮廓发现
    vector<vector<Point>>contours;
    vector<Vec4i>hierachy;
    findContours(m_Mat_OutPut,contours,hierachy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));


    //将符合条件的轮廓画在原图上
    for (int i = 0; i < contours.size(); ++i) {
        double area = contourArea(contours[i]);//轮廓面积
        double  length = arcLength(contours[i],false);//轮廓长度
        if(area<1000&&length<1000){continue;}
        drawContours(m_Mat,contours,i,Scalar(rand()%255,rand()%255,rand()%255));//
    }


    //显示
    QImage img = cvMat_To_Qimage(m_Mat);
    img = img.scaled(ui->label_4->geometry().width(),ui->label_4->geometry().height(),
                     Qt::KeepAspectRatio,Qt::SmoothTransformation);
    ui->label_4->setPixmap(QPixmap::fromImage(img));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值