def perspective_transform(image, points, ratio = None): # print( "print(ing points in finalize " + str(points) height, width = image.shape[0], image.shape[1] if ratio: points = points.reshape(4,2) * ratio else: points = points.reshape(4,2) # points = imutils.boundary_coords(points, width, height) warped = imutils.four_point_transform(image = image.copy(), pts = points) # for use with minAreaRect resul return warped
def finalize(image, points, ratio): print "printing points in finalize " + str(points) warped = imutils.four_point_transform( image=image.copy(), pts=points.reshape(4, 2) * ratio) # for use with minAreaRect result gray = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) gaussian_threshold = cv2.adaptiveThreshold(src = blur, maxValue = 25, adaptiveMethod = cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ thresholdType = cv2.THRESH_BINARY, blockSize = 11, C = 2) return gaussian_threshold
def perspective_transform(image, points, ratio=None): # print( "print(ing points in finalize " + str(points) points = np.array( list(points) ) # added this 4/17/18 to keep the idea of points, corners as tuples consistent. height, width = image.shape[0], image.shape[1] if ratio: points = points.reshape(4, 2) * ratio else: points = points.reshape(4, 2) # points = imutils.boundary_coords(points, width, height) warped = imutils.four_point_transform( image=image.copy(), pts=points) # for use with minAreaRect resul return warped
contours = sorted( contours, key=cv2.contourArea, reverse=True)[:5] # added the [:5] per the kickass scanner example rect = cv2.minAreaRect(contours[0]) # print rect box = cv2.boxPoints(rect) print box box = np.int0(box) # img = np.zeros((3500,3500,3), np.uint8) # detected = cv2.drawContours(image.copy(),[box],0,(0,255,0),1) # <----- WARPING AND THRESHOLDING -------> # warped = imutils.four_point_transform( image=orig, pts=target.reshape(4, 2) * ratio) # this is the original. Works on sample4.jpg # warped = imutils.four_point_transform(image = orig, pts = box.reshape(4, 2) * ratio) # for use with minAreaRect result # warped = imutils.four_point_transform(image = orig.copy(), pts = box.reshape(4, 2) * ratio) # for use with minAreaRect result # convert the warped image to grayscale, then threshold it warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) warped = threshold_adaptive(warped, 251, offset=10) warped = warped.astype("uint8") * 255 images = [detected, orig.copy(), warped] functions.plot_images(images)
src = [] N = (0, 0) tempL = getVertices(contours, top, slope, tempL) tempM = getVertices(contours, right, slope, tempM) tempO = getVertices(contours, bottom, slope, tempO) L = updateCornerOr(orientation, tempL) M = updateCornerOr(orientation, tempM) O = updateCornerOr(orientation, tempO) iflag, N = getIntersection(M[1], M[2], O[3], O[2], N) src.append(L[0]) src.append(M[1]) src.append(N) src.append(O[3]) src = np.asarray(src, np.float32) warped = four_point_transform(img, src) #cv2.imshow("warped",warped) cv2.circle(img, N, 1, (0, 0, 255), 2) cv2.drawContours(img, contours, top, (255, 0, 0), 2) cv2.drawContours(img, contours, right, (0, 255, 0), 2) cv2.drawContours(img, contours, bottom, (0, 0, 255), 2) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) cv2.imwrite('userTest.png', warped) file_path = 'userTest.png' try: with open(file_path, 'rb') as image_file: image = Image.open(image_file) image.load() codes = zbarlight.scan_codes('qrcode', image) #print('QR codes: %s' % codes)