【发布时间】:2020-04-22 18:10:28
【问题描述】:
有没有办法旋转这些类型的图像并删除背景空白或任何背景并获得这样的图像
如果图像没有任何旋转,我尝试删除背景我可以使用此脚本删除背景空白,但如果图像有任何旋转,它不会删除任何空间 我关注了这个How to crop or remove white background from an image
import cv2
import numpy as np
img = cv2.imread('cheque_img\rotate.PNG')
## (1) Convert to gray, and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
## (2) Morph-op to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
## (3) Find the max-area contour
cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cnt = sorted(cnts, key=cv2.contourArea)[-1]
## (4) Crop and save it
x,y,w,h = cv2.boundingRect(cnt)
dst = img[y:y+h, x:x+w]
cv2.imwrite("001.png", dst)
请尝试使用任何扫描的图像并旋转它,并尝试摆脱背景空白并将其旋转到其原始尺寸以进行计算机视觉操作
【问题讨论】:
-
@VardanAgarwal 问题是,不同图片的图片坐标可能不同,
-
我猜你应该问的问题是“我怎样才能找到文档角落的坐标”。剩下的就是轻松的工作。
-
@YvesDaoust 我怎样才能找到动态图像的坐标,是的!
标签: python python-3.x opencv image-processing computer-vision