Removing noise from Contour features in Real time
Hi,
In my project, I apply findcontours function to detect a rectangular object within a region of interest in the image. I am doing it in real time. Subsequently, I enclose the detected contour by a boundedrect which gives me its four vertices. However, the position of these vertices is not stable, it is changing very fast. Looks like the contour area is growing and shrinking, and have variations which causes the vertices' position to change. I have tried following solutions, but to no avail.
- Low pass filtering on the output of pixel positions.
- Median and Gaussian blur within the ROI.
- FastNlmeansDenoising: which is too slow for my application.
I am including my code snippet where I find contours below:
//medianBlur(img, img, 21);
cvtColor (img, img, COLOR_BGR2GRAY);
//fastNlMeansDenoising (img, img, 3, 7, 21);
threshold(img, img, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
//medianBlur(img, img, 21);
Mat dilateElement = getStructuringElement( MORPH_RECT,Size(11,11));
//Mat erodeElement = getStructuringElement ( MORPH_RECT, Size(8,8) );
dilate(img,img,dilateElement);
//erode (img, img, erodeElement);
floodFill (img, Point(0,0), Scalar(0));
// namedWindow("dilated",CV_WINDOW_NORMAL);
// imshow("dilated",img);
// cout << "Inside corrected corners p3" << endl;
//GaussianBlur (img, img, Size(15,15), 0, 0);
Canny (img, img, 0, 10, 5);
dilate(img, img, Mat(), Point(-1,-1));
//cout << "Inside corrected corners p3.5" << endl;
findContours(img, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
// cout<<contours.size()<<endl;
// Mat tempimg(img.rows, img.cols, CV_8U, cv::Scalar(0));
// drawContours( tempimg, contours, -1, Scalar(255), 3, CV_AA );
// namedWindow("contours", CV_WINDOW_NORMAL);
// imshow("contours", tempimg);
// cout << "Inside corrected corners p4" << endl;
I would appreciate any help in this regard. I have tried googling a lot but couldn't find a good post which might direct me towards removal of noise from detected contours in real time. I think the nature of noise is Impulse.
Just some remarks to your code
Could you add a video / image to show how worse the jitter is?
Thank you very much for your response Steven! Even applying erosion doesn't give me a good response. I have uploaded a video on youtube which shows the jitter. You can see it at: https://youtu.be/5bpoDsImkvM
I differentiate the distance between red and blue spots in order to get the feature velocity. The velocity vector besides the image window (red text on black) shows the change, which should be zero since the camera is at rest.