Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Python OpenCV: Canny Edge Detection for Stickerless Rubik's Cube

Hello,

I've started using OpenCV a couple of weeks ago and thought that building a Rubik's cube solver would be a great first project for me. I've done some research and started to implement the color detection for the Rubik's cube. However, everything online uses a Rubik's cube with stickers. This makes sense because the black borders around the stickers make it very easy for edge detection to detect individual squares. But I am using a stickerless Rubik's cube which means that I am struggling to differentiate between the squares due to there being no obvious border.

colors = {
    'red': ([163, 107,  11], [180, 255, 255]),  # Red
    'blue': ([79, 155, 50], [107, 255, 255]),   # Blue
    'yellow': ([17, 58, 50], [34, 255, 255]),  # Yellow
    'orange': ([5, 130, 50], [20, 255, 255]),  # Orange
    'green': ([68, 86, 50], [89, 255, 255])   # Green
}

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV);
mask = np.zeros(hsv.shape, dtype=np.uint8)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
for color, (lower, upper) in colors.items():
    lower = np.array(lower, dtype=np.uint8)
    upper = np.array(upper, dtype=np.uint8)
    color_mask = cv2.inRange(hsv, lower, upper)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, open_kernel, iterations=1)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_CLOSE, close_kernel, iterations=5)

    color_mask = cv2.merge([color_mask, color_mask, color_mask])
    mask = cv2.bitwise_or(mask, color_mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
hsv = cv2.bitwise_and(hsv, hsv, mask=mask)

canny = cv2.Canny(cv2.GaussianBlur(hsv, (3, 3), 0), 20, 40)

cv2.imshow("Canny Edge", canny);

I've tried playing around with canny thresholds but it just has a lot of trouble with the borders of the squares. Making the edge detection very unstable and unusable.

Does anyone have any tips on how I can improve the edge detection? Should I just buy a cheap Rubik's cube with stickers?

Here is a picture of the edge detection

Thanks.

Python OpenCV: Canny Edge Detection for Stickerless Rubik's Cube

Hello,

I've started using OpenCV a couple of weeks ago and thought that building a Rubik's cube solver would be a great first project for me. I've done some research and started to implement the color detection for the Rubik's cube. However, everything online uses a Rubik's cube with stickers. This makes sense because the black borders around the stickers make it very easy for edge detection to detect individual squares. But I am using a stickerless Rubik's cube which means that I am struggling to differentiate between the squares due to there being no obvious border.

colors = {
    'red': ([163, 107,  11], [180, 255, 255]),  # Red
    'blue': ([79, 155, 50], [107, 255, 255]),   # Blue
    'yellow': ([17, 58, 50], [34, 255, 255]),  # Yellow
    'orange': ([5, 130, 50], [20, 255, 255]),  # Orange
    'green': ([68, 86, 50], [89, 255, 255]),   # Green
    'white': ([0, 0, 50], [180, 37, 255])   # Green
   # White
}

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV);
mask = np.zeros(hsv.shape, dtype=np.uint8)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
for color, (lower, upper) in colors.items():
    lower = np.array(lower, dtype=np.uint8)
    upper = np.array(upper, dtype=np.uint8)
    color_mask = cv2.inRange(hsv, lower, upper)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, open_kernel, iterations=1)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_CLOSE, close_kernel, iterations=5)

    color_mask = cv2.merge([color_mask, color_mask, color_mask])
    mask = cv2.bitwise_or(mask, color_mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
hsv = cv2.bitwise_and(hsv, hsv, mask=mask)

canny = cv2.Canny(cv2.GaussianBlur(hsv, (3, 3), 0), 20, 40)

cv2.imshow("Canny Edge", canny);

I've tried playing around with canny thresholds but it just has a lot of trouble with the borders of the squares. Making the edge detection very unstable and unusable.

Does anyone have any tips on how I can improve the edge detection? Should I just buy a cheap Rubik's cube with stickers?

Here is a picture of the edge detection

Thanks.

Python OpenCV: Canny Edge Detection for Stickerless Rubik's Cube

Hello,

I've started using OpenCV a couple of weeks ago and thought that building a Rubik's cube solver would be a great first project for me. I've done some research and started to implement the color detection for the Rubik's cube. However, everything online uses a Rubik's cube with stickers. This makes sense because the black borders around the stickers make it very easy for edge detection to detect individual squares. But I am using a stickerless Rubik's cube which means that I am struggling to differentiate between the squares due to there being no obvious border.

colors = {
    'red': ([163, 107,  11], [180, 255, 255]),  # Red
    'blue': ([79, 155, 50], [107, 255, 255]),   # Blue
    'yellow': ([17, 58, 50], [34, 255, 255]),  # Yellow
    'orange': ([5, 130, 50], [20, 255, 255]),  # Orange
    'green': ([68, 86, 50], [89, 255, 255]),   # Green
    'white': ([0, 0, 50], [180, 37, 255])      # White
}

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV);
mask = np.zeros(hsv.shape, dtype=np.uint8)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
for color, (lower, upper) in colors.items():
    lower = np.array(lower, dtype=np.uint8)
    upper = np.array(upper, dtype=np.uint8)
    color_mask = cv2.inRange(hsv, lower, upper)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, open_kernel, iterations=1)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_CLOSE, close_kernel, iterations=5)

    color_mask = cv2.merge([color_mask, color_mask, color_mask])
    mask = cv2.bitwise_or(mask, color_mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
hsv = cv2.bitwise_and(hsv, hsv, mask=mask)

canny = cv2.Canny(cv2.GaussianBlur(hsv, (3, 3), 0), 20, 40)

cv2.imshow("Canny Edge", canny);

I've tried playing around with canny thresholds but it just has a lot of trouble with the borders of the squares. Making the edge detection very unstable and unusable.

Does anyone have any tips on how I can improve the edge detection? Should I just buy a cheap Rubik's cube with stickers?

Here is a picture of the edge detectionimage description

Thanks.

Python OpenCV: Canny Edge Detection for Stickerless Rubik's Cube

Hello,

I've started using OpenCV a couple of weeks ago and thought that building a Rubik's cube solver would be a great first project for me. I've done some research and started to implement the color detection for the Rubik's cube. However, everything online uses a Rubik's cube with stickers. This makes sense because the black borders around the stickers make it very easy for edge detection to detect individual squares. But I am using a stickerless Rubik's cube which means that I am struggling to differentiate between the squares due to there being no obvious border.

colors = {
    'red': ([163, 107,  11], [180, 255, 255]),  # Red
    'blue': ([79, 155, 50], [107, 255, 255]),   # Blue
    'yellow': ([17, 58, 50], [34, 255, 255]),  # Yellow
    'orange': ([5, 130, 50], [20, 255, 255]),  # Orange
    'green': ([68, 86, 50], [89, 255, 255]),   # Green
    'white': ([0, 0, 50], [180, 37, 255])      # White
}

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV);
mask = np.zeros(hsv.shape, dtype=np.uint8)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
for color, (lower, upper) in colors.items():
    lower = np.array(lower, dtype=np.uint8)
    upper = np.array(upper, dtype=np.uint8)
    color_mask = cv2.inRange(hsv, lower, upper)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, open_kernel, iterations=1)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_CLOSE, close_kernel, iterations=5)

    color_mask = cv2.merge([color_mask, color_mask, color_mask])
    mask = cv2.bitwise_or(mask, color_mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
hsv = cv2.bitwise_and(hsv, hsv, mask=mask)

canny = cv2.Canny(cv2.GaussianBlur(hsv, (3, 3), 0), 20, 40)

cv2.imshow("Canny Edge", canny);

I've tried playing around with canny thresholds but it just has a lot of trouble with the borders of the squares. Making the edge detection very unstable and unusable.

Does anyone have any tips on how I can improve the edge detection? Should I just buy a cheap Rubik's cube with stickers?

image description

Canny output:

image description

Thanks.

Python OpenCV: Canny Edge Detection for Stickerless Rubik's Cube

Hello,

I've started using OpenCV a couple of weeks ago and thought that building a Rubik's cube solver would be a great first project for me. I've done some research and started to implement the color detection for the Rubik's cube. However, everything online uses a Rubik's cube with stickers. This makes sense because the black borders around the stickers make it very easy for edge detection to detect individual squares. But I am using a stickerless Rubik's cube which means that I am struggling to differentiate between the squares due to there being no obvious border.

colors = {
    'red': ([163, 107,  11], [180, 255, 255]),  # Red
    'blue': ([79, 155, 50], [107, 255, 255]),   # Blue
    'yellow': ([17, 58, 50], [34, 255, 255]),  # Yellow
    'orange': ([5, 130, 50], [20, 255, 255]),  # Orange
    'green': ([68, 86, 50], [89, 255, 255]),   # Green
    'white': ([0, 0, 50], [180, 37, 255])      # White
}

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV);
mask = np.zeros(hsv.shape, dtype=np.uint8)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
for color, (lower, upper) in colors.items():
    lower = np.array(lower, dtype=np.uint8)
    upper = np.array(upper, dtype=np.uint8)
    color_mask = cv2.inRange(hsv, lower, upper)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_OPEN, open_kernel, iterations=1)
    color_mask = cv2.morphologyEx(color_mask, cv2.MORPH_CLOSE, close_kernel, iterations=5)

    color_mask = cv2.merge([color_mask, color_mask, color_mask])
    mask = cv2.bitwise_or(mask, color_mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
hsv = cv2.bitwise_and(hsv, hsv, mask=mask)

canny = cv2.Canny(cv2.GaussianBlur(hsv, (3, 3), 0), 20, 40)

cv2.imshow("Canny Edge", canny);

I've tried playing around with canny thresholds but it just has a lot of trouble with the borders of the squares. Making the edge detection very unstable and unusable.

Does anyone have any tips on how I can improve the edge detection? Should I just buy a cheap Rubik's cube with stickers?

image description

Canny output:

image description

Thanks.