This is a Set game soler using OpenCV.
This code is a result of me paying set, staring at the 12 cards and not finding any set, Thinking are there really no sets on the table or it is me that is really bad. So I have decided to create an app that will solve the problem for me.
- At the moment I need to do a lot of code cleanup.
A set deck has 81 cards, varying in four features:
- number (one, two, or three)
- symbol (diamond, squiggle, oval)
- shading (solid, striped, or open)
- color (red, green, or purple).
A set consists of three cards which in each feature is either the same or different among the three cards
- Image contains 12 cards on a dark background.
- Convert RGB image to grayscale.
- Threshold grayscale image to binary image.
- Find all contours in binary image.
- Find 12 contours that are about the same size and their size is greater than
X%
of the image. - Cut each contour and send it to card analysis.
- Image contains only one card.
- The card background is all most white.
1.Convert RGB image to grayscale. 2. Threshold grayscale image to binary image. 3. Find all contours in binary image.
- The Number is determined by the number of contours.
- Convert RGB image to HSV.
- Mask the HSV image with a shape contours, this way we don't count background pixels.
- The card color is determined by the number of pixels in each color range (red, green, or purple).
- Crop one contour.
- Calculate Hu invariant moments for the contour - rotation and scale invariant
- The symbol is determined by Hu moment
$\phi_1$
$$
\phi_1 = \mu_{2,0}+\mu_{0,2}
$$
Where
Hu moment $ \phi_1 $ is analogous to the moment of inertia around the image's centroid, this tell us how much the shape is spread.
Finding the shading has proved to be the most difficult task.
- Crop one contour.
- The Shading is determined by the ratio of pixels to the area of a full contour.
- solid - high ratio
- striped - mid
- open - very low ratio
Another option:
- Run an edge detection on the binary image
- calculate the ratio
- solid - only one line - very low ratio
- striped - high ratio
- open - 2 lines, inner and outer, low ratio
Now when we found all of features for each cards we can look for sets. One way to do it is brute force (I'm lazy so for the moment I'm brute forcing)
An Android app is on the way.