python 绘制随机实心圆,保存标签

import numpy as np
import cv2
import os

def main():
ann_images = ‘D:/University/Data/images/’
ann_savepath = ‘D:/University/Data/landmarks/’
if not os.path.exists(ann_savepath): # 创建文件夹
os.mkdir(ann_savepath)
if not os.path.exists(ann_images): # 创建文件夹
os.mkdir(ann_images)

for j in range(0,300):
    # 1.创建白色背景图片
    d = 400
    img = np.ones((d, d, 3), np.uint8) * 255

    # 2.循环随机绘制实心圆
    # 随机中心点
    center_x = np.random.randint(0, high=d)
    center_y = np.random.randint(0, high=d)
    # 随机半径与颜色
    radius = np.random.randint(20, high=d/5)
    # color = np.random.randint(0, high=256, size=(3, )).tolist()
    color = (10,10,10)

    if(center_x+radius>380):
        continue
    if(center_x-radius<=20):
        continue
    if(center_y+radius>380):
        continue
    if(center_y-radius<=20):
        continue

    cv2.circle(img, (center_x, center_y), radius, color, -1)

    xmin = center_x - radius
    ymin = center_y - radius
    xmax = center_x + radius
    ymax = center_y + radius

    newfile='circular'+str(j)+'.txt'
    #print(newfile)
    ann_savefile = ann_savepath + newfile    # 保存文件
    fp_w = open(ann_savefile, 'w')

    fp_w.write(str(xmin))
    fp_w.write(',')
    fp_w.write(str(ymin))
    fp_w.write('\n')
    fp_w.write(str(xmax))
    fp_w.write(',')
    fp_w.write(str(ymax))
    fp_w.close()

    # 3.显示结果
    newimage='circular'+str(j)+'.jpg'
    ann_saveimage=ann_images+newimage
    cv2.imwrite(ann_saveimage, img, [int(cv2.IMWRITE_JPEG_QUALITY), 95])

if name == ‘main’:
main()

你可能感兴趣的:(数据处理,python)