gpt4 book ai didi

python - 如何在图像中查找多个条形码

转载 作者:行者123 更新时间:2023-12-02 16:13:59 25 4
gpt4 key购买 nike

This is the image having 3 barcodes[![][1]以下的python代码在图像中仅找到一个条形码。我需要查找一个图像中存在的多个条形码,对您的帮助将不胜感激。提前致谢。

import numpy as np
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to the image file")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1)

gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient)

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


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
closed = cv2.erode(closed, None, iterations = 4)
closed = cv2.dilate(closed, None, iterations = 4)

cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
print(len(cnts))
#c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
c = max(cnts, key = cv2.contourArea)

rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
box = np.int0(box)

cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)

最佳答案

我最重要的假设是条形码是水平对齐的。
find_rectangles是从OpenCv Squares example修改而来的。我们使用它来获取候选轮廓。然后将轮廓分组,按x顺序处理。仅当当前轮廓足够接近,中心具有相似的高度并且与该组最后添加的位置具有相似的高度时,我们才能添加到组中。

最后,我们检查每组中最小的轮廓区域,并估计作为该区域的钢筋数量占总组面积的比例。我们将丢弃所有小于10条的组。

我们剩下应该是条形码的组,剩下的就是在原始图像上绘制矩形。

import cv2
import math
import numpy as np
from google.colab.patches import cv2_imshow

def get_center(contour):
M = cv2.moments(contour)
cX = int(M["m10"] / max(M["m00"], 1e-6))
cY = int(M["m01"] / max(M["m00"], 1e-6))

return cX, cY

def find_rectangles(img):
filtered = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
img = cv2.GaussianBlur(img, (5, 5), 0)
for gray in cv2.split(img):
for thrs in range(50, 200, 1):
_retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
contours, h = cv2.findContours(~bin, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
contours = [contours[i] for i in range(len(contours)) if h[0][i][3] == -1]
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
poly = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
w, h = cv2.minAreaRect(cnt)[1]
if len(poly) <= 8 and cv2.contourArea(cnt) > 10 and cv2.contourArea(poly) < 1000 and (h / w) > 5:
cv2.drawContours(filtered, [cnt], -1, 255, -1)

return filtered

def dist(p1, p2):
return math.sqrt((p1[0] - p2[0]) **2 + (p1[1] - p2[1]) ** 2)

def findBarCodes(image):

thresh = find_rectangles(image)
contours, h = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted([contours[i] for i in range(len(contours)) if h[0][i][3] == -1], key = lambda x: cv2.boundingRect(x)[0])

groups = []

for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
center_1 = get_center(cnt)
found = False
if w * h > 50 and w * h < thresh.shape[0] * thresh.shape[1] / 2:
for group in groups:
x2, y2, w2, h2 = cv2.boundingRect(group[-1])
center_2 = get_center(group[-1])
if abs(center_1[1] - center_2[1]) < 20 and (abs(h - h2) / max(h, h2)) < 0.3 and any(map(lambda p: dist((x, y), p) < 20, [(x2, y2), (x2 + w2, y2), (x2, y2 + h2), (x2 + w2, y2 + h2)])):
group.append(cnt)
found = True
break
if not found:
groups.append([cnt])

for group in groups[:]:
mn = 1000000
total = 0
for c in group:
x, y, w, h = cv2.boundingRect(c)
total += w * h
mn = min(mn, w * h)
estimatedBars = total / mn
if estimatedBars < 10:
groups.remove(group)

for idx, group in enumerate(groups):
boxes = []
for c in group:
x, y, w, h = cv2.boundingRect(c)
boxes.append([x,y, x+w,y+h])
cv2.rectangle(thresh, (x,y), (x+w,y+h), 255, 2)
cv2.putText(thresh, str(idx), (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, 255, 2)
cv2.drawContours(thresh, [c], -1, 128, -1)

boxes = np.asarray(boxes)

left = np.min(boxes[:,0])
top = np.min(boxes[:,1])
right = np.max(boxes[:,2])
bottom = np.max(boxes[:,3])

cv2.rectangle(image, (left,top), (right,bottom), 255, 2)


cv2_imshow(image)
cv2_imshow(thresh)

findBarCodes(cv2.imread('tQp93.jpg'))

结果:
enter image description here
enter image description here

关于python - 如何在图像中查找多个条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59835022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com