毕业设计 python opencv实现车牌识别 预处理

主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506

GitHub:https://github.com/yinghualuowu

首先我们需要一个函数可以随时获取图片,无论在什么地方

filename = askopenfilename(title="选择识别图片", filetypes=[("jpg图片", "*.jpg"),("png图片","*.png")])
def img_read(filename):
    return  cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR)

另外我们需要考虑图片大小

pic_hight,pic_width = img.shape[:2]
if pic_width > MAX_WIDTH:
resize_rate = MAX_WIDTH / pic_width
img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate)), interpolation=cv2.INTER_AREA)
#缩小图片

进行形态学操作,以下步骤并不是必选项,根据方法来选取一些预处理步骤

blur = 3
img = cv2.GaussianBlur(img,(blur,blur),0)
oldimg = img
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#转化成灰度图像

Matrix = np.ones((20, 20), np.uint8)
img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, Matrix)
img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0)
#创建20*20的元素为1的矩阵 开操作,并和img重合

ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_edge = cv2.Canny(img_thresh, 100, 200)
#Otsu’s二值化 找到图像边缘

Matrix = np.ones((4, 19), np.uint8)
img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, Matrix)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix)

比如下面这张进行预处理后,就变成

 

 

 

转载于:https://www.cnblogs.com/yinghualuowu/p/8846890.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值