def capture(self): ''' Function to capture the images and give them the names according to their captured time and date. ''' camera = self.ids['camera'] timestr = time.strftime("%Y%m%d_%H%M%S") camera.export_to_png("Doc{}.png".format(timestr)) print("Captured") image = cv2.imread("Doc{}.png".format(timestr)) # read in the image image = cv2.resize( image, (1300, 800) ) # resizing because opencv does not work well with bigger images orig = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # RGB To Gray Scale cv2.imshow("Title", gray) blurred = cv2.GaussianBlur( gray, (5, 5), 0 ) # (5,5) is the kernel size and 0 is sigma that determines the amount of blur cv2.imshow("Blur", blurred) edged = cv2.Canny(blurred, 30, 50) # 30 MinThreshold and 50 is the MaxThreshold cv2.imshow("Canny", edged) contours, hierarchy = cv2.findContours( edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE ) # retrieve the contours as a list, with simple apprximation model contours = sorted(contours, key=cv2.contourArea, reverse=True) # the loop extracts the boundary contours of the page for c in contours: p = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * p, True) if len(approx) == 4: target = approx break approx = mapper.mapp(target) # find endpoints of the sheet pts = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) # map to 800*800 target window op = cv2.getPerspectiveTransform( approx, pts) # get the top or bird eye view effect dst = cv2.warpPerspective(orig, op, (800, 800)) cv2.imshow("Scanned", dst) cv2.waitKey(0) cv2.destroyAllWindows()
def Scanner(path): img_path = os.path.expanduser(path) img = cv2.imread(img_path, 1) img = cv2.resize(img, (600, 600)) # cv2.imshow('original',img) orig = img.copy() gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convertig into gray image #cv2.imshow('gray',gray_img) blur_img = cv2.GaussianBlur(gray_img, (5, 5), 0) #converting into blurred image #cv2.imshow('blur',blur_img) edged_img = cv2.Canny(blur_img, 30, 50) #detecting edges #cv2.imshow('edged',edged_img) contours, hierarchy = cv2.findContours(edged_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 255, 0), 1) contours = sorted(contours, key=cv2.contourArea, reverse=True) for c in contours: p = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * p, True) if len(approx) == 4: target = approx break approx = mapper.mapp(target) pts = np.float32([[0, 0], [600, 0], [600, 600], [0, 600]]) op = cv2.getPerspectiveTransform(approx, pts) dst = cv2.warpPerspective(orig, op, (600, 600)) # cv2.imshow('final',dst) #b&w imgg = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) scanned_image = cv2.adaptiveThreshold(imgg, 255, 1, 1, 7, 2) scanned_image = cv2.bitwise_not(scanned_image) scanned_image = cv2.medianBlur(scanned_image, 3) # cv2.imshow("scanned output",scanned_image) return scanned_image
cv2.imshow('Title', gray) kernel = np.ones((1, 1), np.uint8) gray = cv2.dilate(gray, kernel, iterations=1) gray = cv2.erode(gray, kernel, iterations=1) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #cv2.imshow('Blurred',blurred) edged = cv2.Canny(blurred, 30, 50) #cv2.imshow('Canny',edged) contours, heirarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=cv2.contourArea, reverse=True) for c in contours: p = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * p, True) if len(approx) == 4: target = approx break approx = mapper.mapp(target) pts = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) op = cv2.getPerspectiveTransform(approx, pts) dst = cv2.warpPerspective(ori, op, (800, 800)) cv2.imshow('Scanned Copy', dst) cv2.waitKey(0)
50) # 30 MinThreshold and 50 is the MaxThreshold cv2.imshow("Canny", edged) contours, hierarchy = cv2.findContours( edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # retrieve the contours as a list, with simple contours = sorted(contours, key=cv2.contourArea, reverse=True) # the loop extracts the boundary contours of the page for c in contours: p = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * p, True) if len(approx) == 4: target = approx break approx = mapper.mapp(target) # find endpoints of the sheet pts = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) # map to 800*800 target window op = cv2.getPerspectiveTransform(approx, pts) # get the top or bird eye view effect dst = cv2.warpPerspective(orig, op, (800, 800)) cv2.imshow("Scanned", dst) cv2.waitKey(0) cv2.destroyAllWindows()
input = cv2.resize(input, (1300, 800)) copy = input.copy() #converting to Gray-Scale grayscale = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY) #applying GaussianBlur gaussian = cv2.GaussianBlur(grayscale, (5, 5), 0) #applying Canny-Edge-Detection Technique cannyedge = cv2.Canny(gaussian, 30, 50) contours, hierarchy = cv2.findContours(cannyedge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=cv2.contourArea, reverse=True) for i in contours: x = cv2.arcLength(i, True) approx = cv2.approxPolyDP(i, 0.02 * x, True) if len(approx) == 4: a = approx break approx = mapper.mapp(a) window = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) top_view = cv2.getPerspectiveTransform(approx, window) wrap = cv2.warpPerspective(copy, top_view, (800, 800)) cv2.imshow("Scanned Image", wrap)
def scanPicture(nameOfPicture): image = cv2.imread(nameOfPicture) image = cv2.resize(image, (1500, 880)) # creating copy of original image orig = image.copy() # convert to grayscale and blur to smooth gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #blurred = cv2.medianBlur(gray, 5) # apply Canny Edge Detection edged = cv2.Canny(blurred, 0, 50) orig_edged = edged.copy() # find the contours in the edged image, keeping only the # largest ones, and initialize the screen contour (contours, _) = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) contours = sorted(contours, key=cv2.contourArea, reverse=True) #x,y,w,h = cv2.boundingRect(contours[0]) #cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),0) # get approximate contour for c in contours: p = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * p, True) if len(approx) == 4: target = approx break # mapping target points to 800x800 quadrilateral approx = mapper.mapp(target) pts2 = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]]) M = cv2.getPerspectiveTransform(approx, pts2) dst = cv2.warpPerspective(orig, M, (800, 800)) cv2.drawContours(image, [target], -1, (0, 255, 0), 2) dst = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) # using thresholding on warped image to get scanned effect (If Required) ret, th1 = cv2.threshold(dst, 127, 255, cv2.THRESH_BINARY) th2 = cv2.adaptiveThreshold(dst,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,11,2) th3 = cv2.adaptiveThreshold(dst,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,11,2) ret2, th4 = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # cv2.imshow("Original.jpg", orig) # cv2.imshow("Original Gray.jpg", gray) # cv2.imshow("Original Blurred.jpg", blurred) # cv2.imshow("Original Edged.jpg", orig_edged) # cv2.imshow("Outline.jpg", image) # cv2.imshow("Thresh Binary.jpg", th1) # cv2.imshow("Thresh mean.jpg", th2) # cv2.imshow("Thresh gauss.jpg", th3) # cv2.imshow("Otsu's.jpg", th4) # cv2.imshow("dst.jpg", dst) # cv2.waitKey(0) return dst