如何使用变形网格扭曲图像

pap*_*eps 6 python opencv image-processing computer-vision

我正在尝试使用从平板扫描仪获得的图像生成“皱巴巴的”图像。

按照第 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()
Run Code Online (Sandbox Code Playgroud)

这是扰动网格的样子: 在此处输入图片说明

这是说明合成图像生成的论文的屏幕截图:在此处输入图片说明

用于测试的示例源图像:https : //i.stack.imgur.com/26KN4.jpg

我坚持将源图像像素映射到网格上。如果有人可以提供帮助,我将不胜感激。

Kin*_*t 金 5

(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.
Run Code Online (Sandbox Code Playgroud)

用途:

img = cv2.copyMakeBorder(img, dh, dh, dw, dw, borderType=cv2.BORDER_CONSTANT, value=(0,0,0))
Run Code Online (Sandbox Code Playgroud)

设置dw=nw//2, dh=nh//2也许可以,必要时进行调整。是nh, nw源图像的高度和宽度。

(2) 使用论文中的方法创建扰动网格

xs, ys = create_grid() # the result is like np.meshgrid
Run Code Online (Sandbox Code Playgroud)

注意确定类型和尺寸。

# xs = xs.reshape(nh, nw).astype(np.float32)
# nh, nw is the height and width of the coppied image
Run Code Online (Sandbox Code Playgroud)

(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]
Run Code Online (Sandbox Code Playgroud)

用法:

dst= cv2.remap(img, xs, ys, cv2.INTER_CUBIC)
Run Code Online (Sandbox Code Playgroud)

这是演示结果:

在此输入图像描述

(4) 裁剪非零区域并根据需要调整大小:

在此输入图像描述


有关的:

  1. 将 opencv 重映射代码从 c++ 转换为 python

  2. 分割扫描文档中的文本行