AL-2020/coder/rocognizer.py

37 lines
1.1 KiB
Python
Raw Normal View History

import numpy as np
2020-05-20 11:45:55 +02:00
import argparse
import imutils
import cv2
import matplotlib.pyplot as plt
img = cv2.cvtColor(cv2.imread('barcode.jpg'), cv2.COLOR_BGR2GRAY)
ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
X = cv2.Sobel(img, ddepth=ddepth, dx=1, dy=0, ksize=-1)
Y = cv2.Sobel(img, ddepth=ddepth, dx=0, dy=1, ksize=-1)
gradient = cv2.subtract(X, Y)
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)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
rect = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
box = np.int0(box)
2020-05-20 11:45:55 +02:00
cv2.drawContours(img, [box], -1, (0, 255, 0), 3)
cv2.imshow("Image", img)
cv2.waitKey(0)
2020-05-20 11:45:55 +02:00
plt.imshow(closed ,cmap='binary')
plt.show()