天天看點

Qt5顯示OpenCV4處理的輪廓線

先看一下canny線。

https://blog.csdn.net/islinyoubiao/article/details/113786741

Qt5顯示OpenCV4處理的輪廓線

修改一下代碼:

void MainWindow::cannyProc(cv::Mat in)
{
    if(in.empty())
        return;
    if(in.channels()>1)
        return;
//    lowThreshold = low;
    qDebug() << "start blur";
    cv::blur(in, detected_edges, cv::Size(3,3));
    qDebug() << "start canny";
    cv::Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
    dst= cv::Scalar::all(0);
    qDebug() << "start copy";
    src.copyTo(dst, detected_edges);
//    out = dst;

    //查找輪廓
        std::vector<std::vector<cv::Point>> contours;
        std::vector<cv::Vec4i> hierarchy;
        cv::findContours(detected_edges,contours,hierarchy,cv::RETR_TREE,cv::CHAIN_APPROX_SIMPLE,cv::Point());
        cv::Mat imageContours=cv::Mat::zeros(src.size(),CV_8UC1);  //輪廓
        cv::Mat marks(src.size(),CV_32S);   //Opencv分水嶺第二個矩陣參數
        marks=cv::Scalar::all(0);
        int index = 0;
        int compCount = 0;
        for( ; index >= 0; index = hierarchy[index][0], compCount++ )
        {
            //對marks進行标記,對不同區域的輪廓進行編号,相當于設定注水點,有多少輪廓,就有多少注水點
            cv::drawContours(marks, contours, index, cv::Scalar::all(compCount+1), 1, 8, hierarchy);
            cv::drawContours(imageContours,contours,index,cv::Scalar(255),1,8,hierarchy);
            cv::drawContours(dst, contours, index, cv::Scalar::all(compCount+1), 1, 8, hierarchy);
        }
}
           

添加Contours,

效果:

Qt5顯示OpenCV4處理的輪廓線

多謝,親愛的美美。