Piece Recognition in Java For A Connect 4 Game Board

114 Views Asked by At

I'm currently working on a project and I'm trying to transform a physical 'Connect 4' game board onto a digital java program.

Using Open-CV and my webcam, I am able to constantly grab photos of this game board and save them locally on my PC.

To get the location of the pieces, I have been using colour recognition, however, it is a little bit unreliable at times. I tend to move environments which affect the amount of lighting and then it all goes downhill from there. :(

Does anyone know any alternatives to be able to detect pieces on a Connect 4 board?

Here is some of the code I've made and been using:

  for (int y = 35; y < img.getHeight(); y = y + 85) {
                for (int x = 60; x < img.getWidth(); x = x + 85) {
                    // Retrieving contents of a pixel
                    int pixel = img.getRGB(x, y);
                    
                    float hsb[] = new float[3];
                    int r = (pixel >> 16) & 0xFF;
                    int g = (pixel >>  8) & 0xFF;
                    int b = (pixel      ) & 0xFF;
                    Color.RGBtoHSB(r, g, b, hsb);
                 
                    if (hsb[1] < 0.1 && hsb[2] > 0.9) {
                        stat = "white"; 
                    } else
                     if (hsb[2] < 0.1) {
                        stat = "black"; 
                    } else {
                            float deg = hsb[0]*360;
                            if (deg >=   0 && deg <  30) {
                                stat = "red";
                            }
                            else if (deg >=  30 && deg <  90) {
                                stat = "yellow";
                            }
                            else if (deg >=  180 && deg <  240) {
                                stat = "blue";
                            } else {
                                stat = "none";
                            }
                        }
                    writer.write(stat + "\n");
                    writer.flush();

This is used to store detected pieces:

for (int i = 0; i < k; i++) {
        if(pixels[i].equals("red")) {
            board[i] = "X";
        } else {
            board[i] = " ";
        }
        }

Thanks to anyone who can offer suggestions or help!

0

There are 0 best solutions below