使用 Python OpenCV 的收缩/凸起失真

Dav*_*nes 4 python opencv image-processing filter distortion

我想使用 Python OpenCV 在图像上应用收缩/凸出滤镜。结果应该是这个例子的某种形式:

\n

https://pixijs.io/pixi-filters/tools/screenshots/dist/bulge-pinch.gif

\n

我已阅读以下 stackoverflow 帖子,该帖子应该是过滤器的正确公式:Barrel/Pincushion 失真的公式

\n

但我正在努力在 Python OpenCV 中实现这一点。

\n

我读过有关在图像上应用过滤器的地图:Distortion e\xef\xac\x80ect using OpenCv-python

\n

根据我的理解,代码可能如下所示:

\n
import numpy as np\nimport cv2 as cv\n\nf_img = \'example.jpg\'\nim_cv = cv.imread(f_img)\n\n# grab the dimensions of the image\n(h, w, _) = im_cv.shape\n\n# set up the x and y maps as float32\nflex_x = np.zeros((h, w), np.float32)\nflex_y = np.zeros((h, w), np.float32)\n\n# create map with the barrel pincushion distortion formula\nfor y in range(h):\n    for x in range(w):\n        flex_x[y, x] = APPLY FORMULA TO X\n        flex_y[y, x] = APPLY FORMULA TO Y\n\n# do the remap  this is where the magic happens\ndst = cv.remap(im_cv, flex_x, flex_y, cv.INTER_LINEAR)\n\ncv.imshow(\'src\', im_cv)\ncv.imshow(\'dst\', dst)\n\ncv.waitKey(0)\ncv.destroyAllWindows()\n
Run Code Online (Sandbox Code Playgroud)\n

这是实现示例图像中呈现的失真的正确方法吗?非常感谢有关有用资源或最好的示例的任何帮助。

\n

Dav*_*nes 8

在熟悉了 ImageMagick 源代码后,我找到了一种应用失真公式的方法。借助OpenCV 重映射函数,这是一种扭曲图像的方法:

import numpy as np
import cv2 as cv

f_img = 'example.jpg'
im_cv = cv.imread(f_img)

# grab the dimensions of the image
(h, w, _) = im_cv.shape

# set up the x and y maps as float32
flex_x = np.zeros((h, w), np.float32)
flex_y = np.zeros((h, w), np.float32)

# 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):
            flex_x[y, x] = x
            flex_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)
            flex_x[y, x] = factor * delta_x / scale_x + center_x
            flex_y[y, x] = factor * delta_y / scale_y + center_y

# do the remap  this is where the magic happens
dst = cv.remap(im_cv, flex_x, flex_y, cv.INTER_LINEAR)

cv.imshow('src', im_cv)
cv.imshow('dst', dst)

cv.waitKey(0)
cv.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

这与使用ImageMagick 中的Convert -implode函数具有相同的效果。


fmw*_*w42 7

您可以使用 Python Wand(使用 ImageMagick)中的内爆和爆炸选项来完成此操作。

输入:

在此输入图像描述

from wand.image import Image
import numpy as np
import cv2

with Image(filename='zelda1.jpg') as img:
    img.virtual_pixel = 'black'
    img.implode(0.5)
    img.save(filename='zelda1_implode.jpg')
    # convert to opencv/numpy array format
    img_implode_opencv = np.array(img)
    img_implode_opencv = cv2.cvtColor(img_implode_opencv, cv2.COLOR_RGB2BGR)

with Image(filename='zelda1.jpg') as img:
    img.virtual_pixel = 'black'
    img.implode(-0.5 )
    img.save(filename='zelda1_explode.jpg')
    # convert to opencv/numpy array format
    img_explode_opencv = np.array(img)
    img_explode_opencv = cv2.cvtColor(img_explode_opencv, cv2.COLOR_RGB2BGR)

# display result with opencv
cv2.imshow("IMPLODE", img_implode_opencv)
cv2.imshow("EXPLODE", img_explode_opencv)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

内爆:

在此输入图像描述

爆炸:

在此输入图像描述