边缘对波形图像的校正

时间:2019-07-03 07:57:54

标签: python image transform cv2

我有这样的图像:

enter image description here

我想对图像进行变换,以便纠正空白。

所需的输出应如下所示:

enter image description here

您能帮我存档吗?

1 个答案:

答案 0 :(得分:2)

您可以这样做...

  • 打开输入图像,将灰度和阈值设置为Numpy数组
  • 使输出图像与输入图像大小相同,但全黑
  • 遍历图像的各列,找到每列中的第一个和最后一个白色像素。复制该列像素以输出以水平中心线为中心的图像
  • 保存结果

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open wavy image and make greyscale
i = Image.open('wavy.jpg').convert('L')

# Make Numpy version of input image and threshold at 128
i = np.array(i)
i = np.where(i>128, np.uint8(255), np.uint8(0)) 

# Make Numpy version of output image - all black initially
o = np.zeros_like(i)

h, w = i.shape

# Process each column, copying white pixels from input image
# ... to output image centred on horizontal centreline
centre = h//2
for col in range(w):
    # Find top and bottom white pixel in this column
    whites = np.nonzero(i[:,col])
    top = whites[0][0]    # top white pixel
    bot = whites[0][-1]   # bottom white pixel
    thk = bot - top       # thickness of sandwich filling
    # Copy those pixels to output image
    startrow = centre - thk//2
    o[startrow:startrow+thk,col] = i[top:bot,col]

Image.fromarray(o).save('result.png')

enter image description here