import sys
import os
import cv2
import numpy as np
import random
import time
import json
import base64
from math import cos,sin,pi,fabs,radians
# 读取添加模板
path = './image/'
"""
classes = ["Half open window", "Lighting doors windows", "Open door window_singular_right","Open door window_singular_left",
"Multiple open doors windows_no direction","Multiple open doors windows_left","Multiple open doors windows_right",
"Horizontal sliding door window_singular_right",
"Horizontal sliding door window_singular_left",
"Horizontal sliding door window_no direction","Folding doors windows_singular_right",
"Folding doors windows_singular_left","Folding doors windows_no direction"]
label1 = ['Multiple open doors windows_no direction', 'Multiple open doors windows_left',
'Multiple open doors windows_right', 'Folding doors windows_singular_right',
'Folding doors windows_singular_left', 'Folding doors windows_no direction']
"""
multiLabel_r=['Open door window_singular_right','Open door window_singular_right_1','Open door window_singular_right_s90']
multiLabel_l=['Open door window_singular_left','Open door window_singular_left_1','Open door window_singular_left_2']
label2 = ['Lighting doors windows', multiLabel_r, multiLabel_l,
'Half open window', 'Horizontal sliding door window_singular_right',
'Horizontal sliding door window_singular_left',"Horizontal sliding door window_no direction",
'Multiple open doors windows_no direction', 'Multiple open doors windows_left',
'Multiple open doors windows_right', 'Folding doors windows_singular_right',
'Folding doors windows_singular_left', 'Folding doors windows_no direction'
]
# label2=['Open door window_singular_left_2']
bc_random_xy = [100, 130, 200, 250] # 标尺左上点的随机位置范围,50~70标尺1左上点的x的范围,200~250是标尺1左上点的y的范围。标尺2类似。
ht_random_xy = [10, 40] # 贴图离边缘的随机范围
w_random = [1600, 3000] # 新建图的宽
h_random = [1400, 2800] # 新建图的高
bili_list = [0.8, 1.3] # bili > 0.8 and bili < 1.3
size_random=[0.8, 2.0] #随机resize大小的比例系数
bc_list=['./biaochi1.jpg','./biaochi2.jpg']#标尺图片
biaochi_bili=[1.2,1.6] #标尺比例
#读取json
def readJson(jsonfile):
with open(jsonfile,encoding='utf-8') as f:
jsonData = json.load(f)
return jsonData
#保存json
def writeToJson(filePath,data):
fb = open(filePath,'w')
fb.write(json.dumps(data,indent=2)) # ,encoding='utf-8'
fb.close()
def hetu_biaochi(image_bg, imgge):
"""
画标尺
:param image_bg:白色背景图
:param imgge:标尺模板图
:return:
randx:标尺在背景图上的左上点x坐标
cols:标尺的宽度
randy:标尺在背景图上的左上点y坐标
rows:标尺的高度
"""
moban_rows, moban_cols, moban_channels = imgge.shape
ImageBg_rows,ImageBg_cols,ImageBg_channels=image_bg.shape
if (moban_rows > moban_cols): # 是标尺1
randx = random.randint(bc_random_xy[0], bc_random_xy[1])
randy = random.randint(bc_random_xy[2], bc_random_xy[3])
# print("111",randx,randy)
# print(moban_rows/ImageBg_rows)
if moban_rows/ImageBg_rows<0.5:
size=random.uniform(biaochi_bili[0],biaochi_bili[1])
imgge = cv2.resize(imgge, None, fx=1.0, fy=size)
else:
pass
elif (moban_rows < moban_cols): # 是标尺2
# print("222", randx, randy)
if moban_cols/ImageBg_cols<0.5:
size=random.uniform(biaochi_bili[0],biaochi_bili[1])
imgge = cv2.resize(imgge, None, fx=size, fy=1.0)
else:
pass
moban_rows, moban_cols, moban_channels = imgge.shape
randx = random.randint(image_bg.shape[1] - moban_cols - bc_random_xy[3], image_bg.shape[1] - moban_cols - bc_random_xy[2])
randy = random.randint(image_bg.shape[0] - moban_rows - bc_random_xy[1], image_bg.shape[0] - moban_rows - bc_random_xy[0])
rows, cols, channels = imgge.shape
# print(randx,randy,rows + randy,cols + randx,imgge.shape,image_bg.shape)
roi = image_bg[randy:rows + randy, randx:cols + randx]
# print("cols:",cols,"rows:",rows,"randy:",randy,"randx:",randx)
# print(roi.shape,imgge.shape,image_bg.shape)
# cv2.imshow("roi",roi)
# cv2.imshow("imgge",imgge)
# cv2.waitKey(0)
imggray = cv2.cvtColor(imgge, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(imggray, 150, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img_bg = cv2.bitwise_and(roi, roi, mask=mask)
img_fg = cv2.bitwise_and(imgge, imgge, mask=mask_inv)
dst = cv2.add(img_bg, img_fg)
image_bg[randy:rows + randy, randx:cols + randx] = dst
return randx, cols, randy, rows
def hetu(bc_x, bc_y, image_bg, imgge):
"""
:param bc_x: 左边离标尺的距离
:param bc_y: 下边离标尺的距离
:param image_bg: 背景白图
:param imgge: 需要贴的图
:return:
randx:贴图在背景图上的左上点x坐标
cols:贴图的宽度
randy:贴图在背景图上的左上点y坐标
rows:贴图的高度
"""
rows, cols, channels = imgge.shape
randx = random.randint(bc_x + ht_random_xy[0], image_bg.shape[1] - cols - ht_random_xy[1])
randy = random.randint(ht_random_xy[1], image_bg.shape[0] - bc_y - rows - ht_random_xy[0])
roi = image_bg[randy:rows + randy, randx:cols + randx]
imggray = cv2.cvtColor(imgge, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(imggray, 150, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
img_bg = cv2.bitwise_and(roi, roi, mask=mask)
img_fg = cv2.bitwise_and(imgge, imgge, mask=mask_inv)
dst = cv2.add(img_bg, img_fg)
image_bg[randy:rows + randy, randx:cols + randx] = dst
return randx, cols, randy, rows
def compute_iou(rec1, rec2):
'''
计算IOU
:param rec1:
:param rec2:
:return:
iou:
'''
cy1, cx1, cy2, cx2 = rec1
gy1, gx1, gy2, gx2 = rec2
# 计算每个矩形的面积
S_rec1 = (cx2 - cx1) * (cy2 - cy1) # C的面积
S_rec2 = (gx2 - gx1) * (gy2 - gy1) # G的面积
# 计算相交矩形
x1 = max(cx1, gx1)
y1 = max(cy1, gy1)
x2 = min(cx2, gx2)
y2 = min(cy2, gy2)
w = max(0, x2 - x1)
h = max(0, y2 - y1)
area = w * h # C∩G的面积
iou = area / (S_rec1 + S_rec2 - area)
return iou
def genrateImage():
# 生成两张图片
bili = 1.0
while (True):
# 创建纯白图像
_w = random.randint(w_random[0], w_random[1])
_h = random.randint(h_random[0], h_random[1])
bili = _w / _h
if (bili > bili_list[0] and bili < bili_list[1]):
break
else:
continue
return _w, _h
def morphologyImage(temp):
"""
形态学处理
:param temp:
:return:
"""
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) # 矩形结构
erodeImage = cv2.erode(temp, kernel) # 膨胀
return erodeImage
def GaussProcess(temp):
"""
高斯模糊
:param temp:
:return:
"""
gaussImage = cv2.GaussianBlur(temp, (3, 3), 0)
return gaussImage
def rotate_bound(image, angle):
"""
旋转图像
:param image: 图像
:param angle: 角度
:return: 旋转后的图像
"""
h, w,_ = image.shape
# print(image.shape)
(cX, cY) = (w // 2, h // 2)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
M[0, 2] += (nW /
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
1111012.zip (2个子文件)
kuo.py 13KB
SegementJson.py 16KB
新建文件夹
共 2 条
- 1
资源评论
- m0_723419122023-11-27资源值得借鉴的内容很多,那就浅学一下吧,值得下载!
- 普通网友2022-09-10非常有用的资源,有一定的参考价值,受益匪浅,值得下载。
gu5218
- 粉丝: 31
- 资源: 259
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功