我有一堆像这样的图像:
其中黄色框是不同配置文件(文本)的内容,其中每个部分由虚线(无直线)划分。所以我需要的是通过虚线将图像分割成多个单图像。到目前为止,我已经尝试了很多带有霍夫线变换的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)
这肯定不是一个完美的解决方案,但我希望它能为您提供有关如何改进当前算法的线索!
它给了我这些你提供的图片:
- 第一次分割
- 第二次分割
- 第三次分割
关于Python通过水平虚线将图像分割成多 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672202/