gpt4 book ai didi

来自网格的 Python cv2.remap 创建像素化失真

转载 作者:行者123 更新时间:2023-12-05 03:19:44 26 4
gpt4 key购买 nike

我有一个比图像小 4 倍的网格,我想用来自网格的信息扭曲图像,但是,当使用 cv2.remap 时,它会使扭曲像素化(见下图)。我怎样才能使失真更平滑?

原文:

enter image description here

期望的输出:

enter image description here

我的输出:

enter image description here

我的代码:

img = np.array(Image.open('astronaut.jpg')) # Shape -> (512, 512, 3)
mesh = Mesh('astronaut.msh').get_uvs() # Shape -> (128, 128, 2), 2 channels for x and y

new_mesh = np.zeros((img.shape[1], img.shape[0], 2))
new_mesh[:,:,0] = np.repeat(np.repeat(mesh[:,:,0], 4, axis=0), 4, axis=1)
new_mesh[:,:,1] = np.repeat(np.repeat(mesh[:,:,1], 4, axis=0), 4, axis=1)

nh, nw = img.shape[:2]
xs, ys = np.meshgrid(np.arange(0, nw), np.arange(0, nh))
xs = xs + new_mesh[:,:,0] * 4 # multiply by constant to modulate distort strength
ys = ys + new_mesh[:,:,1] * 4
xs = np.float32(xs)
ys = np.float32(ys)

dst= cv2.remap(img.astype(np.uint8), xs, ys, cv2.INTER_CUBIC)

最佳答案

OpenCV 不是罪魁祸首。它完全按照您的指示执行。

这些人工制品来自您对 np.repeat 的使用。这只是重复 map 数组中的每个索引元素。您没有正确地对网格进行上采样,您实际上只是使用该代码复制 4x4 补丁

适本地对你的网格进行上采样(np.repeat 是错误的),然后你会得到很好的结果。您可以使用 cv.resize除了最近邻插值之外的任何东西轻松做到这一点。如果您需要精确控制边界行为,您将需要 warpAffine 和自定义转换矩阵。您甚至可以使用 cv.pyrUp(两次)。

当您展示 MRE 时(网格的一些数据),我会用工作代码更新我的答案。

关于来自网格的 Python cv2.remap 创建像素化失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73367759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com