def search_stones_mask(self, image, threshold): stones = [] smooth = cv.CloneImage(image) cv.Smooth(image, smooth, cv.CV_GAUSSIAN, 5, 5) hsv_img = cv.CreateImage(cv.GetSize(image), 8, 3) cv.CvtColor(smooth, hsv_img, cv.CV_RGB2HSV) masked_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) #FIXME: Illumination has a strong effect in white detection, try to fix # it by tweaking the threshold or reduce contrast in the image trickeryfu = {WHITE: [(0, 0, 191), (180, 255, 255)], BLACK: [(0, 0, 0), (180, 255, 64)]} for k in trickeryfu.keys(): color_range = trickeryfu.get(k) cv.InRangeS(hsv_img, color_range[0], color_range[1], masked_img) circles = self._get_circles(masked_img) for n in range(circles.cols): # TODO: Try to make this less ugly pixel = cv.Get1D(circles, n) pt = (cv.Round(pixel[0]), cv.Round(pixel[1])) radius = cv.Round(pixel[2]) position = Move.pixel_to_position(image.width, pt) stones.append(Move(k, position)) cv.Circle(image, pt, radius, cv.CV_RGB(255, 255, 255) if k == BLACK else cv.CV_RGB(0, 0, 0), 2) return image, stones
def search_stones_simple(self, image, threshold): stones = [] smooth = cv.CloneImage(image) cv.Smooth(image, smooth, cv.CV_GAUSSIAN, 5, 5) hsv_img = cv.CreateImage(cv.GetSize(image), 8, 3) cv.CvtColor(smooth, hsv_img, cv.CV_RGB2HSV) masked_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) trickeryfu = {WHITE: [(0, 0, 191), (180, 255, 255)], BLACK: [(0, 0, 0), (180, 255, 64)]} for k in trickeryfu.keys(): color_range = trickeryfu.get(k) cv.InRangeS(hsv_img, color_range[0], color_range[1], masked_img) storage = cv.CreateMemStorage() contours = cv.FindContours(masked_img, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE, offset=(0, 0)) while contours: # The original idea was to find the centroids. # This seems simpler perimeter = cv.ArcLength(contours) expected_perim = math.pi * image.width / GOBAN_SIZE if (perimeter < 1.1 * expected_perim and perimeter > 0.9 * expected_perim): __, center, radius = cv.MinEnclosingCircle(contours) position = Move.pixel_to_position(image.width, center) stones.append(Move(k, position)) center = tuple(cv.Round(v) for v in center) radius = cv.Round(radius) cv.Circle(image, center, radius, cv.CV_RGB(255, 255, 255) if k == BLACK else cv.CV_RGB(0, 0, 0), 2) contours = contours.h_next() return image, stones
def search_stones_old(self, image, threshold): circles = search_stones(image, None) false_stones = 0 stones = [] for n in range(circles.cols): pixel = cv.Get1D(circles, n) pt = (cv.Round(pixel[0]), cv.Round(pixel[1])) radious = cv.Round(pixel[2]) # Comprobar el color en la imagen color = check_color_stone(pt, radious, image, threshold) position = Move.pixel_to_position(image.width, pixel) if color == BLACK: cv.Circle(image, pt, radious, cv.CV_RGB(255, 0, 0), 2) stones.append(Move(color, position)) elif color == WHITE: cv.Circle(image, pt, radious, cv.CV_RGB(0, 255, 0), 2) stones.append(Move(color, position)) else: #cv.Circle(image, pt, radious, cv.CV_RGB(255,255,0), 2) false_stones += 1 return image, stones
def search_stones_LaB(self,image,th): stones = [] false_stones = 0 circles = search_stones(image, None) lab_img = cv.CreateImage(cv.GetSize(image), 8, 3) cv.CvtColor(image, lab_img, cv.CV_BGR2Lab) for n in range(circles.cols): pixel = cv.Get1D(circles, n) pt = (cv.Round(pixel[0]), cv.Round(pixel[1])) radious = cv.Round(pixel[2]) color = check_color_stone_LaB(pt, radious, lab_img) position = Move.pixel_to_position(image.width, pixel) if color == BLACK: # print "BLACK" cv.Circle(image, pt, radious, cv.CV_RGB(255, 0, 0), 2) stones.append(Move(color, position)) elif color == WHITE: # print "WHITE" cv.Circle(image, pt, radious, cv.CV_RGB(0, 255, 0), 2) stones.append(Move(color, position)) else: false_stones += 1 return image, stones
def search_stones_old(self, image, threshold): circles = search_stones(image) false_stones = 0 stones = [] if circles == None: return image, stones for ci in circles[0,:]: ci = np.rint(ci).astype(np.int) # apply round and int pt = (ci[0], ci[1]) radious = ci[2] # Comprobar el color en la imagen color = check_color_stone(pt, radious, image, threshold) position = Move.pixel_to_position(image.shape[0], pt) if color == BLACK: circle(image, pt, radious, (255, 255, 255), 2) stones.append(Move(color, position)) elif color == WHITE: circle(image, pt, radious, (0, 0, 0), 2) stones.append(Move(color, position)) else: circle(image, pt, radious, (255,0,0), 2) false_stones += 1 return image, stones
def search_stones_LaB(self,image,th): stones = [] false_stones = 0 circles = search_stones(image) lab_img = np.zeros(image.shape[:2], np.uint8) lab_img = cvtColor(image, COLOR_BGR2Lab) for n in range(circles.cols): pixel = Get1D(circles, n) pt = (Round(pixel[0]), Round(pixel[1])) radious = Round(pixel[2]) color = check_color_stone_LaB(pt, radious, lab_img) position = Move.pixel_to_position(image.width, pixel) if color == BLACK: # print("BLACK") circle(image, pt, radious, (255, 0, 0), 2) stones.append(Move(color, position)) elif color == WHITE: # print("WHITE") circle(image, pt, radious, (0, 255, 0), 2) stones.append(Move(color, position)) else: false_stones += 1 return image, stones