top of page
Computer Sketch

Computer Vision

We used a camera which gives an eye to the robot. The objective of the camera is to recognize the move of the opponent.
The camera is controlled via OpenCV-python running on laptop.
If you are a beginner in OpenCV, you can go through the link below to learn some basics of OpenCV before starting the next section.

Computer Vision: Services

Warp Perspective

As the camera is fixed just above the Chessboard, Image is good enough to start.

The raw image is converted to 800*800px image by warping the perspective(by Opencv function)  so the chess board squares become equal in size.

Learn about warping perspective click here

​

Code :

pts1 = np.float32([[68, 17], [642, 12], [59, 584], [638, 599]])

pts2 = np.float32([[0, 0], [800, 0], [0, 800], [800, 800]])

matrix = cv2.getPerspectiveTransform(pts1, pts2)

​

def perspective_img (img):

     result = cv2.warpPerspective(img,   matrix, (800, 800))

     return result

​

​

Computer Robot
Computer Vision: Text

Gaussian Blur

Before going into mask creation , we have to blur the perspective transformed image , and the gaussian blur function is the best option in OpenCV.

Gaussian blur removes the noise from image making it more clear to detect the pieces. read more about Gaussian blur.

​

Code :

perspective_result_blurred = cv2.GaussianBlur(perspective_result_img1, (41, 41),0)

Computer Vision: Text
Computer Sketch
Computer Vision: Image

Creating Mask

After getting the Perspective transformed blurred image , we need to apply the color_mask function to detect the pieces of opponent(Human).

Depending on the color of  opponent piece you can set set the upper and lower limit of HSV value for color detection.

The mask will look like the image provided below.

​

Code:

def color_mask(img):

      hsv = cv2.cvtColor(img,    cv2.COLOR_BGR2HSV)

      lower_color_range = np.array([18, 50, 100])

      upper_color_range = np.array([38, 200, 255])

      mask= cv2.inRange(hsv,  lower_color_range,  

      upper_color_range)

      mask = cv2.erode(mask, (101, 101), iterations=1)

       mask = cv2.dilate(mask, (141, 141), iterations=1)

       return mask

​

Computer Vision: Text
Computer Sketch
Computer Vision: Image

Contour Function

This function is very important  as far as the detection is concerned.

​

Code :

def get_center(contour_arg):

        M = cv2.moments(contour_arg)

       if M['m00'] != 0:

            cx = int(M['m10']/M['m00'])

            cy = int(M['m01']/M['m00'])

            return (cx,cy)

      else :

           return (-1,-1)

​

contour_area_list = []

​

def contour_func(mask):

       contour, hierarchy = cv2.findContours(mask, cv2.RETR_LIST,             cv2.CHAIN_APPROX_SIMPLE)

      for i in range(len(contour)):

           a = cv2.contourArea(contour[i])

           c = get_center(contour[i])

           contour_area_list.append( (i,a,c))

     contour_area_list.sort(key= lambda x : x[1],reverse=True)

##############################

So there is a inbuilt function in OpenCV named as findContours which takes mask-image(binary image) and will find contours in it.

then we made a list which contains Area and Center of each contours.

For getting center of contour we are using get_center function.

Read more about contours.

​

​

​

​

Computer Vision: Text

Finding opponent's move

Comparing center of each contour with range of each square on chessboard through range_function, we'll get that exact square on which that particular piece is located. 

We do the same process for all contours to get the position of all pieces and store the position in a list.

The list is state of opponent's pieces , we are saving the current and previous state of opponent and from difference function we'll find the move of opponent.

​

Check these functions in the complete code, the code is well commented so you will understand easily .

​

​

​

Computer Vision: Text

Complete code of Robot.

File name is sunfish.py

Computer Vision: Files

©2019 by checkmates

bottom of page