亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

森欄 2023-09-05 17:25:09
我想使用 Python OpenCV 在圖像上應用收縮/凸出濾鏡。結果應該是這個例子的某種形式:https://pixijs.io/pixi-filters/tools/screenshots/dist/bulge-pinch.gif我讀過以下 stackoverflow 帖子,它應該是過濾器的正確公式:Formulas for Barrel/Pincushion Distortion但我正在努力在 Python OpenCV 中實現這一點。我讀過有關在圖像上應用濾鏡的地圖:使用 OpenCv-python 的扭曲效果根據我的理解,代碼可能如下所示:import numpy as npimport cv2 as cvf_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 float32flex_x = np.zeros((h, w), np.float32)flex_y = np.zeros((h, w), np.float32)# create map with the barrel pincushion distortion formulafor y in range(h):    for x in range(w):        flex_x[y, x] = APPLY FORMULA TO X        flex_y[y, x] = APPLY FORMULA TO Y# do the remap  this is where the magic happensdst = 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()這是實現示例圖像中呈現的失真的正確方法嗎?非常感謝有關有用資源或最好的示例的任何幫助。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

在熟悉了 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()

這與使用 ImageMagick 中的Convert -implode函數具有相同的效果。


查看完整回答
反對 回復 2023-09-05
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

您可以使用 Python Wand(使用 ImageMagick)中的內爆和爆炸選項來完成此操作。

輸入:

https://img1.sycdn.imooc.com//64f6f42f0001dc7602490251.jpg

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)

內爆:

https://img1.sycdn.imooc.com//64f6f44100018ba902530252.jpg

爆炸:

https://img1.sycdn.imooc.com//64f6f44e0001fa2f02520255.jpg


查看完整回答
反對 回復 2023-09-05
  • 1 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號