[問題]把圖片映射在網格上但是回貼回去發現變小

作者: kyly157 (我的抄人)   2022-12-09 17:44:02
想問一下各位我現在生成一個變形的網格,想將圖片按照網格的形式用cv2.remap 把圖片
映射在網格上,產生摺痕這樣。類似附圖一樣的效果。
但是remap出來圖片的被壓縮成一個很小的size並且顛倒,我resize 過尺寸,還是一樣。我
想做成像最後一張圖的效果。想請問要怎麼做到? 謝謝各位。
import numpy as np
import matplotlib.pyplot as plt
import cv2
inputs = cv2.imread("26KN4.jpg")
nh, nw = inputs.shape[0], inputs.shape[1]
inputs = cv2.resize(inputs, dsize=(nh, nw), interpolation=cv2.INTER_AREA)
mr = nh
mc = nw
print(mr)
print(mc)
xx = np.arange(mr-1, -1, -1)
yy = np.arange(0, mc, 1)
[Y, X] = np.meshgrid(xx, yy)
ms = np.transpose(np.asarray([X.flatten('F'), Y.flatten('F')]), (1,0))
perturbed_mesh = ms
nv = np.random.randint(20) - 1
for k in range(nv):
#Choosing one vertex randomly
vidx = np.random.randint(np.shape(ms)[0])
vtex = ms[vidx, :]
#Vector between all vertices and the selected one
xv = perturbed_mesh - vtex
#Random movement
mv = (np.random.rand(1,2) - 0.5)*20
hxv = np.zeros((np.shape(xv)[0], np.shape(xv)[1] +1) )
hxv[:, :-1] = xv
hmv = np.tile(np.append(mv, 0), (np.shape(xv)[0],1))
d = np.cross(hxv, hmv)
d = np.absolute(d[:, 2])
d = d / (np.linalg.norm(mv, ord=2))
wt = d
curve_type = np.random.rand(1)
if curve_type > 0.3:
alpha = np.random.rand(1) * 50 + 50
wt = alpha / (wt + alpha)
else:
alpha = np.random.rand(1) + 1
wt = 1 - (wt / 100 )**alpha
msmv = mv * np.expand_dims(wt, axis=1)
perturbed_mesh = perturbed_mesh + msmv
plt.scatter(perturbed_mesh[:, 0], perturbed_mesh[:, 1], c=np.arange(0, mr*mc))
plt.show()
https://ibb.co/FY6MPmd
# dh=500
# dw=500
# img = cv2.copyMakeBorder(inputs, dh, dh, dw, dw, borderType=cv2.BORDER_
CONSTANT, value=(0,0,0))
xs, ys = perturbed_mesh[:, 0], perturbed_mesh[:, 1]
xs = xs.reshape(nh, nw).astype(np.float32)
ys = ys.reshape(nh, nw).astype(np.float32)
dst = cv2.remap(img, xs, ys, cv2.INTER_CUBIC)
image = cv2.resize(dst, (1024,1024),interpolation = cv2.INTER_AREA)
plt.imshow(image)
https://ibb.co/8c1PFMx
https://i.stack.imgur.com/tSBWi.png
作者: tzouandy2818 (Naked Bear)   2022-12-10 02:21:00
不知道是不是我的問題 看不懂你到底想問什麼
作者: jasonfghx (工人)   2022-12-10 08:31:00
Good question

Links booklink

Contact Us: admin [ a t ] ucptt.com