Realize image cropping and fusion.

Please refer to the previous blog link:

https://blog.csdn.net/Helloorld_1/article/details/130107465?spm=1001.2014.3001.5502

How to achieve cropping pictures.

This article mainly describes how to use SAM (Segment Anything) to fuse the cropped pictures

Results as shown below:

 Use sam to cut out the image, and then fuse the image with the image you want to use as the background, so that everything can be cut out.

Inspired by this blog:

Automatic labeling using the Segment Anything (SAM) model - Programmer Sought

Then the specific configuration environment is as mentioned in the above blog content, here I put the code of my fusion image for your reference:

import cv2
import numpy as np

img1 = cv2.imread('output.jpg')#前景图像
img2 = cv2.imread('2.jpg')#背景图像
rows, cols = img1.shape[:2]#图像尺寸大小
img2=cv2.resize(img2, (cols, rows)) # 裁剪背景图像到合适的大小

mask=cv2.imread('s.jpg')#蒙版图像
imgray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
# 根据轮廓信息提取掩模
for i in range(rows):
    for j in range(cols):
        if mask[i,j].sum() >20:
            img2[i,j]=[0,0,0]

img_mix = cv2.addWeighted(img2,1,img1,1,0)#融合图像
# img_mix=img1+img2
# cv2.imshow('mask',mask)
# cv2.imshow('img1', img1)
# cv2.imshow('img2', img2)
cv2.imshow('img_mix', img_mix)

cv2.waitKey(0)
cv2.destroyAllWindows()

In addition, for the annotations in the above blog as json files, convert them into txt file information for use by the annotation tool labelimg

code show as below:

# 处理同一个数据集下多个json文件时,仅运行一次class_txt即可
import json
import os
import cv2

"存储标签与预测框到txt文件中"
def json_txt(json_path, txt_path):
    "json_path: 需要处理的json文件的路径"
    "txt_path: 将json文件处理后txt文件存放的文件夹名"
    # 生成存放json文件的路径
    if not os.path.exists(txt_path):
        os.mkdir(txt_path)
    # 读取json文件
    with open(json_path, 'r') as f:
        dict = json.load(f)
    # 得到images和annotations信息
    images_value = dict.get("categories")  # 得到某个键下对应的值
    annotations_value = dict.get("annotations")  # 得到某个键下对应的值
    # 使用images下的图像名的id创建txt文件
    list=[]  # 将文件id存储在list中
    filename="a"
    img_size = dict.get("images")  # 得到某个键下对应的值
    numh = 1
    numw = 1

    for z in img_size:
        numh=z.get('height')
        numw=z.get('width')
    for i in images_value:
        open(txt_path + str(i.get("name")) + '.txt', 'w')
        list.append(i.get("id"))
        filename=txt_path + str(i.get("name")) + '.txt'

    imgSize = [numw, numh]
    # 将id对应图片的bbox写入txt文件中
    for i in list:
        for j in annotations_value:
            if j.get("image_id") == i:
                # bbox标签归一化处理
                ori_coords = j.get('bbox')# [x,y,w,h]获取检测框
                # print((ori_coords))
                leftup = [round((x + ori_coords[i + 2] / 2) / imgSize[i],6) for i, x in enumerate(ori_coords[:2])]
                wh = [round(x / imgSize[i],6) for i, x in enumerate(ori_coords[2:])]
                coord = [leftup[0], leftup[1], wh[0], wh[1]]  # 最终坐标
                with open(filename, 'a') as file1:  # 写入txt文件中
                    print(j.get("category_id"), coord[0], coord[1], coord[2], coord[3], file=file1)


"将id对应的标签存储在class.txt中"
def class_txt(json_path, class_txt_path):
    "json_path: 需要处理的json文件的路径"
    "txt_path: 将json文件处理后存放所需的txt文件名"
    # 生成存放json文件的路径
    with open(json_path, 'r') as f:
        dict = json.load(f)
    # 得到categories下对应的信息
    categories_value = dict.get("categories")  # 得到某个键下对应的值
    print(categories_value)
    # 将每个类别id与类别写入txt文件中
    with open(class_txt_path, 'a') as file0:
        for i in categories_value:
            print( i.get('name'), file=file0)


json_txt("./json/annotations.json", "txt_label/")#自己在当前目录下,创建一个txt_label文件夹
class_txt("./json/annotations.json", "txt_label/classes.txt")

According to everyone's needs, I put the project files in the link below, and you can pick them up if you need them:

Universal cutout magic modification based on SAM and other people's UI (for exchange and learning only) resources - CSDN Library

Since the uploaded file cannot exceed 1G, there is no vit-h file in the compressed package. If you want to run it, please download another one

sam_vit_h_4b8939.pth

Put this file in the same directory as segment_anything_annotator.py.

Environment configuration, I have exported my environment as environment.yaml file

Anconda only needs to use the following instructions to quickly create an environment

conda activate base #先激活环境

conda env create -f environment.yaml #复制环境,注意目录哟!

Then just run segment_anything_annotator.py

In addition, here is a description of the folders in Yiha:

 .idea is generated by pycharm, don’t worry

images means that if you want to use your own pictures for segmentation, just put the pictures in it.

The json file is described in the reference blogger, so I won’t explain too much here

Salt is the essence, including segmentation, UI interface, and the .py of fused images are in it

txt_label and transformer are what I want to convert the reference blogger, the json data format of the file into the txt data format, it has nothing to do with the segmentation! ! !

You can leave a message in the comment area if you have any questions.

Guess you like

Origin blog.csdn.net/Helloorld_1/article/details/130499156