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, any_smallimg, width, hight):
mask_img = np.ones(shape=[hight, width], dtype=np.uint8)
mask_img[:,:] = 0
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))
minx = width
miny = hight
maxx = 0
maxy = 0
for i in range(len(Point_SUM[0])):
if Point_SUM[0][i][0] < minx:
minx = Point_SUM[0][i][0]
if Point_SUM[0][i][0] > maxx:
maxx = Point_SUM[0][i][0]
if Point_SUM[0][i][1] < miny:
miny = Point_SUM[0][i][1]
if Point_SUM[0][i][1] > maxy:
maxy = Point_SUM[0][i][1]
cv2.fillPoly(mask_img, Point_SUM, 254)
#ret, thresh = cv2.threshold(mask_img, 230, 255, cv2.THRESH_BINARY_INV)
#contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#x, y, w, h = cv2.boundingRect(contours[1]) # 计算点集最外面的矩形边界
#cv2.rectangle(img, (minx, miny), (maxx, maxy), (0, 255, 0), 2)
obj_point = [minx, miny, maxx, maxy, any_smallimg-1]
#左上角 和 右下角坐标
return img, obj_point
'''根据坐标点和半径生成一组列表点,能形成固定形状的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, width, hight):
#生成几个图案
num_smallimg = random.sample([1, 2, 3], num)
SCR_img = np.ones(shape=[hight, width, 3], dtype=np.uint8)
#原图添加背景色
R = random.randint(0, 255)
G = random.randint(0, 255)
B = random.randint(0, 255)
SCR_img[:,:,0:1] = 125#R
SCR_img[:,:,1:2] = 125#G
SCR_img[:,:,2:3] = 125#B
#标签图
obj_points = []
#每一个图案所在的位置
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(20, 120)
half_width = width*0.5
half_high = hight*0.5
#把512的图像拆成4宫格,根据count的值选择在哪个格子里生成矩形
X = loate[0] * half_width
Y = loate[1] * (hight*0.5)
x1 = random.randint(10, 246) + X
y1 = random.randint(10, 246) + Y
if(x1 + radius > X+half_width):
x1 = X+half_width - radius - 2
if(x1 - radius < X):
x1 = X + radius + 2
if(y1 + radius > Y+half_high):
y1 = Y+half_high - 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, obj_point = draw_fill_line(SCR_img, point_list_sum, any_smallimg, width, hight)
obj_points.append(obj_point)
return SCR_img, obj_points
def do_keypoints_action2(number, Gen_img_path, width, hight):
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) + ".txt")
# n = random.sample([1, 2, 3, 4], 1)
n = [1]
img, obj_points = draw_one_img(n[0], width, hight)
#保存图片
cv2.imwrite(img_save_path, img)
with open(label_save_path, "w") as f:
f.write(img_save_path)
f.write(" ")
for obj_point in obj_points:
f.write(str(obj_point[0]) + "," )
f.write(str(obj_point[1]) + "," )
f.write(str(obj_point[2]) + "," )
f.write(str(obj_point[3]) + "," )
f.write(str(obj_point[4]) + " " )
# 自带文件关闭功能,不需要再写f.close()
def sum_txt(path, txt_sum_path):
all_file = os.listdir(path)
with open(txt_sum_path, "w") as f:
for file in all_file:
if(file.split(".")[-1] == "txt"):
txt_file_path = os.path.join(path, file)
with open(txt_file_path, "r") as ff:
line = ff.readline()
f.write(line)
f.write("n")
if __name__ == '__main__':
number = 300
width = 640
hight = 640
Gen_img_path = "F:\000\test_obj\img_test"
txt_sum_path = "F:\000\test_obj\2007_val.txt"
do_keypoints_action2(number, Gen_img_path, width, hight)
sum_txt(Gen_img_path, txt_sum_path)