Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

paper edge detection and perspective transform

$
0
0
before image https://imgur.com/f190UFk processed image https://imgur.com/JkEhWkS you can see the "processed image" has highlight, so the transform works bad. any possible to make an rectangle that ignore that highlight area? import os import cv2 import numpy as np from nanoid import generate def processImage(imagepath, ext): img = cv2.imread(imagepath) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) # _, threshed = cv2.threshold(s, 50, 255, cv2.THRESH_BINARY_INV) _, threshed = cv2.threshold(s, 50, 255, cv2.THRESH_BINARY_INV) # cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] canvas = img.copy() #cv2.drawContours(canvas, cnts, -1, (0, 255, 0), 1) cnts = sorted(cnts, key = cv2.contourArea) cnt = cnts[-1] print(cnt) arclen = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.005 * arclen, True) cv2.drawContours(canvas, [cnt], -1, (255, 0, 0), 5, cv2.LINE_AA) cv2.drawContours(canvas, [approx], -1, (0, 0, 255), 1, cv2.LINE_AA) print(approx) approx = rectify(approx) pts2 = np.float32([[0, 0], [2480, 0], [2480, 3508], [0, 3508]]) M = cv2.getPerspectiveTransform(approx, pts2) dst = cv2.warpPerspective(canvas, M, (2480, 3508)) filename_output = generate() + ext cv2.imwrite('./static/' + filename_output, dst) topLeft, topRight, bottomRight, bottomLeft = approx topLeft = topLeft.tolist() topRight = topRight.tolist() bottomRight = bottomRight.tolist() bottomLeft = bottomLeft.tolist() return { 'filename': './static/' + filename_output, 'shape': img.shape, 'approx': { 'topLeft': topLeft, 'topRight': topLeft, 'bottomRight': bottomRight, 'bottomLeft': bottomLeft, }, } def rectify(h): h = h.reshape((13, 2)) hnew = np.zeros((4, 2), dtype = np.float32) add = h.sum(1) hnew[0] = h[np.argmin(add)] hnew[2] = h[np.argmax(add)] diff = np.diff(h, axis = 1) hnew[1] = h[np.argmin(diff)] hnew[3] = h[np.argmax(diff)] return hnew add similar condition image https://imgur.com/wDXtLsd https://imgur.com/KAvOtdG

Viewing all articles
Browse latest Browse all 41027

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>