opencv图像条形码区域识别检测(python)
程序员文章站
2022-08-06 20:32:52
opencv学习—图像条形码区域检测(python)目录opencv学习—图像条形码区域检测(python)一、检测方法流程如下二、各个流程步骤讲解三、代码以及结果鸣谢一、检测方法流程如下二、各个流程步骤讲解包含二维码原图像原图像灰度化img=cv2.imread("C:\\Users\\Lijian\\Desktop\\test.jpg",cv2.IMREAD_GRAYSCALE)黑帽操作kernel = np.ones((1, 3.....
opencv图像条形码区域检测(python)
目录
一、检测方法流程如下
二、各个流程步骤讲解
包含二维码原图像
原图像灰度化
img=cv2.imread("C:\\Users\\Lijian\\Desktop\\test.jpg",cv2.IMREAD_GRAYSCALE)
黑帽操作
kernel = np.ones((1, 3), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, anchor=(1, 0))
阈值分割
thresh, img = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY)
形态学运算调整区域
kernel = np.ones((1, 5), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_DILATE, kernel, anchor=(2, 0), iterations=2)
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, anchor=(2, 0), iterations=2)
kernel = np.ones((21, 35), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)
区域检测算法(检测区域,过滤噪声,绘制区域)
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
unscale = 1.0 / scale
if contours != None:
for contour in contours:
if cv2.contourArea(contour) <= 2000:
continue
rect = cv2.minAreaRect(contour)
rect = \
((int(rect[0][0] * unscale), int(rect[0][1] * unscale)), \
(int(rect[1][0] * unscale), int(rect[1][1] * unscale)), \
rect[2])
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(img_out, [box], 0, (0, 255, 0), thickness = 2)
三、代码以及结果
import cv2
import matplotlib.pyplot as plt
import numpy as np
cv2.__version__
img=cv2.imread("C:\\Users\\Lijian\\Desktop\\test.jpg",cv2.IMREAD_GRAYSCALE)
img_out=cv2.imread("C:\\Users\\Lijian\\Desktop\\test.jpg")
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyWindow
scale=800/img.shape[1]
img=cv2.resize(img,(int(img.shape[1]*scale),int(img.shape[0]*scale)))
cv2.imshow('img1',img)
cv2.waitKey(0);
cv2.destroyWindow
#blackhat
kernel = np.ones((1, 3), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, anchor=(1, 0))
#sogliatura
thresh, img = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY)
#operazioni morfologiche
kernel = np.ones((1, 5), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_DILATE, kernel, anchor=(2, 0), iterations=2) #dilatazione
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, anchor=(2, 0), iterations=2) #chiusura
kernel = np.ones((21, 35), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)
cv2.imshow('img1',img)
cv2.waitKey(0);
cv2.destroyWindow
#estrazione dei componenti connessi
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
unscale = 1.0 / scale
if contours != None:
for contour in contours:
# se l'area non è grande a sufficienza la salto
if cv2.contourArea(contour) <= 2000:
continue
#estraggo il rettangolo di area minima (in formato (centro_x, centro_y), (width, height), angolo)
rect = cv2.minAreaRect(contour)
#l'effetto della riscalatura iniziale deve essere eliminato dalle coordinate rilevate
rect = \
((int(rect[0][0] * unscale), int(rect[0][1] * unscale)), \
(int(rect[1][0] * unscale), int(rect[1][1] * unscale)), \
rect[2])
#disegno il tutto sull'immagine originale
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(img_out, [box], 0, (0, 255, 0), thickness = 2)
cv2.imshow('img1',img_out)
cv2.waitKey(0);
cv2.destroyWindow
原图
结果图像
鸣谢
本文地址:https://blog.csdn.net/weixin_44690935/article/details/109028599
推荐阅读
-
基于Python+OpenCV的人脸口罩识别检测
-
Python Opencv实现图像轮廓识别功能
-
python opencv实现图像边缘检测
-
python用opencv批量截取图像指定区域的方法
-
人脸检测识别opencv+python实现(Haar分类器)
-
基于jupyter notebook的python编程(Win10通过OpenCv-3.4.1进行人脸口罩数据集的模型训练并进行戴口罩识别检测)
-
python-opencv-人脸识别-图像处理
-
Python+OpenCV 图像边缘检测四种实现方法
-
Python+Opencv身份证号码区域提取及识别实现
-
opencv图像条形码区域识别检测(python)