使用openCV消除二进制图像中的噪声 [英] removing noise in a binary image using openCV

查看:206
本文介绍了使用openCV消除二进制图像中的噪声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}


推荐答案

免责声明:给一个好的答案,因为你提供了很少的信息。如果你在二进制化之前和之后发布你的图像,这将是更容易。

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);

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

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.

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

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.

这篇关于使用openCV消除二进制图像中的噪声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