您可以对输入图像进行上采样,应用一些平滑处理,并找到 Otsu 阈值,然后使用不同的窗口大小 use this threshold to find Canny edges。
对于较大的窗口 (5 x 5),您会得到一个包含几乎所有您需要的边缘以及噪声的嘈杂图像。
对于较小的窗口 (3 x 3),您会得到噪点较少的图像,但缺少一些边缘。
如果这张噪声较小的图像不够好,您可以尝试使用噪声图像作为掩码对其进行形态重建。在这里,我使用形态学命中-未命中变换链接了噪声图像中的一些对角线边缘段,然后应用了重建。
使用
Mat k = (Mat_<int>(3, 3) <<
0, 0, 1,
0, -1, 0,
1, 0, 0);
用于连接断边的内核,您会得到更细的轮廓。
请注意,在下面的c++ 代码中,我使用了简单的重构。
Mat im = imread("rsSUY.png", 0);
/* up sample and smooth */
pyrUp(im, im);
GaussianBlur(im, im, Size(5, 5), 5);
/* find the Otsu threshold */
Mat bw1, bw2;
double th = threshold(im, bw1, 0, 255, THRESH_BINARY | THRESH_OTSU);
/* use the found Otsu threshold for Canny */
Canny(im, bw1, th, th/2, 5, true); /* this result would be noisy */
Canny(im, bw2, th, th/2, 3, true); /* this result would be less noisy */
/* link broken edges in more noisy image using hit-miss transform */
Mat k = (Mat_<int>(3, 3) <<
0, 0, 1,
0, -1, 0,
0, 0, 0);
Mat hitmiss;
morphologyEx(bw1, hitmiss, MORPH_HITMISS, k);
bw1 |= hitmiss;
/* apply morphological reconstruction to less noisy image using the modified noisy image */
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
double prevMu = 0;
Mat recons = bw2.clone();
for (int i = 0; i < 200; i++)
{
dilate(recons, recons, kernel);
recons &= bw1;
Scalar mu = mean(recons);
if (abs(mu.val[0] - prevMu) < 0.001)
{
break;
}
prevMu = mu.val[0];
}
imshow("less noisy", bw2);
imshow("reconstructed", recons);
waitKey();