【问题标题】:How to find the multiple barcodes in an image如何在图像中找到多个条形码
【发布时间】:2020-05-07 03:43:40
【问题描述】:

]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)

【问题讨论】:

  • 发布您的输入和输出图像,以便其他人可以测试您的代码。
  • 另外,如果您说明源代码,当您从其他地方复制粘贴代码时会很好:github.com/sayands/opencv-implementations/blob/master/…
  • 对不起,您链接的来源相同,github.com/sayands/opencv-implementations/blob/master/…
  • 现在,你能帮我解决这个问题吗?
  • 如果你有多个轮廓,你需要遍历你的轮廓。我建议您显示您的关闭图像,以查看您的阈值是否已删除所有其他阈值或仅将其变为一个。为什么你会得到渐变边缘?为什么不只是水平模糊或使用水平形态学内核将条形码连接到一个连接区域。

标签: python opencv image-processing deep-learning


【解决方案1】:

我做的最重要的假设是条码是水平对齐的。

find_rectanglesOpenCv 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'))

结果:

【讨论】:

    【解决方案2】:

    你需要有更好的形态学运算,并且对连通分量的大小有一个阈值。这是我对您的代码所做的更改以捕获所有条形码:

    import numpy as np
    #import argparse
    import imutils
    import cv2
    
    image = cv2.imread('D:/1.jpg')
    
    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))
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5))
    blurred = cv2.erode(blurred, kernel, iterations = 4)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 1))
    blurred = cv2.dilate(blurred, kernel, iterations = 4)
    
    (_, thresh) = cv2.threshold(blurred, 230, 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)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 5))
    closed = cv2.dilate(closed, kernel, iterations = 4)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
    closed = cv2.erode(closed, kernel, iterations = 2)
    
    cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    cnts = imutils.grab_contours(cnts)
    
    for i in range(len(cnts)):
        if cv2.contourArea(cnts[i]) > 2000:
            cv2.drawContours(image, cnts, i, (0, 255, 0), 3)
    
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    【讨论】:

    • @SiddanthShaiva 您应该更改参数(如内核大小或阈值)以获得最佳效果。我没有其他图像,因此无法调整这些值。
    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2021-12-28
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多