Python通过水平虚线将图像分割成多 block

标签 python opencv image-processing

我有一堆像这样的图像:

enter image description here

其中黄色框是不同配置文件(文本)的内容,其中每个部分由虚线(无直线)划分。所以我需要的是通过虚线将图像分割成多个单图像。到目前为止,我已经尝试了很多带有霍夫线变换python和cv2示例,但我的尝试都没有在检测中起作用。

最佳答案

根据 @efirvida 的评论,这里有一个非常基本的方法来说明如何做到这一点。

它的作用只是检查给定图片中的每一行像素的值是否等于包含虚线的第一行,然后裁剪图片以将其分割为多张图片...

# import image/array manipulation packages
import cv2
import numpy as np

# read image with OpenCV 2
img = cv2.imread("path/to/file/z4Xje.jpg")

# identify one line of pixels that has dashes
dashes = img[761,:,:]

# check where to split the picture and store that information
splits = [0]
for i in range(img.shape[0]):
    # np.allclose allows you to have small differences between dashed lines
    if np.allclose(img[i,:,:], dashes):
        splits.append(i)

# add last split (height of picture)
splits.append(img.shape[0])

# write each cropped picture to your desired directory
for j in range(len(splits)-1):
    new_img = img[splits[j]:splits[j+1],:]
    cv2.imwrite("/path/to/export/"+str(j)+".jpg", new_img)

这肯定不是一个完美的解决方案,但我希望它能为您提供有关如何改进当前算法的线索!

它给了我这些你提供的图片:

  • 第一次分割

enter image description here

  • 第二次分割

enter image description here

  • 第三次分割

enter image description here

关于Python通过水平虚线将图像分割成多 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672202/

相关文章:

python - 在opencv Mac OS中打开摄像头时出错

opencv - 使用 macports 安装 opencv 非自由特性

c++ - Xcode 中关于 OpenCV 的链接错误

python - 相当于SkImage中的cv2.INTER_AREA

opencv - 更改图像 DPI 以与 tesseract 一起使用

python - Pandas:检查 json 对象中是否存在 dataframe 列

Python 请求奇怪的 URL %-编码

java - 如何创建高斯核并将其用作 opencv 中的过滤器

android - 如何在android中使图像模糊

python - 为什么 Python 的 datetime.utcnow() 总是返回相同的微秒值?