小编Kav*_*ena的帖子

使用python检测边缘后如何将图像裁剪成碎片

我正在研究一个残缺的文档重建项目。首先,我尝试检测包含撕裂的文档碎片的图像边缘,然后尝试使用示例代码通过检测到的边缘将图像裁剪为碎片,

import cv2
import numpy as np
img = cv2.imread("test.png")
img = cv2.imread("d:/test.jpeg")

cv2.imshow('Original Image',img)

new_img = cv2.Canny(img, 0, 505)
cv2.imshow('new image', new_img)

blurred = cv2.blur(new_img, (3,3))
canny = cv2.Canny(blurred, 50, 200)

## find the non-zero min-max coords of canny
pts = np.argwhere(canny>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)

## crop the region
cropped = new_img[y1:y2, x1:x2]
cv2.imwrite("cropped.png", cropped)

tagged = cv2.rectangle(new_img.copy(), (x1,y1), (x2,y2), (0,255,0), 3, cv2.LINE_AA)
cv2.imshow("tagged", tagged)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)

我的输入图像是 在此处输入图片说明

运行上面的代码后,我得到类似的输出 在此处输入图片说明

有人可以帮助我裁剪破损的文档并将其分配给变量

python opencv image-processing

3
推荐指数
1
解决办法
231
查看次数

标签 统计

image-processing ×1

opencv ×1

python ×1