欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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)

目录

opencv学习—图像条形码区域检测(python)

一、检测方法流程如下

二、各个流程步骤讲解

三、代码以及结果

鸣谢


一、检测方法流程如下

opencv图像条形码区域识别检测(python)

二、各个流程步骤讲解

包含二维码原图像

opencv图像条形码区域识别检测(python)

原图像灰度化

img=cv2.imread("C:\\Users\\Lijian\\Desktop\\test.jpg",cv2.IMREAD_GRAYSCALE) 

opencv图像条形码区域识别检测(python)

黑帽操作

kernel = np.ones((1, 3), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, anchor=(1, 0))

opencv图像条形码区域识别检测(python)

阈值分割

thresh, img = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY)

opencv图像条形码区域识别检测(python)

形态学运算调整区域

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)

opencv图像条形码区域识别检测(python)

区域检测算法(检测区域,过滤噪声,绘制区域)

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) 

opencv图像条形码区域识别检测(python)

三、代码以及结果

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

原图

opencv图像条形码区域识别检测(python)

结果图像

opencv图像条形码区域识别检测(python)

鸣谢

基于OpenCV的条形码区域分割

本文地址:https://blog.csdn.net/weixin_44690935/article/details/109028599

相关标签: opencv图像 opencv