Ask Your Question
0

Detecting an object inside a square table using findchessboardcorners

asked 2015-05-17 14:41:15 -0600

sweptica gravatar image

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;
}
edit retag flag offensive close merge delete

Comments

I would suggest also to try the approach that I have suggested here, in order to detect the squares.

theodore gravatar imagetheodore ( 2015-05-17 18:46:11 -0600 )edit

Hey, i tried you're exact code with my board, the image above but it only detects only 2 squares within the board. (The image of the board is given above) . . somehow with the image in the previous answer it works just as the output shown. Is there something i'm missing?

sweptica gravatar imagesweptica ( 2015-05-18 01:16:59 -0600 )edit

i wonder how to detect where user's foot. if you provide some images with the user on the board. let's think how to find on which square the foot is. ( sory for my poor english )

sturkmen gravatar imagesturkmen ( 2015-05-18 04:29:20 -0600 )edit

Im planning to use kinect to get the location of the user's foot (skeletal tracking), for now if I can just put an object anywhere on the square and detect whether the object is on the square and which square i'll be able to move forward maybe

sweptica gravatar imagesweptica ( 2015-05-18 10:18:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-18 06:44:55 -0600

theodore gravatar image

updated 2015-05-18 08:30:33 -0600

@sweptica you should not adapt the code as it is since the other case had different requirements and features. You need to play with the original code which I point in the other thread plus to apply some customizations in the parameters regarding your use case. For example changing the following in the code:

...
...
int N = 1;
...
findContours(gray, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
...
if( maxCosine < 0.3 && ratio <= 0.15)
...
...
adaptiveThreshold(~gray, bin, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -5);
imshow("bin", bin);

// find squares and draw them
Mat inv;
cvtColor(/*~*/bin, inv, CV_GRAY2BGR);
imshow("inv", inv);
vector<vector<Point> > squares;
findSquares(inv, squares);

I managed to get the following result:

image description

I guess with some further process and customization you will get the desired result. Plus as @sturkmen said it would be nice to see some pictures with people on it, since the requirements I have the feeling that would be totally different.

edit flag offensive delete link more

Comments

The reason i was using findchessboardCorners method is, in realtime it can still hold on to the inner corners irrespective or the perspective. But I tried implementing your code using webcam feed..a shift in perspective will affect the square detection :(

sweptica gravatar imagesweptica ( 2015-05-18 10:49:50 -0600 )edit

for how much shift are we talking, can you add a sample image. Also try to increase the values of ratio (initially this parameters did not exist, I just added it in order to separate rectangles from squares ) and threshold levels, N.

theodore gravatar imagetheodore ( 2015-05-18 14:54:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-05-17 14:41:15 -0600

Seen: 2,565 times

Last updated: May 18 '15