某牛人要求我做一个骰子识别,加了好几次需求,终于初见成果
效果图如下:
原图如下:
代码如下:
import cv2
import numpy as np
import skimage
from PIL import ImageFont, ImageDraw, Image
#设置字体
fontpath = "font/simsun.ttc"
font = ImageFont.truetype(fontpath, 128)
path = r'./t1.jpg'
image = cv2.imread(path,0)
def image_threshold(image):
# image = cv2.GaussianBlur(image,(3,3),0,0,cv2.BORDER_DEFAULT)
# image = cv2.medianBlur(image,5)
image = cv2.threshold(image,180,255,cv2.THRESH_BINARY)
return image[1]
def get_contours(image):
_,contours,h = cv2.findContours(image,0,1)
element = cv2.getStructuringElement(cv2.MORPH_RECT,(9,9))
need_list = []
for contour in contours:
img = np.zeros(image.shape)
area = cv2.contourArea(contour)
if (area>200):
contour = cv2.convexHull(contour)
cv2.fillConvexPoly(img,contour,color=1)
result = image*img
result = cv2.erode(result,element,iterations=1)
result = result.astype(np.uint8)
_,cons,i = cv2.findContours(result,cv2.RETR_CCOMP,1)
num = 0
for con in cons:
con_area = cv2.contourArea(con)
if (con_area>10):
num = num+1
print(num-1)
#提取周长
eps = 1e-3 * cv2.arcLength(contour,True)
#拟合多边形
approx = cv2.approxPolyDP(contour,eps,True)
#提取矩形
rect = cv2.minAreaRect(approx)
#提取矩形坐标
box = cv2.boxPoints(rect)
box = np.int0(box)
print(box)
need = {'h_box':box[0][0],'w_box':box[3][1],'num':str(num-1)}
need_list.append(need)
#在图片中写字
# image_pil = Image.fromarray(image)
# draw = ImageDraw.Draw(image_pil)
# #绘制文字信息
# draw.text((box[0][0],box[3][1]),str(num-1), font = font, fill = 0)
# result = np.array(image_pil)
# result = result.astype(np.uint8)
#获取图片高宽
# box_min_h = min([box[0][0],box[1][0],box[2][0],box[3][0]])
# box_max_h = max([box[0][0],box[1][0],box[2][0],box[3][0]])
# box_min_w = min([box[0][1],box[1][1],box[2][1],box[3][1]])
# box_max_w = max([box[0][1],box[1][1],box[2][1],box[3][1]])
# height = box_max_h - box_min_h
# width = box_max_w - box_min_w
# image_map = np.zeros((width,height))
# image_map = result[box_min_w:box_max_w,box_min_h:box_max_h]
num = 0
image_pil = Image.fromarray(image)
draw = ImageDraw.Draw(image_pil)
print(need_list)
draw.text((need_list[0]['h_box'],need_list[0]['w_box']),need_list[0]['num'], font = font, fill = 125)
draw.text((need_list[1]['h_box'],need_list[1]['w_box']),need_list[1]['num'], font = font, fill = 125)
draw.text((need_list[2]['h_box'],need_list[2]['w_box']),need_list[2]['num'], font = font, fill = 125)
draw.text((need_list[3]['h_box'],need_list[3]['w_box']),need_list[3]['num'], font = font, fill = 125)
draw.text((need_list[4]['h_box'],need_list[4]['w_box']),need_list[4]['num'], font = font, fill = 125)
draw.text((need_list[5]['h_box'],need_list[5]['w_box']),need_list[5]['num'], font = font, fill = 125)
draw.text((need_list[6]['h_box'],need_list[6]['w_box']),need_list[6]['num'], font = font, fill = 125)
count_list = [0,0,0,0,0,0,0]
for need in need_list:
print(need['num'])
count_list[int(need['num'])] += 1
string = ''
for index,count_num in enumerate(count_list):
if (index==0):
continue
string = string + str(index)+'is'+str(count_num)+';'
draw.text((100,100),string, font = font, fill = 125)
image = np.array(image_pil)
image = image.astype(np.uint8)
show_image(image)
def show_image(image,num=0):
name = 'num is '+str(num)
cv2.namedWindow(name,0)
cv2.resizeWindow(name,600,600)
cv2.imshow(name,image)
cv2.waitKey(5000)
cv2.destroyAllWindows()
image = image_threshold(image)
get_contours(image)
#show_image(image)