Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detecting an object inside a square table using findchessboardcorners

I'm trying to create an exercise game where the user's need to step on a square board depending on the instruction. I need to first detect the square board in the environment and then detect whether the user's foot is present in one of the squares within the board. (real time)

Here is the square table that I will be using

Square table

I need to find the boundary of the squares within the square board, for this purpose i'm trying the logic with a chessboard first. I am able to find the corners in the chessboard using findChessboardCorners function and drawing them successfully using cornerSubPix function. However when I place an object over the chessboard square pattern, the findChessboardCorners doesn't work anymore (returns 0). I realize that maybe the algorithm finds only connected squares using icvFindConnectedQuads. Is there a solution to this problem? (do i need to use chessboard to succesfully get my game working? Is there an alternative method to implement the game?)

#include <opencv2/core/core.hpp>;
#include <opencv/cv.h>;
#include <opencv2/highgui/highgui.hpp>;


using namespace cv;
using namespace std;

int main()
{
    int numBoards = 0;
    int numCornersHor;
    int numCornersVer;

    printf("Enter number of corners along width: ");
    scanf("%d",&numCornersHor);

    printf("Enter number of corners along height: ");
    scanf("%d",&numCornersVer);

    printf("Enter number of boards: ");
    scanf("%d",&numBoards);

    int numSquares = numCornersHor * numCornersVer;

    Size board_sz = Size(numCornersHor,numCornersVer);

    VideoCapture capture = VideoCapture(0);

    /*Physical location of the corners in real world (3D space)*/
    vector<vector<Point3f>> object_points;
    /*location of the corners on the image (2D image)*/
    vector<vector<Point2f>> image_points;

    /*Current image's (snapshots)  corners*/
    vector<Point2f>corners;
    int successes = 0;

    Mat image;
    Mat gray_image;
    capture >> image;

    vector<Point3f> obj;
    for(int j = 0; j < numSquares; j++)
    {
        obj.push_back(Point3f(j/numCornersHor,j%numCornersVer,0.0f));
    }

    while(successes < numBoards)
    {
        cvtColor(image,gray_image,CV_BGR2GRAY);

        bool found = findChessboardCorners(image,board_sz,corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

        if(found)
        {
            cornerSubPix(gray_image,corners,Size(11,11),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
            drawChessboardCorners(gray_image,board_sz,corners,found);
        }

        imshow("normal img",image);
        imshow("gray image", gray_image);

        capture >> image;
        int key = waitKey(1);

        if(key == '27')
            return 0;

        if(key == '0' && found!='0')
        {
            image_points.push_back(corners);
            object_points.push_back(obj);

            printf("Snap stored!");

            successes++;

            if(successes >= numBoards)
            {
                break;
            }
        }
    }

    return 0;
}