Opencv hysteresis阈值实现

时间:2017-11-15 15:43:20

标签: opencv edge-detection

我有根据相位和幅度(单基因信号)计算的边缘的二进制图像,我想在OpenCv中应用滞后阈值处理。不幸的是,我无法使用它作为在opencv库中的Canny边缘检测中实现的。我想知道是否有解决方法或简单的实现方法。

1 个答案:

答案 0 :(得分:0)

我想出了这个解决方案:

Mat threshUpper, threshLower;
threshold(inImage, threshUpper, inThreshold, 128, CV_THRESH_BINARY);
threshold(inImage, threshLower, inThreshold-inHysteresis, 128, CV_THRESH_BINARY);

// Find the contours to get the seed from which starting floodfill
vector<vector<Point>> contoursUpper;
cv::findContours(threshUpper, contoursUpper, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

// Makes brighter the regions that contain a seed
for(auto cnt : contoursUpper){
    cv::floodFill(threshLower, cnt[0], 255, 0, 2, 2, CV_FLOODFILL_FIXED_RANGE);
}
//Threshold the image again to make black the not filled regions
threshold(threshLower, outImg, 200, 255, CV_THRESH_BINARY);

尽管如此,我仍然想弄清楚为什么Floodfill在某些输入上花费很多时间(约1.5秒)而在其他输入上却要平稳运行(5毫秒)!

编辑:如果您不在乎具有区域的二值图像,但您对轮廓属性感兴趣,则可以仅获取两个阈值图像,然后执行以下说明。有两种解决方案,一种保证滞后正确性,但为O(n ^ 2),另一种利用边界框近似该区域并在O(n)中运行

 vector<vector<Point>> contoursUpper, contoursLower;
 cv::findContours(threshUpper, contoursUpper, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
 cv::findContours(threshLower, contoursLower, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

 // Correct solution O(n^2)
 for(auto cntU : contoursUpper){
     for(auto cntL : contoursLower){
         if(cv::pointPolygonTest(cntL, cntU[0], false) >= 0){
             ///@todo: Do something with the cntL region (e.g. compute bounding box etc.)
             break; //Already found the connected region the others cannot be connected too
         }
     }
 }


// Approx. solution: O(n)
double minV, maxV;
for(auto cntL : largerContours){
    auto minBoundingBox = boundingRect(cntL);
    minMaxLoc(narrowThreshold(minBoundingBox), &minV, &maxV);
    if(maxV > 1){
        ///@todo: Do something with the cntL region (e.g. compute bounding box etc.)
    }
}