先是题目,应该是要再def merge(images, annotations_file):填的
balloon.py
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from opencv.coding_pentest.opencvjson import allll
"""
需求:
1、在尺寸为 2048 * 2048 的黑色背景图片中,依次填充 images 中的气球图片,气球图
片的间隔至少大于 20 个像素,填充完成并保存,如果填充溢出,可保存多张图片。。
2、annotations.json 中对应每一个气球的标注的掩膜坐标信息,请根据 annotations
中的格式生成新的 json 标注文件,并确保新生成的 json 标注文件里的掩膜坐标信息对
应需求 1 中保存的图片。
3、请验证需求 2 中新生成 json 文件掩膜标注信息的正确性。
"""
def merge(images, annotations_file):
pass
def merge():
allll()
pass
merge()
opencvjson.py第二第三问就是一个线性变换,第116行去掉注释可验证第三问
# -*- coding: utf-8 -*-
import cv2
import os
import numpy as np
import json
def cv_show(name, img, k=0, width=512, height=512):
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(name, width, height)
cv2.imshow(name, img)
cv2.waitKey(k)
def letter_box(image, dim, interval=0):
h, w = dim
h -= interval * 2
w -= interval * 2
height, width = image.shape[0], image.shape[1]
ratio = min(w/width, h/height)
nw = int(width * ratio)
nh = int(height * ratio)
dh = (h-nh)//2
dw = (w-nw)//2
resized_image = cv2.resize(image, (nw, nh))
output = np.zeros((dim[0], dim[1], 3), dtype=np.uint8)
output[dh+interval:dh+interval+nh, dw+interval:dw+interval+nw, :] = resized_image
return output, ratio, dh, dw
def merge1(a,num):
# 输入图片
imgName = 'merge_0.jpg'
if(num>15):
imgName = 'merge_1.jpg'
img = cv2.imread(imgName)
# 展示原图
#cv2.imshow("img", img)
# 创建掩膜
mask = np.zeros(img.shape[:2], dtype=np.uint8)
#a=[[27, 175], [510, 152], [519, 471], [24, 474]]
#a=[[156, 160, 164, 169, 172, 180, 188, 196, 204, 209, 212, 223, 236, 262, 284, 304, 324, 351, 373, 391, 413, 435, 454, 461, 472, 479, 486, 493, 499, 499, 498, 491, 484, 476, 467, 456, 437, 407, 386, 362, 341, 316, 288, 264, 236, 216, 196, 180, 166, 153, 141, 133, 122, 113, 109, 109, 114, 118, 125, 136, 147],[405, 411, 417, 422, 426, 433, 441, 447, 452, 455, 457, 460, 463, 464, 462, 456, 449, 436, 422, 409, 389, 366, 341, 330, 311, 295, 275, 246, 211, 180, 157, 129, 108, 92, 77, 63, 45, 24, 14, 6, 1, 0, 2, 7, 19, 30, 46, 60, 79, 98, 119, 132, 162, 198, 228, 257, 292, 318, 343, 369, 392]]
#a=np.transpose(a)
polygon = np.array(a, np.int32) # 坐标为顺时针方向
cv2.fillConvexPoly(mask, polygon, (255, 255, 255))
image = cv2.add(img, np.zeros(np.shape(img), dtype=np.uint8), mask=mask)
# 展示掩膜图片
#cv2.imshow("mask", mask)
# 展示添加掩膜效果图片
cv2.imshow("image", image)
cv2.waitKey()
cv2.destroyAllWindows()
#cv2.imwrite('01.png',mask)
def allll():
root_path = './images/'
names = os.listdir(root_path)
imgs = [cv2.imread(os.path.join(root_path, name)) for name in names]
merge_size = (2048, 2048) # 背景图尺寸
rows = 4 # 行数设定
columns = 4 # 列数设定
interval = 20 # 图片间的最小间隔
# 计算子图需要resize的尺寸
assert merge_size[0] % columns == 0 and merge_size[1] % rows == 0, '%s or %s is not multiples of %s' % (rows, columns, merge_size)
h = merge_size[0] // columns
w = merge_size[1] // rows
# 计算需要几张背景图
if len(names) <= rows*columns:
num = 1
else:
if len(names) % (rows*columns) == 0:
num = len(names)//(rows*columns)
else:
num = len(names)//(rows*columns)+1
canvas = np.zeros((num, merge_size[0], merge_size[1], 3), dtype=np.uint8)
f = open('annotations.json', 'r')
content = f.read()
json_in = json.loads(content)
print(type(json_in))
with open('1.txt', 'a') as f: # 'a'表示append,即在原来文件内容后继续写数据(不清楚原有数据)
f.write("{")
#print(json_in['%s' % (names[0])]['all_points_x'])
content_s="{"
# 合并图片
cnt = 0
for n in range(len(names)):
print(names[n])
for k in range(num):
for j in range(columns):
for i in range(rows):
if cnt == len(names):
break
canvas[k][j*h:(j+1)*h, i*w:(i+1)*w, :], ratio, dh, dw = letter_box(imgs[cnt+n//(rows*columns)], (h, w), interval=interval)
# TODO: 读入json数据,通过ratio, dw, dh, interval计算合并后的新位置
# ratio是压缩比例a, b是每张图的左上角xy坐标, y = ax + b
# dw=90,dh=0
# b0x=20i+90,b0y=20i+0
a=[(json_in['%s'%(names[cnt])]['all_points_x']),(json_in['%s'%(names[cnt])]['all_points_y'])]
a=np.transpose(a)
for lo in range(a.shape[0]):
a[lo][0]=a[lo][0]*ratio+20+512*(cnt%16%4)+dw
a[lo][1]=a[lo][1]*ratio+20+512*(cnt%16//4)+dh
#将下一行取消注释可验证第三问
#merge1(a,cnt)
a = np.transpose(a)
print(a[0])
content_s +=('"'+names[cnt]+'":')
dict_out1 = dict(name="polygon",all_points_x=a[0].tolist(),all_points_y=a[1].tolist())
print(dict_out1)
content_s += (str(dict_out1))+","
# cv_show('black', canvas[k])
cnt += 1
cv2.imwrite('merge_%s.jpg' % k, canvas[k])
print('cnt: ', cnt)
content_s =content_s[:-1]
content_s += "}"
content_s=content_s.replace("\'",'\"')
with open('1.txt', 'r+') as f:
f.writelines(content_s)
f.close()
os.rename("1.txt", "1.json")