python - 裁剪不需要的图像黑色空间

标签 python image opencv image-processing opencv3.0

我有一组灰度图像,如下所示:

enter image description here

这是示例图片,因为我无法发布原始图片。每个图像都有一个带有纹理的区域、一个纯白色水印 (pos) 和许多不需要的黑色空间。

理想情况下,应将此图像裁剪为:

enter image description here

每个图像中的水印可能略有不同,但始终是非常细的纯白色文本。

图片可能看起来很不一样,这是另一个例子

enter image description here

这个只需要在左边裁剪

enter image description here

另一个:

enter image description here

这个需要在顶部和底部裁剪:

enter image description here

还有一个

enter image description here

这个需要在顶部和右侧裁剪。请注意,我在这张图片中留下了水印。理想情况下,水印也会被删除,但我想没有水印会更容易。

这是水印在现实中的样子。

enter image description here

图片大小不一,但通常都很大(超过 2000x2000)。

我正在寻找 python 中的解决方案(可能是 cv2)。

我的第一个想法是使用这样的东西: Python & OpenCV: Second largest object

但是这个解决方案代码对我来说失败了

最佳答案

我在 C# 和 C++ 中工作,不在 python 中工作,但可以向您建议逻辑。

您需要对图像进行两次扫描,一行扫描,另一列扫描。

既然你说图像不需要的部分总是黑色的,那么只需读取两次扫描中的像素值即可。如果某一行中所有像素的颜色都是黑色,那么您可以删除或删除该行。列扫描可以遵循类似的步骤。

现在我们不能简单地删除行和列,所以只需记下多余的行和列,然后您可以使用以下代码裁剪图像:(我将使用 emgucv 库在 C# 中编写代码,但它很容易理解对于 python)

Mat original_image = new Mat();
Rect ROI = new Rect(x,y,width,height);
Mat image_needed_to_crop = new Mat(original_image,ROI);

此代码仅从原始图像中提取感兴趣区域。

关于python - 裁剪不需要的图像黑色空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911330/

相关文章:

python - 使用 5 个 3D 点计算 3D 单应性

python - 如何修复 TVL1 光流算法的 opencv 导入

python - 我如何在 Python 中反省所有有效的 protobuf 枚举值?

python - pandas 中的 vlookup 应用 %LIKE%

python - 使用 Anaconda 安装 OpenCV 时遇到错误

image - Highcharts:自定义按钮 - 符号错位

html - 具有固定高度 : center image horizontally or vertically based on image dimensions 的 Bootstrap 旋转木马

python - 使用 MySQL 和 Python 返回基于子查询的元组

python - wxpython 没有 'adv'

image - 你能帮我使用任意大小的字体,例如 Times New Roman、Tahoma