opencv 标记有数字的区域
Opencv 数字 区域 标记
2023-09-14 09:09:30 时间
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 12:42:15 2019
@author: lg
"""
import cv2
import numpy as np
# 形态学处理
def Process(img):
# 高斯平滑
gaussian = cv2.GaussianBlur(img, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
# 中值滤波
median = cv2.medianBlur(gaussian, 5)
# Sobel算子
# 梯度方向: x
sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)
# 二值化
ret, binary = cv2.threshold(sobel, 170, 255, cv2.THRESH_BINARY)
# 核函数
element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 7))
# 膨胀
dilation = cv2.dilate(binary, element2, iterations=1)
# 腐蚀
erosion = cv2.erode(dilation, element1, iterations=1)
# 膨胀
dilation2 = cv2.dilate(erosion, element2, iterations=3)
return dilation2
def GetRegion(img):
regions = []
# 查找轮廓
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if (area < 5000):
continue
eps = 1e-3 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, eps, True)
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
ratio =float(width) / float(height)
if (ratio < 5 and ratio > 1.8):
regions.append(box)
return regions
def detect(img):
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
prc = Process(gray)
regions = GetRegion(prc)
# imgt= cv2.imread('yzz.jpg')
print('[INFO]:Detect %d license plates' % len(regions))
for box in regions:
cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
cv2.imshow('Result', img)
#保存结果文件名
cv2.imwrite('result2.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
#输入的参数为图片的路径
img = cv2.imread('red.jpg')
detect(img)
相关文章
- 基于opencv+Dlib的面部合成(Face Morph)
- Android版OpenCV图像处理技术亲自验证[三十四]之Scharr运算符(附源码)
- OpenCV每日函数 杂项图像转换模块 (5) integral函数 积分图
- OpenCV每日函数 Object Detection目标检测模块 (4) 基于 DNN 的人脸检测和识别
- Opencv学习笔记 - 使用opencvsharp和决策树进行训练和预测
- 数字图像处理 使用opencv+python识别七段数码显示器的数字
- 机器学习笔记 基于tensorflow2.0的手写数字识别,并导出pb模型供OpenCV的C++版本的DNN模块调用
- opencv 通过颜色提取显示屏数字
- opencv 采用 matplot 显示图片
- 在OpenCV里学习常见问题汇编22
- 在OpenCV里使用SVM识别手写数字
- Opencv项目实战:20 单手识别数字0到5
- 【OpenCV 例程200篇】82. 频率域巴特沃斯低通滤波器
- 【youcans 的 OpenCV 例程200篇】188.基于拉普拉斯金字塔的图像融合
- 使用 OpenCV 和 Python 识别数字
- 【OpenCV】Chapter10.色彩转换与图像绘制