- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用从平板扫描仪获得的图像生成“皱巴巴”的图像。
按照论文中描述的方法[Link]在第 3.1 节中。我已经编写了生成扰动网格的代码,但我不知道如何将源图像中的像素映射到该网格上以形成扰动图像。
这是生成扰动网格的代码。
import numpy as np
import matplotlib.pyplot as plt
mr = 88
mc = 68
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()
用于测试的示例源图像: /image/26KN4.jpg
我坚持将源图像像素映射到网格上。如果有人可以提供帮助,我将不胜感激。
最佳答案
(1) 使用cv2.copyMakeBorder
放大图像,避免变形点超出原始图像尺寸范围。
cv2.copyMakeBorder(...)
copyMakeBorder(src, top, bottom, left, right, borderType[, dst[, value]]) -> dst
. @brief Forms a border around an image.
.
. The function copies the source image into the middle of the destination image. The areas to the
. left, to the right, above and below the copied source image will be filled with extrapolated
. pixels. This is not what filtering functions based on it do (they extrapolate pixels on-fly), but
. what other more complex functions, including your own, may do to simplify image boundary handling.
用法:
img = cv2.copyMakeBorder(img, dh, dh, dw, dw, borderType=cv2.BORDER_CONSTANT, value=(0,0,0))
设置 dw=nw//2, dh=nh//2
可能没问题,必要时调整。 nh, nw
是源图像的高度和宽度。
(2) 使用 the method from the paper 创建扰动网格
xs, ys = create_grid() # the result is like np.meshgrid
注意确定类型和大小。
# xs = xs.reshape(nh, nw).astype(np.float32)
# nh, nw is the height and width of the coppied image
(3) 使用cv2.remap
重映射:
cv2.remap(...)
remap(src, map1, map2, interpolation[, dst[, borderMode[, borderValue]]]) -> dst
. @brief Applies a generic geometrical transformation to an image.
.
. The function remap transforms the source image using the specified map:
. \f[\texttt{dst} (x,y) = \texttt{src} (map_x(x,y),map_y(x,y))\f]
用法:
dst= cv2.remap(img, xs, ys, cv2.INTER_CUBIC)
这是一个演示结果:
(4) 裁剪非零区域并根据需要调整大小:
相关:
关于python - 如何使用变形网格扭曲图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53907633/
我正在尝试在我的网站上嵌入多个 .svg 文件。只要我使用 Chrome、Firefox 或我目前测试过的任何移动浏览器,一切似乎都运行良好。但是,有一个异常(exception):每当我在 Wind
我正在尝试在我的网站上嵌入多个 .svg 文件。只要我使用 Chrome、Firefox 或我目前测试过的任何移动浏览器,一切似乎都运行良好。但是,有一个异常(exception):每当我在 Wind
是否有某种方法(库或算法)可用于在 java.awt.Shape 或其路径迭代器的两个实例之间进行插值?例如,要在矩形和椭圆之间无缝过渡?或者更一般的 Path2D 情况。 最佳答案 SwingX 中
我试图在动态大小的视频上包含一个 Canvas 元素,该视频将异步加载。在 Canvas 上,用户将能够拖动矩形选择框并调整其大小。 在我的 JS 文件中,我有一个监听器监 window 口,并通过
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。 去
有没有办法通过 GDAL(使用 Python API)使用移位向量来扭曲图像? 通过移位向量,我的意思是例如。包含以下列的 CSV(或 numpy)文件:starting_x,starting_y,t
我正在创建一个导航按钮。当用户按下它时,按钮的图像应该改变,反射(reflect)它的状态(例如菜单打开/关闭)。我决定为此做一个 morphing-liek 动画。可以用CoreAnimation来
我在 Pandas 中有以下示例数据框。如何获取每个 'Id' 的 'label_weight' 值的最大值并将相应的 'label' 列分配给该 'Id' 在新列 'assgined_label'
文本使我的框变形。 这是我的: This text is deforming the "leftOne" 还有 CSS: .leftOne { float: left;
HTML: Home Services About Us Contact Us CSS: ul { pa
我想在这里得到 openCV 爱好者的帮助。 我想知道关于如何变形 2 个面孔的方向(以及一些建议或代码段),以及一种比率,即第一个面孔的 10% 和第二个面孔的 90%。 我见过像 cvWarpAf
我已经搜索了很长时间,但还没有找到真正的答案,但是,也许我的眼睛上有西红柿,但是真的没有针对 python/MATLAB 的框架可以进行面部扭曲/开箱即用? 一个框架,我在其中放入两张带有特征点的图像
根据material.io float 操作按钮可以变身为操作菜单,如 this .有什么方法可以只使用 Material 库(没有第三方库)吗? 我试过了this库,但它会在菜单关闭后根据底部应用栏
这就是我想用我的 NSOpenGLView 做的事情。目前 NSOpenGLView 覆盖了窗口的整个区域,我想在 NSOpenGLView 顶部添加按钮、nsviews 和图像。我浏览了网页,发现
我正在遍历在 Controller 中定义的集合。 我正在使用基础轨道插件将其转变为轮播。 但是我的HTML被弄乱了,并且破坏了插件,因为它期望获得一定的输出。
我不知道如何使用 BufferedImage 使图像变形。有人能帮我吗 ?我绝对绝望了。感谢您的所有提示。对不起我的英语不好。 | |
这个问题的答案似乎相互矛盾,我很困惑在 Core Data 数据库中存储图像的最佳方式是什么。 This question说可变形,但是this question说要使用二进制数据。 如果我只是想把它
您好,我不确定如何处理有关表单的逻辑。所以,表格很大,我知道有 20 多个字段被认为是“不好的做法”,表格应该最少,但这就是客户想要的,所以不用争论,无论如何表格都会接受订单,但有不同的顺序类型(更具
我正在使用 animator() 在我的应用程序的帧( subview )之间横向滚动 NSScrollView。当动画发生并且我调整 NSWindow 的大小时,整个 NSView 会像这样扭曲:
仍在我的太空入侵者克隆上工作,我想在屏幕底部添加可破坏的基地: 我已经弄清楚如何通过让炸弹和盾牌相互接触来修改盾牌的外观,然后在 didBegincontact 中,从炸弹爆炸的 mask 和盾牌的当
我是一名优秀的程序员,十分优秀!