Skip to content

Instantly share code, notes, and snippets.

@erickdsama
Created February 24, 2018 09:56
Show Gist options
  • Select an option

  • Save erickdsama/d31319fc8bfa6b5f30c981733e043a8c to your computer and use it in GitHub Desktop.

Select an option

Save erickdsama/d31319fc8bfa6b5f30c981733e043a8c to your computer and use it in GitHub Desktop.
import cv2
from pylibdmtx.pylibdmtx import decode
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import imutils
cap = cv2.VideoCapture(0)
id = 1
def filtros_imagen(img):
# capa 1
well = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
# capa 2
harris = cv2.cornerHarris(well,15, 5,0.04)
# capa 3
x, thr = cv2.threshold(harris, 0.1 * harris.max(), 255, cv2.THRESH_BINARY)
thr = thr.astype('uint8')
#obtener contornos
dst, contours, hierarchy = cv2.findContours(thr.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
areas = map(lambda x: cv2.contourArea(cv2.convexHull(x)), contours)
# obtiene el area mayor
max_i = areas.index(max(areas))
rect =cv2.minAreaRect(contours[max_i])
x, y, width, height = cv2.boundingRect(contours[max_i])
# box = cv2.boxPoints(rect)
# box = np.int0(box)
img = img[y:y+height, x:x+width]
return img, {"width": width, "height": height}
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
id += 1
# frame.save()
cv2.imshow('frame',frame)
# cv2.imwrite("frame%d.jpg" % id, frame)
img, data_image = filtros_imagen(frame)
width = data_image.get("width")
height = data_image.get("height")
if width > 200 and height > 200 and (width - height) < 50:
deco = decode(img)
if len(deco) > 0:
print deco
break
# if img is None:
# cv2.imshow('frame',frame)
# else:
# cv2.imshow('frame',img)
# img = cv2.imread("qr2.png")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment