当前位置:   article > 正文

python openCv实现批量图像自动识别裁剪区域进行裁剪保存_python 识别 智能剪裁图片

python 识别 智能剪裁图片
  1. # -*- coding: utf-8 -*-
  2. import cv2
  3. import numpy as np
  4. import copy
  5. import cv2 as cv
  6. import os
  7. def read_path(file_pathname):
  8. # 遍历该目录下的所有图片文件
  9. for filename in os.listdir(file_pathname):
  10. src = cv.imread(file_pathname + '/' + filename)
  11. gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  12. width = gray.shape[0] #图片行
  13. height = gray.shape[1]#图片列
  14. green_max = np.zeros(2) #一行两列0
  15. green_min = np.zeros(2)
  16. # print(green_max)
  17. a_list = []
  18. for i in range(width):
  19. for j in range(height):
  20. gray_value = gray[i, j]
  21. if gray_value > 20:
  22. a = np.array([i, j])
  23. a_list.append(a) #所有黑色区域坐标点
  24. #print(a_list[1])
  25. #print(a_list)
  26. # 最大坐标
  27. green_max = a_list[1]
  28. for g, k in enumerate(a_list):
  29. if k[0] > green_max[0]:
  30. green_max[0] = int(k[0])
  31. else:
  32. green_max[0] = int(green_max[0])
  33. if k[1] > green_max[1]:
  34. green_max[1] = int(k[1])
  35. else:
  36. green_max[1] = int(green_max[1])
  37. # print(green_max)
  38. # 最小坐标
  39. green_min = a_list[0]
  40. # print(green_min)
  41. for h, z in enumerate(a_list):
  42. # print(green_min)
  43. if z[0] < green_min[0]:
  44. green_min[0] = z[0]
  45. else:
  46. green_min[0] = green_min[0]
  47. if z[1] < green_min[1]:
  48. green_min[1] = z[1]
  49. else:
  50. green_min[1] = green_min[1]
  51. dst = src[int(green_min[0] - 80):int(green_max[0] + 60), int(green_min[1] - 60):int(green_max[1] + 60)]
  52. # cv.imshow('gray', dst)
  53. cv.imwrite('D:/Anaconda3/test7' + "/" + filename, dst)
  54. read_path("09")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/77484
推荐阅读
相关标签
  

闽ICP备14008679号