【问题标题】:Rectification of wavy image by edges通过边缘校正波浪图像
【发布时间】:2019-11-13 20:54:18
【问题描述】:

我有这样的图像:

我想转换图像以纠正空白。

所需的输出应如下所示:

你能帮我存档吗?

【问题讨论】:

  • 整改是什么意思?
  • 你能显示上图所需的输出吗?
  • 为转换添加了所需的输出。

标签: python image transform cv2


【解决方案1】:

你可以这样做......

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

【讨论】:

  • 谢谢你解决了我的问题 :) 你能告诉我你为什么使用双斜杠吗?
  • 双斜杠让你得到整数除法,所以答案没有小数部分 - 这对索引数组不利;-)
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 2019-11-14
  • 1970-01-01
  • 2022-11-10
  • 2018-09-08
  • 2019-06-07
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多