OpenCV - 删除“白”来自图像的伪影和拟合曲线 [英] OpenCV - Remove "white" artifacts from image and fit a curve
问题描述
我有一个图像,我需要删除不代表检测到的曲线的白色工件。我想保留代表曲线的白色像素,但删除远离曲线的像素离群值。删除伪像后,我想为图像中的点拟合一条平滑的曲线。
I have an image where in I need to remove the "white" artifacts that do not represent a detected curve. I want to preserve the white pixels which represent the curve but, remove pixel outliers which are far away from the curve. After removing the artifacts, I would like to fit a smooth curve to the points in the image.
这是我的形象:
图像是一个只有黑点和白点的8位灰度图像。 曲线中的白色像素是感兴趣的区域,并且它们中的大多数仅仅是单个像素的厚度。一些白色像素彼此连接,但不是全部连接。一些白色像素被散布。我想删除这些白色像素,因为它们不对整体形状有贡献,因为我想要为它们拟合一条平滑的曲线。
The image is a 8-bit grayscale image image with only black and white points. The white pixels in the "curve" are the area of interest and most of them are only a single pixel thick. Some white pixels are connected to each other but, not all of them. Some white pixels are strewn across. I would like to remove these white pixels since they are not contributing to the overall shape because I want to fit a smooth curve to them.
到目前为止,我尝试过 dilation
,然后是侵蚀
,关闭
和黑帽子
操作。他们似乎没有给我我期望的结果。
我也尝试遍历图像中的所有点,找到亮点,在该像素周围看一个3x3邻域,并将值设置为 0
没有多于两个相等于自己的邻居。
So far, I have tried to do dilation
followed by erosion
, closing
and black hat
operations. None of them seem to give me the result that I expect.
I also tried to iterate through all points in the image, find the bright points, look in a 3x3 neighborhood around that pixel and set the value to 0
if it did not have more than two neighbors which are equal to itself.
我可以申请什么才能达到我想要的结果?此外,一旦我有最后的干净输出,如何为图像中的点拟合一条平滑的曲线。
What can I apply in order to achieve my desired result? Also, once I have my final "clean" output, how do I fit a smooth curve to the points in the image?
推荐答案
掌握具有实用计算机视觉项目的OpenCV - Daniel Lelis
Baggio
Mastering OpenCV with Practical Computer Vision Projects - Daniel Lelis Baggio
我将这些包括在实例化此函数的标题上添加:
I have these includes added on the header where this function was instantiated:
#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
mask是您要过滤的图片,它会删除任何没有点的点5个像素周围分析的一个...
希望它有帮助
mask is the image you want to filter, and it removes dots that don't have dots in any 5 pixels around the analysed one... Hope it helps
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++;
}
}
这篇关于OpenCV - 删除“白”来自图像的伪影和拟合曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!