使用 python 删除图像中的空白

发布于 01-13 13:46 字数 839 浏览 3 评论 0原文

有多个图像有空白,我需要删除它们。只需裁剪图像即可消除空白 这是我到目前为止尝试过的代码(这是搜索的结果)

import numpy as np
import cv2

img = cv2.imread('Sample.png')
img = img[:-5,:-5] 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, np.ones((2, 2), dtype=np.uint8))
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
rect = img[y:y+h, x:x+w] 
cv2.imshow("Cropped", rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Output.png", rect)

这是示例图像

在此处输入图像描述

这是所需的输出

在此处输入图像描述

There are multiple images that have white spaces that I need to remove. Simply crop the image so as to get rid of the white spaces
Here's the code I tried so far (this is a result of search)

import numpy as np
import cv2

img = cv2.imread('Sample.png')
img = img[:-5,:-5] 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, np.ones((2, 2), dtype=np.uint8))
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
rect = img[y:y+h, x:x+w] 
cv2.imshow("Cropped", rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("Output.png", rect)

Here's the sample image

enter image description here

And this is the desired output

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯2025-01-20 13:46:45

代码几乎是完美的。由于滚动条,它无法在右侧裁剪。并且它不考虑一些填充(您根据评论喜欢它)。

我删除的是拓扑修改。

import numpy as np
import cv2

img = cv2.imread('cropwhitefromimage.png')
scrollbarright = 20
img = img[:, :-scrollbarright]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
padding = 10
rect = img[y-padding:y+h+padding, x-padding:x+w+padding]
cv2.imshow("Cropped", rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("cropwhitefromimage_result.png", rect)

The code is almost perfect. It just can't crop on the right side because of the scrollbar. And it does not consider some padding (which you liked according the comments).

The thing I removed is the topology modification.

import numpy as np
import cv2

img = cv2.imread('cropwhitefromimage.png')
scrollbarright = 20
img = img[:, :-scrollbarright]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
padding = 10
rect = img[y-padding:y+h+padding, x-padding:x+w+padding]
cv2.imshow("Cropped", rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("cropwhitefromimage_result.png", rect)
奢华的一滴泪2025-01-20 13:46:45

垫的结果是否定的可能存在问题。使用 max 和 min 确保提取正确的内容

rect = cv_img[
    max(y-padding,0):min(y+h+padding,orig_h), 
    max(x-padding,0):min(x+w+padding,orig_w)
    ]

there could be issue with the result being negative with the pad. use max and min to be sure to extract the right content

rect = cv_img[
    max(y-padding,0):min(y+h+padding,orig_h), 
    max(x-padding,0):min(x+w+padding,orig_w)
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文