python opencv裁剪数据增强

举报
风吹稻花香 发表于 2022/07/24 00:47:55 2022/07/24
【摘要】 import globimport random import cv2 import numpy as np dir=r'F:\data' files=glob.glob(dir+'/*.jpg') big_count=0file_len=len(files)for file in files: img_raw = cv...


  
  1. import glob
  2. import random
  3. import cv2
  4. import numpy as np
  5. dir=r'F:\data'
  6. files=glob.glob(dir+'/*.jpg')
  7. big_count=0
  8. file_len=len(files)
  9. for file in files:
  10. img_raw = cv2.imread(file)
  11. area_o=f"{img_raw.shape[1]*img_raw.shape[0]}_{max(img_raw.shape[1],img_raw.shape[0])}"
  12. if img_raw.shape[1]*img_raw.shape[0] > 200*300:
  13. x_scale = 224 / max(img_raw.shape[1],img_raw.shape[0])
  14. img_raw = cv2.resize(img_raw, None, fx=x_scale, fy=x_scale, interpolation=cv2.INTER_AREA)
  15. cv2.imshow("img_raw", img_raw)
  16. img_h,img_w=img_raw.shape[:2]
  17. x_1=random.randint(0, int(img_w*0.05))
  18. y_1=random.randint(0, int(img_h*0.05))
  19. x_2=random.randint( int(img_w*0.95),img_w)
  20. y_2=random.randint( int(img_h*0.95),img_h)
  21. img_crop=img_raw[y_1:y_2,x_1:x_2] #子区域
  22. t_h, t_w = img_crop.shape[:2]
  23. if t_h*t_w>50*200:
  24. big_count+=1
  25. print(file_len,big_count)
  26. a_w = max(t_w, t_h)
  27. a_w = max(a_w,200)
  28. img_b = np.zeros((a_w, a_w, 3), dtype=np.uint8)
  29. img_b[:t_h, :t_w, :] = img_crop
  30. img = cv2.resize(img_b,(128,128))
  31. cv2.imwrite(r'F:\chuli/'+area_o+".jpg",img_b)
  32. # cv2.imshow("img_crop", img)
  33. # cv2.waitKey()

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/125947228

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。