很多关于消除径向(或桶形)变形的问题,但我该怎么加呢?
在视觉上,我想把我的输入(假设为图像(a) ),并扭曲它,就像图像(b):
理想情况下,我想要一个可调的“半径”参数来控制我得到的“多少失真”。根据我想要做的事情,看起来我只需要一个参数来控制‘失真半径’或任何它将被称为(如果我错了的话纠正我)。
如何使用OpenCV实现这一目标?我想这肯定是有可能的,因为很多人都试着用另一种方式去做这样的事情。我只是不太熟悉正确的数学操作和库调用。
任何帮助都很感激,干杯。
发布于 2022-09-08 15:01:45
下面的程序会产生桶形失真。
from wand.image import Image
import numpy as np
import cv2
with Image(filename='Path/to/Img/') as img:
print(img.size)
img.virtual_pixel = 'transparent'
img.distort('barrel', (0.1, 0.0, 0.0, 1.0)) # play around these values to create distortion
img.save(filename='filname.png')
# convert to opencv/numpy array format
img_opencv = np.array(img)
# display result with opencv
cv2.imshow("BARREL", img_opencv)
cv2.waitKey(0)
cv2.destroyAllWindows()
发布于 2022-09-08 17:25:19
这里有一种方法,通过创建X和Y失真映射,然后使用cv.remap()来进行扭曲,从而在Python/OpenCV中生成桶形或枕形畸变。
输入:
import numpy as np
import cv2 as cv
import math
img = cv.imread('lena.jpg')
# grab the dimensions of the image
(h, w, _) = img.shape
# set up the x and y maps as float32
map_x = np.zeros((h, w), np.float32)
map_y = np.zeros((h, w), np.float32)
scale_x = 1
scale_y = 1
center_x = w/2
center_y = h/2
radius = w/2
#amount = -0.75 # negative values produce pincushion
amount = 0.75 # positive values produce barrel
# create map with the barrel pincushion distortion formula
for y in range(h):
delta_y = scale_y * (y - center_y)
for x in range(w):
# determine if pixel is within an ellipse
delta_x = scale_x * (x - center_x)
distance = delta_x * delta_x + delta_y * delta_y
if distance >= (radius * radius):
map_x[y, x] = x
map_y[y, x] = y
else:
factor = 1.0
if distance > 0.0:
factor = math.pow(math.sin(math.pi * math.sqrt(distance) / radius / 2), amount)
map_x[y, x] = factor * delta_x / scale_x + center_x
map_y[y, x] = factor * delta_y / scale_y + center_y
# do the remap
dst = cv.remap(img, map_x, map_y, cv.INTER_LINEAR)
# save the result
#cv.imwrite('lena_pincushion.jpg',dst)
cv.imwrite('lena_barrel.jpg',dst)
# show the result
cv.imshow('src', img)
cv.imshow('dst', dst)
cv.waitKey(0)
cv.destroyAllWindows()
桶(正数):
针垫(负值):
https://stackoverflow.com/questions/59776772
复制相似问题