[关闭]
@BruceWang 2018-01-08T21:59:49.000000Z 字数 4726 阅读 1594

图像增强等代码

数据增强
调用超级API: Auto Generated Documentation


增强代码参考链接

1.图像增强

  1. # _*_ coding:utf-8 _*_
  2. """
  3. > Deep learning image augmentation
  4. cited from https://scottontechnology.com/flip-image-opencv-python/
  5. http://augmentor.readthedocs.io/en/master/userguide/mainfeatures.html
  6. """
  7. import cv2
  8. import glob
  9. import random
  10. import os
  11. from multiprocessing import Pool as ProcessPool
  12. from multiprocessing.dummy import Pool as ThreadPool
  13. import Augmentor
  14. import numpy as np
  15. # import path_var

2.定义函数

  1. def img_flip():
  2. # path = "F:/ad_samples/download_sample/14/8DB54D749B1D4A2D5FD3441C681D9A2C522453AC_s.jpg"
  3. path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\original_path\A01_8.jpg'
  4. img = cv2.imread(path)
  5. horizontal_img = img.copy()
  6. vertical_img = img.copy()
  7. both_img = img.copy()
  8. horizontal_img = cv2.flip(img, 0)
  9. vertical_img = cv2.flip(img, 1)
  10. both_img = cv2.flip(img, -1)
  11. cv2.imshow("original img", img)
  12. cv2.imshow("horizontal img", horizontal_img)
  13. cv2.imshow("vertical img", vertical_img)
  14. cv2.imshow("both flip", both_img)
  15. cv2.waitKey(0)
  16. cv2.destroyAllWindows()
  17. def flip_img_save2dir(file):
  18. img = cv2.imread(file)
  19. dst_dir = path_var.g_dst_dir
  20. h_img = img.copy()
  21. v_img = img.copy()
  22. b_img = img.copy()
  23. h_img = cv2.flip(img, 0)
  24. v_img = cv2.flip(img, 1)
  25. b_img = cv2.flip(img, -1)
  26. # file like F:/ad_samples/train_samples/ad_text_artifact/base_type/type_10.jpg
  27. # get file name "type_10"
  28. # type_10.jpg
  29. base_name = os.path.basename(file)
  30. # type_10
  31. base_name = os.path.splitext(base_name)[0]
  32. file_name = dst_dir + base_name + "_h" + ".jpg"
  33. cv2.imwrite(file_name, h_img)
  34. file_name = dst_dir + base_name + "_v" + ".jpg"
  35. cv2.imwrite(file_name, v_img)
  36. file_name = dst_dir + base_name + '_b' + ".jpg"
  37. cv2.imwrite(file_name, b_img)
  38. def do_all_flip(base_dir="F:/ad_samples/train_samples/ad_web_2/"):
  39. """
  40. flip all the images in dir, and then save them
  41. to another dir
  42. :return:
  43. """
  44. # get all files
  45. files = glob.glob(base_dir + "/*.png")
  46. # like ['E:/img\\1.jpg', 'E:/img\\10.jpg']
  47. # start 3 process
  48. # pool = ProcessPool(3)
  49. pool = ThreadPool(3)
  50. rets = pool.map(flip_img_save2dir, files)
  51. pool.close()
  52. pool.join()
  53. print ('all images accomplish flip and save to dir')
  54. def flip_all_in_dir():
  55. base_dir = 'F:/ad_samples/train_samples/others/'
  56. sub_dir_lst = glob.glob(base_dir + "*")
  57. # ['F:/dir1', 'F:/dir2']
  58. # print sub_dir_lst
  59. new_sub_dir = [os.path.join(base_dir, item + '_flip/') for item in os.listdir(base_dir)]
  60. # ['F:/dir1_flip', 'F:/dir2_flip']
  61. for dir_item, new_item in zip(sub_dir_lst[10:], new_sub_dir[10:]):
  62. global g_dst_dir
  63. if not os.path.exists(new_item):
  64. os.makedirs(new_item)
  65. # g_dst_dir = new_item
  66. # Path.g_dst_dir = new_item
  67. path_var.g_dst_dir = new_item
  68. print ('flip %s, flip dir %s' % (dir_item, new_item))
  69. do_all_flip(base_dir=dir_item)

