通过边缘校正波浪图像

Rectification of wavy image by edges

我有这样一张图片:

我想变换图像,使白色 space 得到矫正。

所需的输出应如下所示:

你能帮我存档吗?

你可以这样做...

  • 打开输入图像,将灰度和阈值做成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')