Smoothing image after using Inrange() function

asked 2014-09-14 06:12:17 -0500

nhat.truong gravatar image

updated 2014-09-14 06:15:35 -0500

Hi everybody ! I used Inrange() function to detect tennis ball from webcam (this ball is green), after I used this function I got an binary image that is very rough. Here is my code : void morphOps(Mat &thresh) {

        Mat erodeElement = getStructuringElement( MORPH_RECT,Size2i (2,2));
        Mat dilateElement = getStructuringElement( MORPH_RECT,Size2i (20,20));
        erode(thresh,thresh,erodeElement);
        erode(thresh,thresh,erodeElement);
        dilate(thresh,thresh,dilateElement);
        dilate(thresh,thresh,dilateElement);
        return;
    }

void main() { VideoCapture capture(0); Mat src,dst,hsv; int red,grn,blu,red1,grn1,blu1 =0; namedWindow("Window"); namedWindow("Window1"); createTrackbar("red","Window",&red,255); createTrackbar("green","Window",&grn,255); createTrackbar("blue","Window",&blu,255); createTrackbar("red1","Window1",&red1,255); createTrackbar("green1","Window1",&grn1,255); createTrackbar("blue1","Window1",&blu1,255);

while(true)
{

    capture >> src;
    cvtColor(src,hsv,COLOR_BGR2HSV);
    Scalar lower(red,grn,blu);
    Scalar upper(red1,grn1,blu1);
    inRange(hsv,lower,upper,dst);

            //morphOps(dst);

            imshow("Input Image",hsv);
            imshow("Output Image",dst);
            char c= waitKey(10);
            if(c==27) break;
}

This is Input image after transforming to HSV color C:\fakepath\input.png and the result C:\fakepath\clutter.png Can everyone help me to filter and smooth this image ? And finally there is only this ball on black background. This result is created after I choose the argument for Scalar is : lower(41,44,49) upper(91,255,255) ! Have a great day !!

edit retag flag offensive close merge delete