p y t h o n 使 用 o p e n c v 进 行 数 据 特 征 融 合 python使用opencv进行数据特征融合 python使用opencv进行数据特征融合
缺陷库准备
import os
import json
import base64
import numpy as np
from PIL import Image
from labelme import utils
import cv2
''''
1.读取标签json和原图
'''
json_path = r".\annotations\38.json"
img_path = r".\images\38.bmp"
label_name_to_value = {'_background_': 0}
json_data = json.load(open(json_path))
with open(img_path, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
''''
2.根据json,获取描边的最小外接矩形,然后进行抠图,获取原图和mask
'''
'''
构建类名和label的字典-label_name_to_value
'''
for shape in json_data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
print('Found new class name:{}/ value: {}'.format(label_name, label_value))
lbl, _ = utils.shapes_to_label(img.shape, json_data['shapes'], label_name_to_value)
lbl = lbl*255
lbl = lbl.astype(np.uint8)
cv2.imwrite(r"./label_masks/mask.jpg",lbl)
contours, hierarchy = cv2.findContours(lbl, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
offset = 10
for c in contours:
if len(c) < 10:
continue
x, y, w, h = cv2.boundingRect(c)
crop = img[y-offset:y + h+offset, x-offset:x + w+offset]
mask = lbl[y-offset:y + h+offset, x-offset:x + w+offset]
cv2.imwrite('./defects/crop.jpg', crop)
cv2.imwrite('./masks/mask.jpg', mask)
单个特征融合
import cv2
import numpy as np
from math import sqrt
from PIL import Image
img_back = Image.open(r".\images\85.bmp")
img_back = cv2.cvtColor(np.asarray(img_back), cv2.COLOR_RGB2BGR)
img_obj = cv2.imread(r"./defects/crop.jpg")
mask = cv2.imread(r"./masks/mask.jpg",0)
width,height,channels = img_back.shape
loaction = (height//2,width//2)
normal_clone = cv2.seamlessClone(img_obj,img_back,mask,loaction,cv2.NORMAL_CLONE)
normal_clone = cv2.seamlessClone(img_obj,normal_clone,mask,(height//4,width//2),cv2.NORMAL_CLONE)
cv2.imwrite("./out/normal_clone.jpg",normal_clone)
raw_img = cv2.imread(r"D:\workplace\python\RealAug\label_masks\mask.jpg",0)
w,h = mask.shape
start_x = int(width//2 -w/2)
start_y = int(height//2 - h/2)
for i in range(w):
for j in range(h):
raw_img[start_x + i, start_y + j] = mask[i, j]
cv2.imwrite("label_mask.jpg",raw_img)

随机生成坐标点
import numpy as np
point_num = 10
'''
随机生成坐标
'''
width = 1200
height = 800
X = np.random.rand(1, point_num) * width
X = X.flatten()
Y = np.random.rand(1, point_num) * height
Y = Y.flatten()
points = []
for i in range(len(X)):
points.append((int(X[i]),int(Y[i])))
print(points)
随机多特征融合
'''
1.读取标签json和原图,获取
'''
import cv2
import numpy as np
from math import sqrt
from PIL import Image
img_back = Image.open(r".\images\85.bmp")
img_back = cv2.cvtColor(np.asarray(img_back), cv2.COLOR_RGB2BGR)
img_obj = cv2.imread(r"./defects/crop.jpg")
mask = cv2.imread(r"./masks/masks.jpg")
point_num = 100
'''
随机生成坐标
'''
width,height,channels = img_back.shape
X = np.random.rand(1, point_num) * width
X = X.flatten()
Y = np.random.rand(1, point_num) * height
Y = Y.flatten()
points = []
for i in range(len(X)):
points.append((int(X[i]),int(Y[i])))
for i_location in points:
try:
img_back = cv2.seamlessClone(img_obj, img_back, mask, i_location, cv2.NORMAL_CLONE)
cv2.imwrite("./out/normal_clone.jpg", img_back)
except Exception as e:
print(e)
5生成融合后分割json
import os
import glob
import cv2
import json
'''
1.读取图像,获取图像的宽高
'''
mask_lbl = cv2.imread(r"D:\workplace\python\RealAug\label_masks\label_mask.jpg",0)
'''
2.findcontour,获取mask的所有点
'''
contours, hierarchy = cv2.findContours(mask_lbl, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
list_rect = []
list_polygon_points = []
for c in contours:
if len(c) < 10:
continue
'''
生成检测任务的检测json
'''
x, y, w, h = cv2.boundingRect(c)
list_rect.append((x, y, w, h))
'''
生成分割任务的分割json
'''
points = c[ :, 0, :]
list_polygon_points.append(points)
'''
3.对分割任务的分割json文件进行赋值
'''
json_str = {}
json_str['version'] = "0.0.1"
json_str['flags'] = {}
shapes = []
width, height = mask_lbl.shape
imagePath = ""
imageData = ""
cls_name = "dc"
group_id = 1
for index_countour in list_polygon_points:
shape = {}
shape['points'] = index_countour.tolist()
shape['label'] = cls_name
shape['group_id'] = group_id
shape['shape_type'] = "polygon"
shapes.append(shape)
json_str['shapes'] = shapes
json_str['imagePath'] = imagePath
json_str['imageData'] = imageData
json_str['imageHeight'] = height
json_str['imageWidth'] = width
'''
4.生成json
'''
json_file = os.path.join("./", 'out1.json')
with open(json_file, 'w') as f:
json.dump(json_str, f, indent=2)
{
"version": "0.0.1",
"flags": {},
"shapes": [
{
"points": ,
"label": "dc",
"group_id": 1,
"shape_type": "polygon"
},
{
"points": ,
"label": "dc",
"group_id": 1,
"shape_type": "polygon"
}
],
"imagePath": "",
"imageData": "",
"imageHeight": 5120,
"imageWidth": 2000
}
6生成融合后检测json
import os
import cv2
import json
'''
1.读取图像,获取图像的宽高
'''
mask_lbl = cv2.imread(r"D:\workplace\python\RealAug\label_masks\label_mask.jpg",0)
width, height = mask_lbl.shape
image_data = None
img_path = ""
'''
2.findcontour,获取mask的所有点
'''
contours, hierarchy = cv2.findContours(mask_lbl, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
list_rect = []
list_polygon_points = []
for c in contours:
if len(c) < 10:
continue
'''
生成检测任务的检测json
'''
x, y, w, h = cv2.boundingRect(c)
list_rect.append([x, y, w, h])
'''
3.对检测任务的检测json文件进行赋值
'''
json_res = []
json_str = {}
annotations = []
width, height = mask_lbl.shape
imagePath = ""
imageData = ""
cls_name = "dc"
group_id = 1
for index_rect in list_rect:
annotation = {}
annotation['label'] = cls_name
annotation['group_id'] = group_id
coordinates = {}
coordinates['x'] = index_rect[0]
coordinates['y'] = index_rect[1]
coordinates['width'] = index_rect[2]
coordinates['height'] = index_rect[3]
annotation['coordinates'] = coordinates
annotations.append(annotation)
json_str['image'] = "Image"
json_str['annotations'] = annotations
json_str['isReasoned'] = False
json_res.append(json_str)
'''
4.生成json
'''
json_file = os.path.join("./", 'out_rect.json')
with open(json_file, 'w') as f:
json.dump(json_res, f, indent=2)
[
{
"image": "Image",
"annotations": [
{
"label": "dc",
"group_id": 1,
"coordinates": {
"x": 2904,
"y": 1392,
"width": 128,
"height": 104
}
},
{
"label": "dc",
"group_id": 1,
"coordinates": {
"x": 2496,
"y": 948,
"width": 131,
"height": 107
}
}
],
"isReasoned": false
}
]
Frm1_缺陷录入
Frm2_缺陷增强
Frm3_选取融合