使用 openCV 去除二值图像中的噪声

发布于 2025-01-06 15:27:58 字数 1382 浏览 1 评论 0原文

我使用 openCV 将视频读入 Visual Studio 并将其转换为灰度,然后使用函数 CV_THRESH_BINARY 将其转换为二进制图像。然而,框架上存在孔洞和噪音。消除噪音或孔洞的简单方法是什么?我已经阅读了 openCV 中的 Erode 和 Dilate 函数,但我不太清楚如何使用它们。这是我到目前为止的代码。如果有人可以向我展示如何将噪声消除合并到我的代码中,我将不胜感激。

#include "cv.h"
#include "highgui.h"

int main( int argc, char* argv ) {

CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
    return -1;
}

IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 70;

int frameCount=0;//Counts every 5 frames
cvNamedWindow( "Binary video", CV_WINDOW_AUTOSIZE );

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;
    gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height),      color_frame->depth, 1);
    if( !color_frame ) break;// If the frame does not exist, quit the loop

    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_BINARY);
        cvShowImage("Binary video", gray_frame);
        frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "Grayscale video" );

return 0;
}

I had read in a video into Visual Studio using openCV and converted it to grayscale then used the function CV_THRESH_BINARY to convert it into a binary image. However, there are holes and noise in the frames. What is a simple way to remove noise or the holes? I have read up on the Erode and Dilate functions in openCV but I am not too clear on how to use them. this is my code so far. If anyone can show me how to incorporate the noise removal into my code, it would be greatly appreciated.

#include "cv.h"
#include "highgui.h"

int main( int argc, char* argv ) {

CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
    return -1;
}

IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 70;

int frameCount=0;//Counts every 5 frames
cvNamedWindow( "Binary video", CV_WINDOW_AUTOSIZE );

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;
    gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height),      color_frame->depth, 1);
    if( !color_frame ) break;// If the frame does not exist, quit the loop

    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_BINARY);
        cvShowImage("Binary video", gray_frame);
        frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "Grayscale video" );

return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2025-01-13 15:27:58

免责声明:很难给出一个好的答案,因为您提供的信息很少。如果你在二值化之前和之后发布你的图像,那就容易多了。不过,我会尝试给出一些提示。

如果洞比较大,那么阈值可能是错误的,尝试增加或减少它并检查结果。您可以尝试

cv::threshold(gray_frame, gray_frame, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

这会自动计算阈值。
如果找不到好的阈值,那么尝试一些自适应阈值算法,opencv有adaptiveThreshold()函数,但不太好。

如果孔和噪声相当小(每个像素很少),您可以尝试以下一些操作:

  • 使用开(腐蚀,下一个膨胀)来消除白噪声,并使用闭(膨胀,下一个腐蚀)来消除小黑噪声。但请记住,开运算在消除白噪声的同时,也会增强黑噪声,反之亦然。

  • 进行阈值处理后的中值模糊。它可以消除黑色和白色的小噪声,同时保留颜色(图像仍然是二进制的),并且可能存在小错误,保留形状。在二值化之前应用中值模糊也可能有助于减少小噪声。

DISCLAIMER: It is hard to give a good answer, because you provided very little info. If you posted your image before and after binarization, it would be much easier. However, I will try to give some hints.

If the holes are rather big, then probably threshold value is wrong, try increasing or decreasing it and check the result. You can try

cv::threshold(gray_frame, gray_frame, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

This will calculate threshold value automatically.
If you cannot find a good thresholding value, then try some adaptive thresholding algorithms, opencv has adaptiveThreshold() function, but it's not so good.

If the holes and noise are rather small (few pixels each), you can try some of the following:

  • Using opening (erosion, next dilatation) to remove white noise and closing(dilatation, next erosion) to small black noise. But remember, that opening, while removing white noise, will also strengthen black noise and vice versa.

  • Median blur AFTER you do thresholding. It may remove small noise, both black and white, while preserving colors (image will stil be binary) and, with posssible small errors, shapes. Applying median blur BEFORE binarization may also help reduce small noise.

小…楫夜泊 2025-01-13 15:27:58

您可以尝试使用带有 CV_MEDIANSmooth 函数> 在进行阈值处理之前。

You might try using a Smooth function with CV_MEDIAN before you do the thresholding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文