Binary noise removal using openCV

I read the video in Visual Studio using openCV and converted it to grayscale, then used the CV_THRESH_BINARY function to convert it to a binary image. However, there are holes and noise in the frames. What is an easy way to remove noise or holes? I read the Erode and Dilate functions in openCV, but I don’t really understand how to use them. this is my code so far. If anyone can show me how to include noise removal in my code, we will be very grateful.

#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; } 
+7
source share
2 answers

DISCLAIMER: It is difficult to give a good answer because you have provided very little information. If you placed your image before and after binarization, it would be much easier. However, I will try to give some tips.

If the holes are large enough, then probably the threshold value is wrong, try increasing or decreasing it and checking the result. You can try

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

This will automatically calculate the threshold value. If you cannot find a good threshold value, try some adaptive threshold algorithms, opencv has an adaptiveThreshold () function, but that is not so good.

If the holes and noise are quite small (a few pixels each), you can try the following:

  • Use opening (erosion, next dilatation) to remove white noise and close (dilatation, next erosion) to a little black noise. But remember that opening by removing white noise will also amplify black noise and vice versa.

  • Medium blur AFTER you create the threshold. It can remove small noise, both black and white, while maintaining colors (the image will be binary) and with possible small errors, shapes. Applying median blur to binarization can also help reduce a little noise.

+9
source

You can try using Smooth with CV_MEDIAN before you complete the threshold.

+1
source

All Articles