Сканер документов NID для сканирования только необходимого прямоугольникаPython

Программы на Python
Гость
Сканер документов NID для сканирования только необходимого прямоугольника

Сообщение Гость »


enter image description here I just want to crop the inner white rectangle out of the image using python. I tried it with findContours, but as the edges are not clear its not showing any better output. how to do this. can anybody help me. I am new to it.

import cv2 import numpy as np
Load the image
image = cv2.imread("doc.jpg")
Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Apply binary thresholding
_, thresholded = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)

cv2.imshow("thresholded", thresholded)
Find contours
contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Filter contours
rects = [] for contour in contours: # Approximate the contour to a polygon polygon = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)

Код: Выделить всё

# Check if the polygon has 4 sides and the aspect ratio is close to 1  if len(polygon) == 4 and abs(1 - cv2.contourArea(polygon) / (cv2.boundingRect(polygon)[2] * cv2.boundingRect(polygon)[3])) < 0.1:      rects.append(polygon)  
Draw rectangles for rect in rects: cv2.drawContours(image, [rect], 0, (0, 255, 0), 2)
Show the result
cv2.imshow("Rectangles", image) cv2.waitKey(0) cv2.destroyAllWindows() i tried this but didnt work.


Источник: https://stackoverflow.com/questions/781 ... -rectangle

Вернуться в «Python»