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

标签 python opencv image-processing deep-learning

This is the image having 3 barcodes[![][1] ] 2以下 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/

相关文章:

python - OpenCV HSV 值根据屏幕/相机上的位置而变化

image-processing - 为什么 ImageMagick 生成的 '.png' 文件比 '.jpg' 和 '.gif' 文件大得多?

python - 向重写方法添加关键字参数并使用 **kwarg

python - 如何使用 python 脚本中的值来设置环境变量

迭代集合/剩余集合的 pythonic 方法

python - 属性错误 : module 'cv2.cv2' has no attribute 'createLBPHFaceRecognizer'

python - 我的编码有什么问题? 'AttributeError ' 模块'对象没有属性 'VideoWriter fourcc''

Python 3.x : parse ATOM XML and convert to dict

c++ - 无法使用鼠标单击在图像上绘制多条线

c++ - OpenCV filter2D : Is there any use of kernels not anchored at center?