mhashas/Document-Image-Unwarping-pytorch

Question about how to generate systhesis dataset

Opened this issue · 2 comments

  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
    xs, ys  = create_grid(nh, nw)
    xs = xs.reshape(nh, nw).astype(np.float32)
    ys = ys.reshape(nh, nw).astype(np.float32)
    dst = cv2.remap(src_img, xs, ys, cv2.INTER_CUBIC)
    cv2.imwrite('result.jpg', dst)

code above is a way to create fake data. But sometime image's over distorted and it's flipped. Can anyone spot the mistake i made? Here is the question i asked here on stackoverflow.

above code only provide warpped images, but how can you get the restore image matrix @ronghui19