# Reading Rubik's Cube states into computer using Computer Vision magic

##### Member
I thought some people might be interested: here are some results from my Computer Vision project:

It dealt with a better/faster way to read in a state of a rubiks cube so that you can enter any particular configuration into solvers quickly, or just share some configuration with other people.

Implemented in Python and OpenCV library.

I haven't quite figured out what I can port the resulting configuration to yet

Also the code is NOT online, but I may post it sometime later... for now just sharing it with everyone!
EDIT: ah what the hell here is the code and my report:
http://www.cs.ubc.ca/~andrejk/525project/

Last edited:

#### lilkdub503

##### Member
So sick! How long did it take you to write the code? Oh, and have you started cubing again seriously (if so, what are you averaging)?

#### Chapuunka

##### Member
I could see this being on every computer with a webcam pretty soon. That would be pretty cool.

I also saw your videos about the evolutions of simple organisms. That was actually pretty fascinating.

#### riffz

##### Member
Something I've always wondered about: Why not read in 3 faces at a time?
Might be tough with the bad lighting at an angle.

Last edited:

##### Member
Lucas: good idea.
I suspect that it would suffer from lighting problems as riffz suggested.
It would also be harder to align it exactly toward the camera in that way and much harder to hold it in a way as to not cause occlusions.

The detection itself is also not to be trusted TOO much. It is more reliable if it is ok to get it slightly wrong, which is the case for a single face, but most likely not for 3 faces. Not enough wiggle.

And yes riffz thats the channel I usually post my programming stuff on. I just need to sometimes share results of some of my programs with other people and thats mostly what i use it for. In this case that someone was my prof for the class

#### bamboocha

##### Member
Sorry for me being a noob but how do I run this program? I downloaded Python 2.6 and OpenCV 2.1 with bindings and numpy library.

When I try to run it I get an error:

Code:
Traceback (most recent call last):
File "C:\folders\cubefinder.py", line 2, in <module>
import cv
ImportError: No module named cv

##### Member
Sorry for me being a noob but how do I run this program? I downloaded Python 2.6 and OpenCV 2.1 with bindings and numpy library.

When I try to run it I get an error:

Code:
Traceback (most recent call last):
File "C:\folders\cubefinder.py", line 2, in <module>
import cv
ImportError: No module named cv
Pretty simple, OpenCV install didn't go right. Check out their install directions more closely. When I installed it they required me to copy the cv.pyd files into Python/Lib/site-packages folder but maybe they have a more automatic way in their newer install

#### Crosshash

##### Member
Very interesting. Are you going to elaborate on this code some more?

I'm wondering if the question you asked in the other thread was related to making sure all the colours matched up correctly so you could solve the cube based on colour input only. Would be very interesting to see it.

I wish I could read your code more easily but I've never used Python before (i study Java), was the reason for using an edge detector in order to identify the cube in the image?

#### MrIndianTeen

##### Member
when I try the link it says --

Sorry, something went wrong.

A team of highly trained monkeys has been dispatched to deal with this situation.

If you see them, show them this information:

[edit: lots of chars were here]

Last edited by a moderator:

#### PatrickJameson

I was going to delete the above two posts, infract, and move on but this gave me a nice little project for the next three days(or less, depending on how long it takes and how far I go with it) since I have have a slight break from robotics. I've been dealing with a bit of object tracking stuff for robotics already. Should be fun. I'll probably just use Java and v4l4j since I used it for the linked video already.

#### Herbert Kociemba

##### Member
This thread is dead since 2011 but since I are also do some experiments with Python and OpenCV I decided to revive it instead of creating a new thread. The link http://www.cs.ubc.ca/~andrejk/525project/ from above also is dead and I want to ask if anybody here has a copy of the code or a working link because I am interested in the code.

With my approach I put a grid on a video frame (gridsize about 25x35) and compute the average hue values and the variance of the hue values in a grid-square. Squares with low variances are candidates for a square which is situated completely inside a cube facelet. Omitting some details in the end I have I binary mask which I search for squares using the openCV findContours and approxPolyDP functions. This works best for cubes where the facelets are well separated of course. In this way I can detect all facelets within the picture.

The color detection itself I also use the hsv-values, h to decide the color of all non-white facelets, white faclets are detected by low s values and high v values. Distingushing between red and orange in advance is probably impossible, it depends on the colors of the actual cube and to some extend also on the light source. At least you have to know the hue range fo an orange sticker.
Here is an example: http://kociemba.org/videos/cuberec.mp4

I plan to integrate the computer vision part into my Python cubesolver project https://github.com/hkociemba/RubiksCube-TwophaseSolver as soon as possible.

#### abunickabhi

##### Member
This thread is dead since 2011 but since I are also do some experiments with Python and OpenCV I decided to revive it instead of creating a new thread. The link http://www.cs.ubc.ca/~andrejk/525project/ from above also is dead and I want to ask if anybody here has a copy of the code or a working link because I am interested in the code.

With my approach I put a grid on a video frame (gridsize about 25x35) and compute the average hue values and the variance of the hue values in a grid-square. Squares with low variances are candidates for a square which is situated completely inside a cube facelet. Omitting some details in the end I have I binary mask which I search for squares using the openCV findContours and approxPolyDP functions. This works best for cubes where the facelets are well separated of course. In this way I can detect all facelets within the picture.

The color detection itself I also use the hsv-values, h to decide the color of all non-white facelets, white faclets are detected by low s values and high v values. Distingushing between red and orange in advance is probably impossible, it depends on the colors of the actual cube and to some extend also on the light source. At least you have to know the hue range fo an orange sticker.
Here is an example: http://kociemba.org/videos/cuberec.mp4

I plan to integrate the computer vision part into my Python cubesolver project https://github.com/hkociemba/RubiksCube-TwophaseSolver as soon as possible.
Good initiative and all the best!

#### Herbert Kociemba

##### Member
Faster than I thought I was able to integrate the computervision part. The files are uploaded to Github now. Here is an example how the parts work together:

http://kociemba.org/videos/visiongui.avi

Yet some critical explanations are missing how to handle the sliders for the filters which is quite essential to achieve a good recognition rate...