【发布时间】:2019-12-27 15:39:17
【问题描述】:
我想编写一个代码来纠正失真并帮助去除鱼眼图像。
我在这里找到了它的伪代码,并试图坚持下去:
http://www.tannerhelland.com/4743/simple-algorithm-correcting-lens-distortion/
from PIL import Image
import numpy as np
im = Image.open('myimage.png')
img = Image.new("RGB",(512,512),'green')
im = im.convert("RGB")
pix_val = im.load()
pix_valNew = img.load()
width, height = im.size
strength = 1.5
zoom = 1.0
halfWidth = width/2
halfHeight = height/2
theta = -1
if strength == 0:
strength = 0.00001
correctionRadius = ((width**2 + height**2)/strength)**0.5
for x in range(512):
for y in range(512):
newX = x - halfWidth
newY = y - halfHeight
distance = (newX**2 + newY**2)**0.5
r = distance/correctionRadius
if r == 0:
theta = 1
else:
theta = np.arctan(r)/r
sourceX = (int)(halfWidth + theta * newX * zoom)
sourceY = (int)(halfHeight + theta * newY * zoom)
pix_valNew[x,y] = pix_val[sourceX,sourceY]
img.show()
我不断收到一张全白的图像,但我无法对其进行故障排除,因为我对它完全陌生。
512x512 是我想要“去鱼”的图像的分辨率。 据我了解,逻辑是在 鱼眼图像并将其映射到正常图像中的相应位置
有人要求我提供链接的伪代码,但我也将其粘贴在这里。如下: 输入: 作为浮点数的强度 >= 0。0 = 没有变化,高数字等于更强的修正。 缩放为浮点 >= 1。(1 = 缩放不变)
算法:
set halfWidth = imageWidth / 2
set halfHeight = imageHeight / 2
if strength = 0 then strength = 0.00001
set correctionRadius = squareroot(imageWidth ^ 2 + imageHeight ^ 2) / strength
for each pixel (x,y) in destinationImage
set newX = x - halfWidth
set newY = y - halfHeight
set distance = squareroot(newX ^ 2 + newY ^ 2)
set r = distance / correctionRadius
if r = 0 then
set theta = 1
else
set theta = arctangent(r) / r
set sourceX = halfWidth + theta * newX * zoom
set sourceY = halfHeight + theta * newY * zoom
set color of pixel (x, y) to color of source image pixel at (sourceX, sourceY)
我们将不胜感激任何形式的帮助。
【问题讨论】:
-
欢迎堆栈溢出。您应该考虑更新您的帖子以包含伪代码。
-
您的“myimage.png”是 512x512 图像吗?
-
你调试过变量的值吗?想知道它们在哪一点偏离了您的预期?
-
@Cris "Lense" 是 generally considered to be a misspelling。
-
@LightnessRacesBY-SA3.0:我在修复错字时打错了字。谢谢!
标签: python image image-processing