how to remove green ground from a soccer footage

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

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 =<Vec3b>(i, j)[0];
        green =<Vec3b>(i, j)[1];
        red =<Vec3b>(i, j)[2];
        if (green > red && (green > blue && red > blue) ) {
  <uchar>(i, j) = 0;
        else {
  <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


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 -0500 )edit

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

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

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 -0500 )edit