2

I am using OpenCV to dynamically detect barcodes on licenses. The cv2 method getStructingElement creates a rectangle around the barcode. How can I add padding to all sides of the barcode borders? The contour is fit too tightly on the barcode, such that I am losing data from the edges. The barcode is in pdf417 format, which is a 2D barcode.

Tightly fit Detected Barcode: Detected Barcode

# import the necessary packages
import numpy as np
import imutils
import cv2

# load the image and convert it to grayscale
image = cv2.imread("image.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

# compute the Scharr gradient magnitude representation of the images
# in both the x and y direction using OpenCV 2.4
ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1)
gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, 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,(27, 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)
cnts = imutils.grab_contours(cnts)
c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
# print(c)

# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
box = np.int0(box)

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

# draw a bounding box 
min_y = int(np.min(box[:,-1]))
max_y = int(np.max(box[:,-1]))
min_x = int(np.min(box[:,0]))
max_x = int(np.max(box[:,0]))
image = image[min_y:max_y, min_x:max_x] 

cv2.imshow("Image", image)
cv2.waitKey(0)

Test Image: Test image

  • 1
    The solution seems to be simple. I think you are not getting an answer because you didn't post a sample image (make the problem reproducible) – Rotem Aug 11 '21 at 07:57
  • @Rotem Thank you for the advice. I will update with test image – nowYouSeeMe007 Aug 16 '21 at 16:42
  • 1
    You may use something like: `(topy, topx) = (np.min(box[:,1]), np.min(box[:,0]))`, `(bottomy, bottomx) = (np.max(box[:,1]), np.max(box[:,0]))`, Then subtract `n` from `topy`, `topx` and add `n` to `bottomy`, `bottomx`. Create new `box` (in the correct order). – Rotem Aug 16 '21 at 20:59
  • @Rotem I implemented something similar after "cv2.drawContours": min_y = int(np.min(box[:,-1])) max_y = int(np.max(box[:,-1])) min_x = int(np.min(box[:,0])) max_x = int(np.max(box[:,0])) image = image[min_y:max_y, min_x:max_x] – nowYouSeeMe007 Aug 16 '21 at 22:10
  • 1
    Could you clarify what 'n" is in this context. I have similar code but I want to see if your method yields different results. – nowYouSeeMe007 Aug 16 '21 at 23:57
  • 1
    `n` is the size of the padding (in pixels). Try `n = 10` for example, and check the padding size. – Rotem Aug 17 '21 at 20:37
  • @Rotem nowYouSeeMe007 is going to be away from office for a while. I don't know how to direct connect with people. If you do, please reach out to me and I'll hire you to finish making the software related to this question work. – Cyphryx Aug 19 '21 at 20:48
  • @Cyphryx Sorry, but I am not interested. – Rotem Aug 20 '21 at 09:53

0 Answers0