0

I'd like to build an AI that can play a game of tic-tac-toe. So first, I'm trying to use OpenCV to "see" the board. To build my classifier, I used the following commands:

opencv_createsamples -vec pos_out.vec -img board.png -w 48 -h 48 -bg neg.txt -num 50
opencv_traincascade -data training_data/ -vec pos_out.vec -numPos 50 -numNeg 7 -bg neg.txt -w 48 -h 48

I don't have many pictures laying around so the seven background (negative) photos are just desktop backgrounds. The positive picture is a screenshot of a game of tic-tac-toe, cropped so just the board is visible.

The code that uses the classifier just takes the classifier as an argument, and a picture it's supposed to be looking in for a game board:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

void detectAndDisplay(Mat frame);

CascadeClassifier board_cascade;

int main(int argc, char **argv)
{
    CvCapture* capture;
    Mat frame;

    if (!board_cascade.load(argv[1])) {
        fprintf(stderr, "Error loading cascade file %s.", argv[1]);
        return -1;
    }

    frame = imread(argv[2], 1);

    for (int i=0; i<10; i++)
        detectAndDisplay(frame);

    waitKey(0);

    return 0;
}

void detectAndDisplay(Mat frame)
{
    int i;
    vector<Rect> boards;
    Mat frame_gray;

    cvtColor(frame, frame_gray, CV_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    board_cascade.detectMultiScale(frame_gray, boards, 1.5, 8, 0, Size(500, 500));

    for(i=0; i<boards.size(); i++) {
        Point center(boards[i].x + boards[i].width/2, boards[i].y+boards[i].height/2);
        ellipse(frame, center, Size(boards[i].width/2, boards[i].height/2), 0, 0, 360, Scalar(255, 0, 0), 2, 8, 0);
        printf("Width: %d height: %d x: %d y: %d\n", boards[i].width, boards[i].height, boards[i].x, boards[i].y);;
    }

    if (i > 0) {

        int startx = boards[0].x;
        int starty = boards[0].y;
        int width = boards[0].width;
        int height = boards[0].height;

        for (i=1; i<=6; i+=2) {
            for (int j=1; j<=6; j+=2) {
                Point center(startx + (width/6 * i), starty + (height/6 * j));
                ellipse(frame, center, Size(30, 30), 0, 0, 360, Scalar(255, 0, 0), 2, 8, 0);
            }
        }
    }

    imshow("Board detection", frame);
}

This is board.png, which I use as my "positive" image. This is the result when the board in the image is the same size as the "positive" image, and this is the result when its not. As you can see, it does reasonably alright when the board is the same size, but not when its larger.

I've never "trained" a classifier before, so I was hoping to get some pointers on how to improve my results. Would adding more positive samples help? Or are there some parameters I should be modifying somewhere?

Thanks!

4
  • 1
    1 positive sample is too few. In your case, because your positive sample is very "simple" and varies only in scale, you can generate multiple versions of your positive samples with different scales using code. Start with just a few hundred of them, and slowly increase the number. Similarly, for your negative examples, you randomly crop out regions in screenshots. Btw, because your positive example is "fixed", you can also consider using standard image processing technique such as finding square regions with the color distribution of your board or use that to post-process the classifier result. Mar 18, 2014 at 2:39
  • 1
    Why don't you just try to detect lines and continue from there?
    – GilLevi
    Mar 18, 2014 at 16:10
  • @lightalchemist - I was under the impression that all the positive samples must have the same dimensions, is that not true? Could use both a 400x400px picture of the board, as well as a 200x200px picture as positive samples?
    – user693861
    Mar 18, 2014 at 18:37
  • @both - I originally thought that using a classifier would be easier than detecting lines, but maybe not. I'd like to still get the classifier working for my own education, but I think I will end up using a simpler line/square detection technique. Thanks!
    – user693861
    Mar 18, 2014 at 18:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.