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

看板Python作者 (我的抄人)時間2年前 (2022/12/09 17:44), 2年前編輯推噓1(101)
留言2則, 2人參與, 2年前最新討論串1/1
想問一下各位我現在生成一個變形的網格,想將圖片按照網格的形式用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
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.118.102.68 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1670579044.A.C09.html

12/10 02:21, 2年前 , 1F
不知道是不是我的問題 看不懂你到底想問什麼
12/10 02:21, 1F

12/10 08:31, 2年前 , 2F
Good question
12/10 08:31, 2F
※ 編輯: kyly157 (111.250.190.58 臺灣), 12/10/2022 11:02:26 ※ 編輯: kyly157 (111.250.190.58 臺灣), 12/10/2022 11:06:38 ※ 編輯: kyly157 (111.250.190.58 臺灣), 12/10/2022 11:07:20
文章代碼(AID): #1ZamDam9 (Python)
文章代碼(AID): #1ZamDam9 (Python)