Python-based boundary extraction program

Recently, I need to use the extended data set BSD of VOC-2012, but there is no corresponding boundary image, so I use python to process the mat file to get the corresponding boundary image file.

The first is to get the mat file

import numpy as np
from scipy import io
import scipy.io


def edge(ID):
    ##将语义分割图像转为边界图,ID表示mat文件名,从txt文件中读取
    #加载mat文件
    a = scipy.io.loadmat('D:\\datasets\\cls\\' + ID + '.mat')
    #提取mat文件中的图像矩阵
    b = a['GTcls']['Segmentation']
    d = b[0][0]
    #计算矩阵的大小,长×宽,h×w
    h = len(d)
    w = len(d[0])
    #创建新的0矩阵
    f = np.zeros((h, w), dtype=np.int)

    #遍历d,找出边界点
    for i in range(h - 1):
        for j in range(w - 1):
            if d[i][j] != 0:
                if d[i][j] == d[i - 1][j] == d[i + 1][j] == d[i][j - 1] == d[i][j + 1] == d[i - 1][j - 1] == d[i - 1][j + 1] == d[i + 1][j - 1] == d[i + 1][j + 1]:
                    continue
                else:
                    f[i][j] = 1
                continue
    #将边界矩阵f传回mat文件
    b[0][0] = f
    a['GTcls']['Segmentation'] = b
    #保存新的mat文件
    scipy.io.savemat('D:\\datasets\\edge_val\\' + ID + '.mat', a)

fin = open('D:\\datasets\\val.txt')
for line in fin:
    ID = line.strip()
    edge(ID)
    print(ID)

The size of the mat file after python processing will become very large, you can go to matlab to load and save the solution (the batch code is in another blog)

Since not all visual images are required, this procedure is separated.

from PIL import Image
import scipy.io

_target = Image.fromarray(scipy.io.loadmat('D:\\datasets\\benchmark\\benchmark\\benchmark_RELEASE\dataset\\SBD_edge\\2007_006490.mat')["GTcls"][0]['Segmentation'][0])
######   .convert('L')
_target.save('D:\\datasets\\009.png')
_target.show()

Guess you like

Origin blog.csdn.net/qq_36804414/article/details/107891767