Search code examples
opencvcomputer-visiondetection

Detecting a hand above a chessboard using opencv


I am developing an android application for analyzing chess games based on series of photos. To process images, I am using OpenCV. My question is how can I detect that there is a player's hand on a picture? Because I would like to filter those photos and analyze only the ones with the only chessboard on them.

So far I managed to get the Canny, so from an image like that original image

I am able to get that canny

.

But I have no idea what can I do next...

The code I used to get Canny:

Mat gray, blur, cannyed;
cvtColor(img, gray, CV_BGR2GRAY);
GaussianBlur(gray, blur, Size(7, 7), 0, 0);
Canny(blur, cannyed, 50, 100, 3);

I would highly appreciate any ideas and advice on what to do next and what OpenCV functions can I use.


Solution

  • Thank you all very much for your suggestions.

    So I solved the problem mostly using Gowthaman's method. First I use his code to generate vertical and horizontal lines. Then I combine them like this:

    Mat combined = vertical + horizontal; 
    

    So I get something like that when there is no hand

    without hand

    or like that when there is a hand

    with hand.

    Next I count white pixels using the code:

    int GetPixelCount(Mat image, uchar color)
    {
        int result = 0;
    
        for (int i = 0; i < image.rows; i++)
        {
            for (int j = 0; j < image.cols; j++)
            {
                if (image.at<uchar>(Point(j, i)) == color)
                    result++;
            }
        }
    
        return result;
    }
    

    I do that for every photo in the series. First photo is always without a hand, so I use is as a template. If current photo has less then 98% of template white pixels then I deduce there is hand (or something else) in it.

    Most likely this is not an optimal method and has lots of weaknesses, but it is very simple and works for me just fine :)