1

I tried to make dataset for training network. Here is my reference. How to warp an image using deformed mesh.

However, image i created in incomplete, like this:

image i created.

what may go wrong in my code?

Source image is:

here.

import numpy as np
import matplotlib.pyplot as plt
import cv2

def create_grid(width=100, height=100):
    mr = width
    mc = height

    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)[1])
        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) * 20 + 20
          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
    
    perturbed_mesh[:, 0] = np.where(perturbed_mesh[:, 0] > height, height, perturbed_mesh[:, 0])
    perturbed_mesh[:, 1] = np.where(perturbed_mesh[:, 1] > width, width, perturbed_mesh[:, 1])

    # plt.scatter(perturbed_mesh[:, 0], perturbed_mesh[:, 1], c=np.arange(0, mr*mc))
    # plt.show()

    return perturbed_mesh[:, 0], perturbed_mesh[:, 1]

src_img = cv2.imread('source.jpg')
height, width, _ = src_img.shape
dh = height // 20
dw = width // 20
img = cv2.copyMakeBorder(src_img, dh, dh, dw, dw, borderType=cv2.BORDER_CONSTANT,   value=(0,0,0))
nh, nw, _ = img.shape

# create perturbed mesh grid based on https://openaccess.thecvf.com/content_cvpr_2018/papers/Ma_DocUNet_Document_Image_CVPR_2018_paper.pdf

xs, ys  = create_grid(nh, nw)
xs = xs.reshape(nh, nw).astype(np.float32)
ys = ys.reshape(nh, nw).astype(np.float32)

# Use cv2.remap to remap

dst = cv2.remap(src_img, xs, ys, cv2.INTER_CUBIC)
cv2.imwrite('result.jpg', dst)
Bilal
  • 3,191
  • 4
  • 21
  • 49

0 Answers0