流域边界紧紧围绕一个区域

Watershed boundaries closely surround one area

我试图在 OpenCV 中平均生成两个斑点。为此,我计划对按以下方式预处理的图像使用分水岭算法:

cv::Mat common, diff, processed, result;
cv::bitwise_and(blob1, blob2, common); //calc common area of the two blobs
cv::absdiff(blob1, blob2, diff);       //calc area where they differ

cv::distanceTransform(diff, processed, CV_DIST_L2, 3); //idea here is that the highest intensity 
                                                       //will be in the middle of the differing area
cv::normalize(processed, processed, 0, 255, cv::NORM_MINMAX, CV_8U); //convert floats to bytes

cv::Mat watershedMarkers, watershedOutline;
common.convertTo(watershedMarkers, CV_32S, 1. / 255, 1); //change background to label 1, common area to label 2
watershedMarkers.setTo(0, processed); //set 0 (unknown) for area where blobs differ

cv::cvtColor(processed, processed, CV_GRAY2RGB); //watershed wants 3 channels
cv::watershed(processed, watershedMarkers);
cv::rectangle(watershedMarkers, cv::Rect(0, 0, watershedMarkers.cols, watershedMarkers.rows), 1); //remove the outline

//draw the boundary in red (for debugging)
watershedMarkers.convertTo(watershedOutline, CV_16S);
cv::threshold(watershedOutline, watershedOutline, 0, 255, CV_THRESH_BINARY_INV);
watershedOutline.convertTo(watershedOutline, CV_8U);
processed.setTo(cv::Scalar(CV_RGB(255, 0, 0)), watershedOutline);

//convert computed labels back to mask (blob), less relevant but shows my ultimate goal
watershedMarkers.convertTo(watershedMarkers, CV_8U);
cv::threshold(watershedMarkers, watershedMarkers, 1, 0, CV_THRESH_TOZERO_INV);
cv::bitwise_not(watershedMarkers * 255, result);

我对结果的问题是计算出的边界(几乎)总是与两个斑点的公共区域相邻。以下是图片:

输入标记(黑色 = 0,灰色 = 1,白色 = 2)

分水岭输入图像(距离变换结果),结果轮廓以红色绘制:

我希望边界沿着输入的最大强度区域(即,沿着不同区域的中间)。相反(如您所见)它主要围绕标记为 2 的区域移动,并稍微移动以触摸背景(标记为 1)。我在这里做错了什么,还是我误解了分水岭的工作原理?

从这张图片开始:

只需将 all-zero 图像传递给分水岭算法即可得到正确的结果。 "basin" 然后从每个 "side"[ 开始平均填充 "water" =28=](然后记得去掉分水岭算法默认设置为-1的外边框):

代码:

#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);

    Mat1i markers(img.rows, img.cols, int(0));
    markers.setTo(1, img == 128);
    markers.setTo(2, img == 255);

    Mat3b image(markers.rows, markers.cols, Vec3b(0,0,0));
    markers.convertTo(markers, CV_32S);
    watershed(image, markers);

    Mat3b result;
    cvtColor(img, result, COLOR_GRAY2BGR);
    result.setTo(Scalar(0, 0, 255), markers == -1);

    imshow("Result", result);
    waitKey();

    return(0);
}