sythetic_doc.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import shutil
  2. import os
  3. import glob
  4. import cv2
  5. import numpy as np
  6. import random
  7. import imgaug.augmenters as iaa
  8. def visualizeImg(list=[]):
  9. for item in list:
  10. cv2.imshow(item[0], item[1])
  11. cv2.waitKey(0)
  12. cv2.destroyAllWindows()
  13. def transformation(src):
  14. height, width = src.shape[:2]
  15. srcPts = np.array([[0, 0], [width, 0], [width, height], [0, height]]).astype(
  16. np.float32
  17. )
  18. float_random_num = random.uniform(0.0, 0.3)
  19. float_random_num2 = random.uniform(0.0, 0.3)
  20. float_random_num3 = random.uniform(0.7, 1)
  21. float_random_num4 = random.uniform(0.0, 0.3)
  22. float_random_num5 = random.uniform(0.7, 1)
  23. float_random_num6 = random.uniform(0.7, 1)
  24. float_random_num7 = random.uniform(0.0, 0.3)
  25. float_random_num8 = random.uniform(0.7, 1)
  26. dstPts = np.array(
  27. [
  28. [width * float_random_num, height * float_random_num2],
  29. [width * float_random_num3, height * float_random_num4],
  30. [width * float_random_num5, height * float_random_num6],
  31. [width * float_random_num7, height * float_random_num8],
  32. ]
  33. ).astype(np.float32)
  34. M = cv2.getPerspectiveTransform(srcPts, dstPts)
  35. # warp_dst = cv2.warpPerspective(src, M, (src.shape[1], src.shape[0]), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, 255)
  36. warp_dst = cv2.warpPerspective(
  37. src,
  38. M,
  39. (width, height),
  40. flags = cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue = [0, 0, 0, 0]
  41. )
  42. # for pt in dstPts:
  43. # warp_dst = cv2.circle(warp_dst, (int(pt[0]), int(pt[1])), radius=4, color=(0, 0, 255), thickness=-1)
  44. # visualizeImg([("warp_dst", warp_dst)])
  45. return warp_dst
  46. def blending(img1, img2):
  47. # I want to put logo on top-left corner, So I create a ROI
  48. rows, cols, channels = img2.shape
  49. roi = img1[0:rows, 0:cols]
  50. # Now create a mask of logo and create its inverse mask also
  51. img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  52. ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_BINARY)
  53. mask_inv = cv2.bitwise_not(mask)
  54. # Now black-out the area of logo in ROI
  55. img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
  56. # Take only region of logo from logo image.
  57. img2_fg = cv2.bitwise_and(img2, img2, mask=mask)
  58. # Put logo in ROI and modify the main image
  59. dst = cv2.add(img1_bg, img2_fg)
  60. img1[0:rows, 0:cols] = dst
  61. return img1
  62. def smoothEdge(blended_img):
  63. up_sample_img = cv2.pyrUp(blended_img)
  64. blur_img = up_sample_img.copy()
  65. for i in range(4):
  66. blur_img = cv2.medianBlur(blur_img, 21)
  67. down_sample_img = cv2.pyrDown(blur_img)
  68. return down_sample_img
  69. if __name__ == "__main__":
  70. dataDir = (
  71. "/Users/imac-1/workspace/hed-tutorial-for-document-scanning/sample_images/"
  72. )
  73. bk_imgs_folder = "background_images"
  74. rect_folder = "rect_images"
  75. bk_img_paths = glob.glob(f"{dataDir+bk_imgs_folder}/*.jpg")
  76. rect_img_paths = glob.glob(f"{dataDir+rect_folder}/*.jpg")
  77. outputDir = f"{dataDir}output"
  78. shutil.rmtree(outputDir, ignore_errors=True)
  79. os.makedirs(outputDir)
  80. for bk_img_path in bk_img_paths:
  81. bk_img = cv2.imread(bk_img_path)
  82. for rect_img_path in rect_img_paths:
  83. rect_img = cv2.imread(rect_img_path)
  84. warpedImg = transformation(rect_img)
  85. resized_img = cv2.resize(warpedImg, bk_img.shape[1::-1])
  86. blended_img = blending(bk_img.copy(), resized_img)
  87. final_img = smoothEdge(blended_img)
  88. rectImgName = os.path.basename(rect_img_path).split(".")
  89. bkImgName = os.path.basename(bk_img_path).split(".")
  90. outputFile = f"{outputDir}/{bkImgName[0]}_{rectImgName[0]}.jpg"
  91. cv2.imwrite(outputFile, final_img)