在python中旋转图像并移除背景

2024-09-28 22:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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) 

请尝试与任何扫描的图像,旋转它,并尝试摆脱背景空白,旋转到它的原始尺寸进行计算机视觉操作


Tags: andto图像importimgthresholdcv2kernel
3条回答

考虑到你不知道旋转的角度,而且每个扫描的图像都可能不同,你需要先找到它。在

将您已经做的与this问题的已接受答案结合起来。在

对于您提供的图像:

Angle is -25.953375702364195

enter image description here

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

enter image description here

您将需要使用cv2.boundingRect,而不是使用cv2.minareRect来获得一个适合轮廓的矩形。cv2.minarerect结果:

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

如果保证背景是饱和的白色(值255),而文档主要是非饱和值,则在阈值255以下进行二值化并拟合一个边框。在

相关问题 更多 >