How I made straightforward 3*3 Rubik cube solver using OpenCV python.
I choose this project precisely because it sounds easy to accomplish.
But, after some days I realized that the keyword “sound” in above sentence
is skeptical. During this project I face some problem regrading with
color extraction of Rubik cube cubies* from webcam.
Therefore I am writing this article to share what I have learned in the
process.
The project was coded using python and uses the OpenCV library.
OpenCV is a library of programming functions mainly aimed at
real-time computer vision.
To better describe I went through the project and how I extract
each cubies color from webcam.
The steps are following
1. color extraction.
I didn’t use object detection to detect Rubik cube and then
extract color. I went through basic OpenCV operation and draw a
cube like below.
For drawing the cube, you can use OpenCV python function
cv2.line(image, start point, end point, color, thickness)
So, now we have fix position to put Rubik cube and we can extract
color from particular position. Now it’s easy to extract color from this area. Sounds easy, Right?? Can you try this? So, now go and try.
I am Just kidding….
But, now problem is how can we comprehend the every color accurately. like, yellow at 1st place red at 2nd place, etc. So, I crop and save, each cubies into 9 different image according to there pixel position. Now, we will get 9 different images for each face like below.
For this I simply use slicing the image.
Now, we are all set just need to extract r, g, b values for each 9 images. For this apply white balancing in image and find average value of red, blue and yellow color. The white balance is an algorithm that adjust the color intensity of an image. It is used to improve photos with whitish, orange, bluish or greenish tones.
use following code for white balancing the image
Now the last step is to compare the r,g,b values with color max and color min r,g,b value and according with r,g,b find color of cubbies. Firstly, find the r,g,b values for each color according to with your webcam. Might be it varying from camera to camera.
It is simple or not?
very simple, am I right?…
Now we done with color extraction, you complete 80% of project work.
2. Use kociemba module and find solution.
The python has Rubik cube solver pypi library, So by using kociemba you will get all Rubik cube solution steps. Now just need to represent that steps on webcam image.
use following function to represent steps,
cv2.arrowedLine(image, start point, end point, color, thickness)
In improvement we can do automatic Rubik cube detection and then do all this steps.
Use following source code for better understanding,
https://github.com/rutujapadgilwar06/rubik-s-cube-solver
and watch solving demo.
Thanks a Lot!! Live long and prosper!!!