当前位置:主页 > 软件编程 > Python代码 >

python随机在一张图像上截取任意大小图片的方法

时间:2021-03-18 09:45:35 | 栏目:Python代码 | 点击:

如下所示:

'''
机器学习中随机产生负样本的
'''
 
import cv2
import random
 
#读取图片
img=cv2.imread('1.png')
 
#h、w为想要截取的图片大小
h=80
w=80
 
count=1
while 1:
    #随机产生x,y  此为像素内范围产生
   y = random.randint(1, 890)
  x = random.randint(1, 1480)
  #随机截图
   cropImg = img[(y):(y + h), (x):(x + w)]
  cv2.imwrite('pic/' + str(count) + '.png', cropImg)
  count+=1
 
  if count==2500:
    break
 

您可能感兴趣的文章:

相关文章