how to remove green ground from a soccer footage

asked 2016-01-13 01:50:03 -0600

valentine gravatar image

Hi all,

I am trying to perform player and ball tracking from video footage. How having some issues. Below is a sample code:

Mat grayInputimage = Mat(size(inputImage),CV_8UC1);
int blue,green, red;
cvtColor(inputImage,grayInputimage, CV_BGR2GRAY);
for (int i = 0; i < inputImage.rows; i++) {
    for (int j = 0; j < inputImage.cols; j++) {
        blue = inputImage.at<Vec3b>(i, j)[0];
        green = inputImage.at<Vec3b>(i, j)[1];
        red = inputImage.at<Vec3b>(i, j)[2];
        if (green > red && (green > blue && red > blue) ) {
            grayInputimage.at<uchar>(i, j) = 0;
        }
        else {
            grayInputimage.at<uchar>(i, j) = 1;
        }
    }
}

The input image is the frame from the video, while the grayimage should be image without the ground color green of the football pitch. However am getting this result. I will like if anyone can help.

edit retag flag offensive close merge delete

Comments

You do not need extra parenthesis in the if condition if you use just ANDs; and you have not posted any image. Did it removed some info? Maybe the problem is in red > blue. Another remark: you could use a mask for that condition and setTo(0) instead of that for loop

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-13 02:10:40 -0600 )edit
1

hi @valentine, hope you don't mind, i removed the duplicate ...

berak gravatar imageberak ( 2016-01-13 02:14:23 -0600 )edit
3

imho, you should use inRange , and not write loops

(also, HSV might work better than BGR)

berak gravatar imageberak ( 2016-01-13 02:20:35 -0600 )edit