Rectification of wavy image by edges

Viewed 110

I have an image like this:

enter image description here

and i want to transform the image so that the white space is rectified.

The desired output should look something like this:

enter image description here

Can you help me archive this?

1 Answers

You can do it like this...

  • Open input image, make greyscale and threshold into Numpy array
  • Make output image same size as input, but fully black
  • Iterate over the columns of the image, finding the first and last white pixel in each column. Copy that column of pixels to output image centred on the horizontal centreline
  • Save result

#!/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

Related