# 从上往下扫描找到棋盘的顶点 left = 0
right = 0 num = 0 for i in range(int(h / 3), int(h * 2 / 3)): flag = True first_pixel = img_pixel[0, i] for j in range(board_x_start, board_x_end): pixel = img_pixel[j, i] # 20是色差阈值可以调节 if abs(pixel[0] - first_pixel[0]) + abs(pixel[1] - first_pixel[1]) + abs(pixel[2] - first_pixel[2]) > 10: if flag: left = j right = j flag = False else: right = j num += 1 # print(left, right) ifnot flag: break
board_x = (left + right) // 2 top_point = img_pixel[board_x, i+1] # i+1去掉上面一条白线的bug # 从上顶点往下 + con['hight'] 的位置开始向上找颜色与上顶点一样的点,为下顶点 # if num # # 说明是方形 # if abs(top_point[0] - 255) + abs(top_point[1] - 228) + abs(top_point[2] - 226) # print('唱片图案') # top = 0 # bottom = 0 # for k in range(i, i + con["hight"]): # pixel = img_pixel[board_x, k] # # 根据唱片中的红色部分判断 # # if (155 # # print(pixel[0], pixel[1], pixel[2]) # if (abs(pixel[0] - 239) # # if not top: # top = k # else: # bottom = k # # print(top, bottom) # board_y = (top + bottom) // 2 # return piece_x, piece_y, board_x, board_y
# 该方法对所有纯色平面和部分非纯色平面有效 # print(top_point) for k in range(i + con["hight"], i, -1): pixel = img_pixel[board_x, k] # print(pixel) if
abs(pixel[0] - top_point[0]) + abs(pixel[1] - top_point[1]) + abs(pixel[2] - top_point[2]) 10: break board_y = (i + k) // 2
if num 5: # 去除有些颜色比较多的误差
if k - i 30: print('酱红色433----》》》') board_y += (k - i)
# 去掉药瓶
if top_point[:-1] == (255, 255, 255): print('药瓶图案') board_y = (i + board_y) // 2
# 去掉唱片 if num == 3: if top_point[:-1] == (219, 221, 229): print('唱片') top = 0 bottom = 0 for k in range(i, i + con["hight"]): pixel = img_pixel[board_x, k] # 根据唱片中的红色部分判断 # if (155 # print(pixel[0], pixel[1], pixel[2]) if pixel[:-1] == (118, 118, 118):
ifnot top: top = k else: bottom = k # print(top, bottom) board_y = (top + bottom) // 2 return piece_x, piece_y, board_x, board_y