python - 在python中旋转图像并去除背景

标签 python python-3.x opencv image-processing computer-vision

enter image description here

有没有办法旋转这些类型的图像并删除背景空白或任何背景并获取像这样的图像

enter image description here

如果图像没有任何旋转,我尝试删除背景,我可以使用此脚本删除背景空白,但如果图像有任何旋转,它不会删除任何空间 我关注了这个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) 

请尝试使用任何扫描图像并旋转它,并尝试消除背景空白并将其旋转到原始尺寸以进行计算机视觉操作

最佳答案

使用cv2.boundingRect将为您提供适合轮廓的最小非旋转矩形。 cv2.boundingRect结果:

enter image description here

您需要使用cv2.minAreaRect来获取适合轮廓的矩形,而不是cv2.boundingRectcv2.minAreaRect结果:

enter image description here

获得旋转后的矩形信息后,需要求模型点与当前点之间的仿射变换矩阵。当前点是在旋转矩形中找到的点,模型点是原始对象的点。在本例中,对象具有初始位置 (0,0) 以及旋转矩形的宽度和高度。

仿射在这里可能有点大材小用,但为了一般性,使用仿射变换。

enter image description here

详细解释位于代码中。

import cv2
import numpy as np

img = cv2.imread('Bcm3h.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]


## This will extract the rotated rect from the contour
rot_rect = cv2.minAreaRect(cnt)

# Extract useful data
cx,cy = (rot_rect[0][0], rot_rect[0][1]) # rect center
sx,sy = (rot_rect[1][0], rot_rect[1][1]) # rect size
angle = rot_rect[2] # rect angle


# Set model points : The original shape
model_pts = np.array([[0,sy],[0,0],[sx,0],[sx,sy]]).astype('int')
# Set detected points : Points on the image
current_pts = cv2.boxPoints(rot_rect).astype('int')

# sort the points to ensure match between model points and current points
ind_model = np.lexsort((model_pts[:,1],model_pts[:,0]))
ind_current = np.lexsort((current_pts[:,1],current_pts[:,0]))

model_pts = np.array([model_pts[i] for i in ind_model])
current_pts = np.array([current_pts[i] for i in ind_current])


# Estimate the transform betwee points
M = cv2.estimateRigidTransform(current_pts,model_pts,True)

# Wrap the image
wrap_gray = cv2.warpAffine(gray, M, (int(sx),int(sy)))


# for display
cv2.imshow("dst",wrap_gray)
cv2.waitKey(0)

#cv2.imwrite("001.png", dst) 

结果:

enter image description here

关于python - 在python中旋转图像并去除背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591102/

相关文章:

python-3.x - python 3.9 : Construct DST valid timestamp using standard library

python - bs4 python 从 <span></span> 提取值到 .csv 一遍又一遍地打印相同的结果

c++ - 编译一个使用Opencv的cpp文件

python-3.x - 在1个视频上输出2个功能

python - 创建相关的 pandas 系列

python - 如何搭建深度学习图像处理服务器

python - 使用 OpenCV 从随机数组在 Python 中创建 RGB 图片

python - 调试 urwid 应用程序的好选择是什么?

opencv - 如何获得自己制造的Haar级联的更高质量或精度

python - Python 中的回文链长度不正确