def img_mser(self, filename): if type(filename) == type(""): img = img_math.img_read(filename) else: img = filename oldimg = img mser = cv2.MSER_create(_min_area=600) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) regions, boxes = mser.detectRegions(gray) colors_img = [] for box in boxes: x, y, w, h = box width, height = w, h if width < height: width, height = height, width ration = width / height if w * h > 1500 and 3 < ration < 4 and w > h: cropimg = img[y:y + h, x:x + w] colors_img.append(cropimg) debug.img_show(img) colors, car_imgs = img_math.img_color(colors_img) for i, color in enumerate(colors): if color != "no": print(color) debug.img_show(car_imgs[i])
def img_only_color(self, filename, oldimg, img_contours): """ :param filename: 图像文件 :param oldimg: 原图像文件 :return: 已经定位好的车牌 """ pic_hight, pic_width = img_contours.shape[:2] lower_blue = np.array([100, 110, 110]) upper_blue = np.array([130, 255, 255]) lower_yellow = np.array([15, 55, 55]) upper_yellow = np.array([50, 255, 255]) hsv = cv2.cvtColor(filename, cv2.COLOR_BGR2HSV) mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) output = cv2.bitwise_and(hsv, hsv, mask=mask_blue + mask_yellow) # 根据阈值找到对应颜色 output = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY) Matrix = np.ones((20, 20), np.uint8) img_edge1 = cv2.morphologyEx(output, cv2.MORPH_CLOSE, Matrix) img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix) debug.img_show(img_edge2) card_contours = img_math.img_findContours(img_edge2, oldimg) card_imgs = img_math.img_Transform(card_contours, oldimg, pic_width, pic_hight) colors, car_imgs = img_math.img_color(card_imgs)
def img_Transform(car_contours, oldimg, pic_width, pic_hight): car_imgs = [] for car_rect in car_contours: if car_rect[2] > -1 and car_rect[2] < 1: angle = 1 # 对于角度为-1 1之间时,默认为1 else: angle = car_rect[2] car_rect = (car_rect[0], (car_rect[1][0] + 5, car_rect[1][1] + 5), angle) box = cv2.boxPoints(car_rect) heigth_point = right_point = [0, 0] left_point = low_point = [pic_width, pic_hight] for point in box: if left_point[0] > point[0]: left_point = point if low_point[1] > point[1]: low_point = point if heigth_point[1] < point[1]: heigth_point = point if right_point[0] < point[0]: right_point = point if left_point[1] <= right_point[1]: # 正角度 new_right_point = [right_point[0], heigth_point[1]] pts2 = np.float32([left_point, heigth_point, new_right_point]) # 字符只是高度需要改变 pts1 = np.float32([left_point, heigth_point, right_point]) M = cv2.getAffineTransform(pts1, pts2) dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight)) point_limit(new_right_point) point_limit(heigth_point) point_limit(left_point) car_img = dst[int(left_point[1]):int(heigth_point[1]), int(left_point[0]):int(new_right_point[0])] car_imgs.append(car_img) debug.img_show(car_img) elif left_point[1] > right_point[1]: # 负角度 new_left_point = [left_point[0], heigth_point[1]] pts2 = np.float32([new_left_point, heigth_point, right_point]) # 字符只是高度需要改变 pts1 = np.float32([left_point, heigth_point, right_point]) M = cv2.getAffineTransform(pts1, pts2) dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight)) point_limit(right_point) point_limit(heigth_point) point_limit(new_left_point) car_img = dst[int(right_point[1]):int(heigth_point[1]), int(new_left_point[0]):int(right_point[0])] car_imgs.append(car_img) debug.img_show(car_img) return car_imgs
def img_findContours(img_contours, oldimg): img, contours, hierarchy = cv2.findContours(img_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area] print("findContours len = ", len(contours)) # 排除面积最小的点 debug.img_show(img) car_contours = [] for cnt in contours: ant = cv2.minAreaRect(cnt) width, height = ant[1] if width < height: width, height = height, width ration = width / height print(ration) if ration > 2 and ration < 5.5: car_contours.append(ant) box = cv2.boxPoints(ant) box = np.int0(box) debug.img_contours(oldimg, box) return car_contours
def show_img_pre(self): filename = config.get_name() if filename.any() == True: debug.img_show(filename)