Python OpenCV : Rubiks cube solver color extraction
Question: **Description:**

I am working on solving rubiks cube using Python & OpenCV. For this purpose I
am trying to extract all the colors of the cubies(individual cube pieces) and
then applying appropriate algorithm(which I've designed, no issues there).

**The problem:**

Suppose if I've extracted all the colors of the cubies, how I can locate the
position of the extracted cubies. Like how I'll know whether it is in top-
middle-lower layer or whether its a corner-middle-edge piece.

**What I've done:**

Here I have just extracted yellow color.

**_After color extraction:_**

![enter image description here](http://i.stack.imgur.com/x298k.jpg)

**_Original Image_**

![enter image description here](http://i.stack.imgur.com/OYFN2.png)

**_The Code_**

    
    
    import numpy as np
    import cv2
    from cv2 import *
    
    im = cv2.imread('v123.bmp')
    im = cv2.bilateralFilter(im,9,75,75)
    im = cv2.fastNlMeansDenoisingColored(im,None,10,10,7,21)
    hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)   # HSV image
    
    
    COLOR_MIN = np.array([20, 100, 100],np.uint8)       # HSV color code lower and upper bounds
    COLOR_MAX = np.array([30, 255, 255],np.uint8)       # color yellow 
    
    frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)     # Thresholding image
    imgray = frame_threshed
    ret,thresh = cv2.threshold(frame_threshed,127,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    print type(contours)
    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        print x,
        print y
        cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
    cv2.imshow("Show",im)
    cv2.imwrite("extracted.jpg", im)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

Please give some suggestions on how can I locate the positions of the cubies.
Here 4 yellow cubies are spotted: top-right-corner, center, right-edge,
bottom-left-corner. How can I identify these positions for eg: by assigning
digits to each position (here: 3, 4, 5, 7)

Any help/idea is appreciated :) Thanks.

P.S.: OpenCV newbie :)


Answer: Here's the original code and location of the found yellow squares.

## source

    
    
    import numpy as np
    
    import sys; sys.path.append('/usr/lib/pyshared/python2.7')
    
    import cv2
    from cv2 import *
    
    im = cv2.imread('rubik.png')
    im = cv2.bilateralFilter(im,9,75,75)
    im = cv2.fastNlMeansDenoisingColored(im,None,10,10,7,21)
    hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)   # HSV image
    
    
    COLOR_MIN = np.array([20, 100, 100],np.uint8)       # HSV color code lower and upper bounds
    COLOR_MAX = np.array([30, 255, 255],np.uint8)       # color yellow 
    
    frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)     # Thresholding image
    imgray = frame_threshed
    ret,thresh = cv2.threshold(frame_threshed,127,255,0)
    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # print type(contours)
    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        print x,y
        cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
    cv2.imshow("Show",im)
    cv2.imwrite("extracted.jpg", im)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

## output

    
    
    185 307
    185 189
    307 185
    431 55
    

