?可自动生成三边形、四边形、五边形的目标图案,以及对应的标签,标签的像素值可自定义
import math
from math import fabs, sin, cos, radians
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageDraw
import cv2
import random
import os
'''根据列表点,在图中画折线'''
def draw_Polyline(img, point_list, color=(0, 0, 255)):
pts=np.array(point_list,np.int32)
pts=pts.reshape((-1,1,2))
cv2.polylines(img,pts=[pts],isClosed=True, color=color, thickness=1)
return img
'''根据列表点集合,在图中画填充的图像'''
def draw_fill_line(img, point_list_sum, mask_img, any_smallimg):
Point_SUM = []
for point_list in point_list_sum:
point_list = np.array(point_list)
Point_SUM.append(point_list)
R = random.randint(0, 255)
G = random.randint(0, 255)
B = random.randint(0, 255)
cv2.fillPoly(img, Point_SUM, (R, G, B))
cv2.fillPoly(mask_img, Point_SUM, any_smallimg*80)
return img, mask_img
'''根据坐标点和半径生成一组列表点,能形成固定形状的5边形(各向异性)'''
def draw_one_FiveSides_img(x, y, radius, angle):
angle = angle/180 * math.pi #角度转换成弧度
x1 = x + radius * sin(angle)
y1 = y - radius * cos(angle)
x2 = x + radius * cos(angle)
y2 = y + radius * sin(angle)
x3 = x - radius * sin(angle)
y3 = y + radius * cos(angle)
x4 = x - 0.5*radius * cos(angle)
y4 = y - 0.5*radius * sin(angle)
points = []
points.append([int(x), int(y)])
points.append([int(x1), int(y1)])
points.append([int(x2), int(y2)])
points.append([int(x3), int(y3)])
points.append([int(x4), int(y4)])
return points
'''根据坐标点和半径生成一组列表点,能形成固定形状的四边形(各向异性)'''
def draw_one_FourSides_img(x, y, radius, angle):
angle = angle / 180 * math.pi # 角度转换成弧度
x1 = x + radius * sin(angle)
y1 = y - radius * cos(angle)
x2 = x + 0.5 * radius * cos(angle)
y2 = y + 0.5 * radius * sin(angle)
x3 = x - radius * sin(angle)
y3 = y + radius * cos(angle)
x4 = x - 0.5 * radius * cos(angle)
y4 = y - 0.5 * radius * sin(angle)
points = []
#points.append([int(x), int(y)])
points.append([int(x1), int(y1)])
points.append([int(x2), int(y2)])
points.append([int(x3), int(y3)])
points.append([int(x4), int(y4)])
return points
'''根据坐标点和半径生成一组列表点,能形成固定形状的三边形(各向异性)'''
def draw_one_threeSides_img(x, y, radius, angle):
angle = angle / 180 * math.pi # 角度转换成弧度
x1 = x + radius * sin(angle)
y1 = y - radius * cos(angle)
rate = random.randint(5, 9) * 0.1
x2 = x + rate * radius * cos(angle)
y2 = y + rate * radius * sin(angle)
x3 = x - radius * sin(angle)
y3 = y + radius * cos(angle)
#x4 = x - 0.5 * radius * cos(angle)
#y4 = y - 0.5 * radius * sin(angle)
points = []
#points.append([int(x), int(y)])
points.append([int(x1), int(y1)])
points.append([int(x2), int(y2)])
points.append([int(x3), int(y3)])
#points.append([int(x4), int(y4)])
return points
#每次生成一张图
def draw_one_img(num):
#生成几个图案
num_smallimg = random.sample([1, 2, 3], num)
SCR_img = np.ones(shape=[512, 512, 3], dtype=np.uint8)
R = random.randint(0, 255)
G = random.randint(0, 255)
B = random.randint(0, 255)
SCR_img[:,:,0:1] = R
SCR_img[:,:,1:2] = G
SCR_img[:,:,2:3] = B
#标签图
mask_img = np.ones(shape=[512, 512], dtype=np.uint8)
mask_img[:,:] = 0
#每一个图案所在的位置
loate_smallimg = random.sample([[0, 0], [0,1], [1,0], [1,1]], num_smallimg[0])
for loate in loate_smallimg:
point_list_sum = []
#生成随机角度
angle = random.randint(0, 360)
#生成随机半径
radius = random.randint(50, 120)
#把512的图像拆成4宫格,根据count的值选择在哪个格子里生成矩形
X = loate[0] * 256
Y = loate[1] * 256
x1 = random.randint(10, 246) + X
y1 = random.randint(10, 246) + Y
if(x1 + radius > X+256):
x1 = X+256 - radius - 2
if(x1 - radius < X):
x1 = X + radius + 2
if(y1 + radius > Y+256):
y1 = Y+256 - radius -2
if(y1 - radius < Y):
y1 = Y + radius + 2
any_smallimg = random.randint(1, 3)
if any_smallimg==1:
points = draw_one_threeSides_img(x1, y1, radius, angle)
if any_smallimg==2:
points = draw_one_FourSides_img(x1, y1, radius, angle)
if any_smallimg==3:
points = draw_one_FiveSides_img(x1, y1, radius, angle)
point_list_sum.append(points)
#画颜色填充的多边形图
SCR_img, mask_img = draw_fill_line(SCR_img, point_list_sum, mask_img, any_smallimg)
#画红色折线图可视化
#if(visual == True):
# for point_list in point_list_sum:
# result_img = draw_Polyline(result_img, point_list)
return SCR_img, mask_img
def do_keypoints_action2(number, Gen_img_path):
path = Gen_img_path
for i in range(number):
img_save_path = os.path.join(path, str(i) + ".png")
label_save_path = os.path.join(path, str(i) + "_mask.png")
# n = random.sample([1, 2, 3, 4], 1)
n = [1]
img, mask_img = draw_one_img(n[0])
#保存图片
cv2.imwrite(img_save_path, img)
cv2.imwrite(label_save_path, mask_img)
if __name__ == '__main__':
number = 10
Gen_img_path = "F:\\000\\test"
do_keypoints_action2(number, Gen_img_path)
?
?
?
?
|