3.终极执行扩增函数

  1. def augmentation():
  2. path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\original_path'
  3. output_path = r'C:\Users\aixin\Desktop\all_my_learning\match\niu_qu\niuqu_path'
  4. p = Augmentor.Pipeline(path, output_directory=output_path)
  5. # p.flip_left_right(probability=0.4)
  6. # p.flip_top_bottom(probability=0.6)
  7. # p.flip_random(probability=0.5)
  8. # p.crop_centre(probability=0.2, percentage_area=0.8)
  9. # p.crop_random(probability=0.6, percentage_area=0.7)
  10. # p.rotate(probability=0.2, max_left_rotation=10, max_right_rotation=16)
  11. # p.rotate_random_90(probability=0.5)
  12. # p.rotate180(probability=0.4)
  13. # p.rotate270(probability=0.3)
  14. p.zoom(probability=0.3, min_factor=1.1, max_factor=1.5)
  15. p.random_distortion(probability=0.5, grid_height=4, grid_width=4, magnitude=4)
  16. p.shear(probability=0.2, max_shear_left=15, max_shear_right=15)
  17. p.shear(probability=0.5, max_shear_left=15, max_shear_right=15)
  18. p.skew(probability=0.1, magnitude=0.6)
  19. p.skew_tilt(probability=0.2, magnitude=0.6)
  20. p.skew_corner(probability=0.2, magnitude=0.6)
  21. p.skew_top_bottom(probability=0.3, magnitude=0.6)
  22. p.skew_left_right(probability=0.2, magnitude=0.6)
  23. # SIZE = 4 * 5
  24. p.sample(10)
  25. if __name__ == '__main__':
  26. # img_flip()
  27. # flip_all_in_dir()
  28. # do_all_flip()
  29. augmentation()
  30. # test single image flip and save
  31. # file = 'F:/ad_samples/train_samples/ad_text_artifact/base_type/type_10.jpg'
  32. # flip_img_save2dir(file=file)
  33. pass

4.opencv执行数据增强

  1. import numpy as np
  2. import cv2
  3. # img_path = r'C:\Users\aixin\Desktop\Phebe.jpg'
  4. img_path = r'C:\Users\aixin\Desktop\Bruce.png'
  5. img = cv2.imread(img_path)
  6. rows, cols, channels = img.shape
  7. print(img.shape)
  8. src = np.float32([[50,50],[200,50],[50,200]])
  9. dst = np.float32([[10,100],[200,50],[100,250]])
  10. pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
  11. pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
  12. M1 = cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
  13. M2 = cv2.getAffineTransform(src, dst)
  14. M3 = cv2.getPerspectiveTransform(pts1, pts2)
  15. dst1 = cv2.warpAffine(img,M1,(cols,rows))
  16. dst2 = cv2.warpAffine(img,M2,(cols,rows))
  17. dst3 = cv2.warpPerspective(img,M3,(cols,rows))
  18. cv2.imshow('img', img)
  19. cv2.imshow('dst1', dst1)
  20. cv2.imshow('dst2', dst2)
  21. cv2.imshow('dst3', dst3)
  22. cv2.waitKey(0)
  23. cv2.destroyAllWindows()

如果你有什么疑问,欢迎联系我哈,我会给大家慢慢解答啦~~~
怎么联系我? 笨啊~ ~~ 你留言也行。

你关注微信公众号1.听朕给你说:2.tzgns666,3.或者扫那个二维码,后台联系我也行啦!
tzgns666

终于写完了,赞赏一下下嘛!

(爱心.gif) 么么哒~么么哒~么么哒
爱心从我做起,贫困山区捐衣服,为开源社区做贡献!

码字不易啊,如果你觉得本文有帮助,三毛也是爱!真的就三毛,呜呜。。。

weiChat
Alibaba

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注