Python识别图片条形码并框选

感谢http://blog.jobbole.com/80448/ 提供参考,并将他的代码转到Pycharm中

 

import numpy as np
import cv2

# load the image and convert it to grayscale
image = cv2.imread('./pic/cde.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# compute the Scharr gradient magnitude representation of the images
# in both the x and y direction
gradX = cv2.Sobel(gray, ddepth = cv2.cv2.CV_32F, dx = 1, dy = 0, ksize = -1)
gradY = cv2.Sobel(gray, ddepth = cv2.cv2.CV_32F, dx = 0, dy = 1, ksize = -1)

# subtract the y-gradient from the x-gradient
gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient)

# blur and threshold the image
blurred = cv2.blur(gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)

# construct a closing kernel and apply it to the thresholded image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations = 4)
closed = cv2.dilate(closed, None, iterations = 4)

# find the contours in the thresholded image, then sort the contours
# by their area, keeping only the largest one
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]

# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))

# draw a bounding box arounded the detected barcode and display the
# image
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)

转载于:https://www.cnblogs.com/qiuya/p/10853392.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python中的第三方库来实现批量识别条形码并保存到Excel文件中。一个常用的库是`pyzbar`,它可以用于解码条形码。另外,你还需要使用`openpyxl`库来操作Excel文件。 下面是一个简单的示例代码,可以帮助你完成这个任务: ```python import os import cv2 import pyzbar.pyzbar as pyzbar from openpyxl import Workbook def decode_barcodes(image): barcodes = pyzbar.decode(image) results = [] for barcode in barcodes: barcode_data = barcode.data.decode("utf-8") results.append(barcode_data) return results def save_to_excel(data, output_file): wb = Workbook() sheet = wb.active for i, barcode in enumerate(data, 1): sheet.cell(row=i, column=1, value=barcode) wb.save(output_file) def batch_process_images(input_folder, output_file): barcodes = [] for filename in os.listdir(input_folder): if filename.endswith(".jpg") or filename.endswith(".png"): img_path = os.path.join(input_folder, filename) image = cv2.imread(img_path, 0) decoded_barcodes = decode_barcodes(image) barcodes.extend(decoded_barcodes) save_to_excel(barcodes, output_file) # 设置输入文件夹和输出文件路径 input_folder = "path/to/input/folder" output_file = "path/to/output/file.xlsx" # 执行批量处理 batch_process_images(input_folder, output_file) ``` 你需要将示例代码中的`input_folder`和`output_file`变量替换为你自己的路径。该代码会遍历指定的输入文件夹中的所有图片文件(支持`.jpg`和`.png`格式),识别条形码,并将结果保存到指定的Excel文件中。 请确保在运行代码之前,已经安装了所需的库:`pyzbar`、`opencv-python`和`openpyxl`。你可以使用以下命令来安装它们: ```bash pip install pyzbar opencv-python openpyxl ``` 希望这可以帮助到你!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值