图像预处理
将拍摄到的原始图像预处理为识别的图像,这个阶段需要对原始图像中的数字物体进行定位、切割等操作。
import cv2
import numpy as np
import sys
import os
def position(pic):
arr = []
# 复制图像,防止图像被更改
img = pic.copy()
# 将RGB转换为HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([20, 43, 46])
upper_red = np.array([40, 255, 255])
# 获取每一个像素点的HSV值,将黄色像素点转换为黑色像素点,其余为白色
img = cv2.inRange(img_hsv, lower_red, upper_red)
ret, img=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# 进行闭操作
kenel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 5))
close = cv2.morphologyEx(img, cv2.MORPH_OPEN, kenel, iterations=1)
# 查找轮廓,只检查外轮廓
binary,contours,hierarchy = cv2.findContours(\
close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
cnt = contours[i]
# 计算该轮廓的面积
area = cv2.contourArea(cnt)
# 面积小的都筛选掉
if (area < 1800):
continue
# 轮廓近似,作用很小
epsilon = 0.001 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
# 找到最小的矩形
rect = cv2.minAreaRect(cnt)
# box是4个点的坐标
box = cv2.boxPoints(rect)
box = np.int0(box)
# 计算高和宽
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
# 物体正常情况下的长宽比在0.5~1.5之间
ratio =float(width) / float(height)
if (ratio > 1.5 or ratio < 0.5):
continue
# 得到上、下边的坐标
box = cv2.boxPoints(rect)
box = np.int0(box)
ys = [box[0, 1], box[1, 1], box[2, 1], box[3, 1]]
xs = [box[0, 0], box[1, 0], box[2, 0], box[3, 0]]
ys_sorted_index = np.argsort(ys)
xs_sorted_index = np.argsort(xs)
if box[xs_sorted_index[0], 0] > 0:
x1 = box[xs_sorted_index[0], 0]
else:
x1 = 0
if box[xs_sorted_index[3], 0] > 0:
x2 = box[xs_sorted_index[3], 0]
else:
x2 = 0
if box[ys_sorted_index[0], 1] > 0:
y1 = box[ys_sorted_index[0], 1]
else:
y1 = 0
if box[ys_sorted_index[3], 1] > 0:
y2 = box[ys_sorted_index[3], 1]
else:
y2 = 0
img_plate = binary[y1:y2, x1:x2]
arr.append(y1)
arr.append(y2)
arr.append(x1)
arr.append(x2)
return arr
得到4个物体在原始图像上的坐标后,将切割后的图像放入神经网络中就可以开始预测过程。