python使用opencv进行数据特征融合

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
        # classes=1,2,3...   label_value=1,2,3...
        print('Found new class name:{}/ value: {}'.format(label_name, label_value))

# 构建mask
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)  # 计算点集最外面的矩形边界
    # cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)  # 红,画框

    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")
# 掩膜,确定贴图的ROI区域(0表示不需要的区域,255表示需要贴合的区域)
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.rectangle(normal_clone, loaction, (height//2+100,width//2+100), (255, 0, 255), -1)
# 保存贴合图
cv2.imwrite("./out/normal_clone.jpg",normal_clone)
# 获取mask原图
raw_img = cv2.imread(r"D:\workplace\python\RealAug\label_masks\mask.jpg",0)
# 获取mask的宽高
w,h = mask.shape
# 获取贴合起始位置
start_x = int(width//2 -w/2)
start_y = int(height//2 - h/2)
# mask进行贴合
for i in range(w):
    for j in range(h):
        raw_img[start_x + i, start_y + j] = mask[i, j]
# 保存label_mask
cv2.imwrite("label_mask.jpg",raw_img)

python使用opencv进行数据特征融合_第1张图片

随机生成坐标点

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)
# print(Y)

随机多特征融合

'''

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")
# 掩膜,确定贴图的ROI区域(0表示不需要的区域,255表示需要贴合的区域)
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)):
    # 在这里进行筛选
    # 简单阈值分割确定ROI区域,非ROI区域进行去除
    points.append((int(X[i]),int(Y[i])))
# print(points)
# print(Y)
# 进行贴合
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)
# cv2.drawContours(mask_lbl,contours,-1,(0,0,255),3)
# 检测框label列表
list_rect = []
# 分割点集列表
list_polygon_points = []
# 遍历所有轮廓,进行筛选
for c in contours:
    # 去除非轮廓
    if len(c) < 10:
        continue
    '''
    生成检测任务的检测json
    '''
    # 计算点集最外面的矩形边界,用于生成检测json
    x, y, w, h = cv2.boundingRect(c)
    list_rect.append((x, y, w, h))
    '''
    生成分割任务的分割json
    '''
    # 去除轮廓c的中间维度
    points = c[ :, 0, :]
    list_polygon_points.append(points)


'''
3.对分割任务的分割json文件进行赋值
'''
# json文件
json_str = {}
# 版本
json_str['version'] = "0.0.1"
# flags
json_str['flags'] = {}
# 所有点位
shapes = []
# 宽高
width, height = mask_lbl.shape
# 图像路径
imagePath = ""
# 图像数据
imageData = ""
# 类别名称
cls_name = "dc"
# id号
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)
# cv2.drawContours(mask_lbl,contours,-1,(0,0,255),3)
# 检测框label列表
list_rect = []
# 分割点集列表
list_polygon_points = []
# 遍历所有轮廓,进行筛选
for c in contours:
    # 去除非轮廓
    if len(c) < 10:
        continue
    '''
    生成检测任务的检测json
    '''
    # 计算点集最外面的矩形边界,用于生成检测json
    x, y, w, h = cv2.boundingRect(c)
    # 左上角点和右下角点
    list_rect.append([x, y, w, h])


'''
3.对检测任务的检测json文件进行赋值
'''
json_res = []
# json文件
json_str = {}
# 所有点位
annotations = []
# 宽高
width, height = mask_lbl.shape
# 图像路径
imagePath = ""
# 图像数据
imageData = ""
# 类别名称
cls_name = "dc"
# id号
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_选取融合

你可能感兴趣的:(python,opencv,计算机视觉)