想问一下各位我现在生成一个变形的网格,想将图片按照网格的形式用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