gpt4 book ai didi

c++ - OpenCV - 从图像中删除 "white"伪影并拟合曲线

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:41 25 4
gpt4 key购买 nike

我有一张图像,我需要在其中移除不代表检测到的曲线的“白色”伪影。我想保留代表曲线的白色像素,但要删除远离曲线的像素异常值。去除伪影后,我想为图像中的点拟合一条平滑的曲线。

这是我的图片:

white detected curve

图像是只有黑白点的8位灰度图像。 “曲线”中的白色像素是感兴趣的区域,其中大部分只有一个像素厚。一些白色像素相互连接,但不是全部。散落着一些白色像素。我想删除这些白色像素,因为它们对整体形状没有贡献,因为我想为它们拟合一条平滑的曲线。

到目前为止,我已尝试执行膨胀,然后是腐 eclipse 关闭黑帽 操作。他们似乎都没有给我我期望的结果。我还尝试遍历图像中的所有点,找到亮点,查看该像素周围的 3x3 邻域并将值设置为 0 如果它没有超过两个相等的邻居对自己。

我可以应用什么来达到我想要的结果?另外,一旦我有了最终的“干净”输出,我如何将平滑曲线拟合到图像中的点?

最佳答案

我不知道如何拟合曲线,你最好在另一个问题中问这个。


关于噪声:这个函数应该可以解决问题,我用它来去除黑色和等待图像中的噪声。这是书中的代码示例:

Mastering OpenCV with Practical Computer Vision Projects - Daniel Lelis Baggio

我在实例化此函数的标题中添加了这些包含:

#include  "opencv2/opencv.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

mask 是您要过滤的图像,它会删除被分析图像周围任意 5 个像素中没有点的点...希望对你有帮助

void removePepperNoise(Mat &mask)
{
// For simplicity, ignore the top & bottom row border.
for (int y=2; y<mask.rows-2; y++) {
// Get access to each of the 5 rows near this pixel.
uchar *pThis = mask.ptr(y);
uchar *pUp1 = mask.ptr(y-1);
uchar *pUp2 = mask.ptr(y-2);
uchar *pDown1 = mask.ptr(y+1);
uchar *pDown2 = mask.ptr(y+2);

// For simplicity, ignore the left & right row border.
pThis += 2;
pUp1 += 2;
pUp2 += 2;
pDown1 += 2;
pDown2 += 2;
for (int x=2; x<mask.cols-2; x++) {
uchar v = *pThis; // Get the current pixel value (either 0 or 255).
// If the current pixel is black, but all the pixels on the 2-pixel-radius-border are white
// (ie: it is a small island of black pixels, surrounded by white), then delete that island.
if (v == 0) {
bool allAbove = *(pUp2 - 2) && *(pUp2 - 1) && *(pUp2) && *(pUp2 + 1) && *(pUp2 + 2);
bool allLeft = *(pUp1 - 2) && *(pThis - 2) && *(pDown1 - 2);
bool allBelow = *(pDown2 - 2) && *(pDown2 - 1) && *(pDown2) && *(pDown2 + 1) && *(pDown2 + 2);
bool allRight = *(pUp1 + 2) && *(pThis + 2) && *(pDown1 + 2);
bool surroundings = allAbove && allLeft && allBelow && allRight;
if (surroundings == true) {
// Fill the whole 5x5 block as white. Since we know the 5x5 borders
// are already white, just need to fill the 3x3 inner region.
*(pUp1 - 1) = 255;
*(pUp1 + 0) = 255;
*(pUp1 + 1) = 255;
*(pThis - 1) = 255;
*(pThis + 0) = 255;
*(pThis + 1) = 255;
*(pDown1 - 1) = 255;
*(pDown1 + 0) = 255;
*(pDown1 + 1) = 255;
}
// Since we just covered the whole 5x5 block with white, we know the next 2 pixels
// won't be black, so skip the next 2 pixels on the right.
pThis += 2;
pUp1 += 2;
pUp2 += 2;
pDown1 += 2;
pDown2 += 2;
}
// Move to the next pixel.
pThis++;
pUp1++;
pUp2++;
pDown1++;
pDown2++;
}
}

关于c++ - OpenCV - 从图像中删除 "white"伪影并拟合曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com