Exemple #1
0
from skimage.segmentation import slic
from skimage.util import img_as_float
from skimage import io as skimageIO
from segraph import create_graph
import numpy as np

image = img_as_float(skimageIO.imread("test_2.png"))
segments = slic(image, n_segments=500, sigma=1.0)
# Create graph of superpixels
vertices, edges = create_graph(segments)

# Compute centers:
gridx, gridy = np.mgrid[:segments.shape[0], :segments.shape[1]]
centers = dict()
for v in vertices:
    centers[v] = [gridy[segments == v].mean(), gridx[segments == v].mean()]
print(centers)
def read_img(rownum, colnum, iput_img_original, gt_original, iput_img, gt,
             n_segments, max_n_superpxiel, patch_size):
    def crop_fun(new_input_padding, wi_ipt, hi_ipt, patch_size):
        patch_out = new_input_padding[wi_ipt:wi_ipt + patch_size,
                                      hi_ipt:hi_ipt + patch_size]
        return patch_out

    h_ipt = gt_original.shape[0]
    w_ipt = gt_original.shape[1]

    rowheight = h_ipt // rownum
    colwidth = w_ipt // colnum
    train_adj = []
    train_features = []
    train_labels = []
    train_patch = []
    test_adj = []
    test_features = []
    test_labels = []
    test_patch = []
    for r in range(1):  #
        for c in range(1):  #
            ALL_DATA_X_L = []
            ALL_DATA_Y_L = []

            segments = slic(iput_img, n_segments=n_segments, compactness=0.5)
            out = mark_boundaries(gt, segments)
            segments[segments > (max_n_superpxiel - 1)] = max_n_superpxiel - 1
            #得到每个super pixel的质心
            segments_label = segments + 1  #这里+1,是因为regionprops函数的输入要求是label之后的图像,而label的图像的区域编号是从1开始的
            region_fea = measure.regionprops(segments_label)

            #定义一个边界扩大的patch_size的空矩阵,主要是为了当super pixel位于图像边缘时,
            new_input_padding = np.zeros(
                (rowheight + patch_size, colwidth + patch_size))
            #把这个iput_img放到new_input_padding中

            new_input_padding[int(patch_size / 2):-int(patch_size / 2),
                              int(patch_size /
                                  2):-int(patch_size / 2)] = iput_img

            # Create graph of superpixels
            from segraph import create_graph
            vertices, edges = create_graph(segments)
            #print( r*rownum+c ,len(vertices))

            # settings for LBP
            radius = 3
            n_points = 8 * radius
            METHOD = 'uniform'
            lbp = local_binary_pattern(iput_img, n_points, radius, METHOD)
            img_feature = []

            #对所有的super pixel开始循环
            for ind_pixel in range(segments_label.max()):

                #计算当前superpixel的质心,为了生成切片,切片以这个质心为中心
                centriod = np.array(
                    region_fea[ind_pixel].centroid).astype("int32")
                wi_ipt = centriod[0]
                hi_ipt = centriod[1]

                #得到这个超像素的所有像素的坐标,根据坐标能够知道这个超像素在GT图中的所有像素值all_pixels_gt
                #根据所有的像素,得到哪一个像素值最多,例如【0,0,0】最多,那这个超像素的标签就是“河流”

                all_pixels_gt = gt[region_fea[ind_pixel].coords[:, 0],
                                   region_fea[ind_pixel].coords[:, 1]]
                n0 = np.bincount(all_pixels_gt[:, 0])
                n1 = np.bincount(all_pixels_gt[:, 1])
                n2 = np.bincount(all_pixels_gt[:, 2])
                gt_of_superp = [n0.argmax(),
                                n1.argmax(),
                                n2.argmax()]  #gt_of_superp这个超像素中出现最多次的像素值

                # red ---urban
                if gt_of_superp[0] >= 200 and gt_of_superp[
                        1] <= 50 and gt_of_superp[2] <= 50:
                    ALL_DATA_X_L.append(
                        crop_fun(new_input_padding, wi_ipt, hi_ipt,
                                 patch_size))
                    ALL_DATA_Y_L.append(0)

                # yellow ---farmland
                elif gt_of_superp[0] >= 200 and gt_of_superp[
                        1] >= 200 and gt_of_superp[2] <= 50:
                    ALL_DATA_X_L.append(
                        crop_fun(new_input_padding, wi_ipt, hi_ipt,
                                 patch_size))
                    ALL_DATA_Y_L.append(1)

                else:
                    ALL_DATA_X_L.append(
                        crop_fun(new_input_padding, wi_ipt, hi_ipt,
                                 patch_size))
                    ALL_DATA_Y_L.append(1)

                #计算每个超像素的color difference(cd), color histogram difference (hd) 和 texture disparity (lbpd)
                pixels_img = iput_img[
                    region_fea[ind_pixel].coords[:, 0],
                    region_fea[ind_pixel].coords[:, 1]]  #超像素内所有的像素
                cd = np.mean(pixels_img)
                hd, a = np.histogram(pixels_img, bins=64, range=[0, 255])
                lbp_a_supixel = lbp[region_fea[ind_pixel].coords[:, 0],
                                    region_fea[ind_pixel].coords[:, 1]]
                lbp_d, a = np.histogram(lbp_a_supixel.astype(np.int64),
                                        bins=n_points,
                                        range=[0, n_points])
                #所有超像素的featu10
                img_feature.append(np.concatenate(([cd], hd, lbp_d)))

            img_feature = np.array(img_feature)
            edges = np.array(edges)
            labels = np.array(ALL_DATA_Y_L)
            adj = sp.coo_matrix(
                (np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
                shape=(labels.shape[0], labels.shape[0]),
                dtype=np.float32)

            # build symmetric adjacency matrix
            adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
            features = normalize_features(img_feature)

            adj_propg = np.linalg.inv(
                sp.eye(adj.shape[0]).todense() -
                0.5 * normalize_adj(adj).todense())
            adj = normalize_adj(adj + sp.eye(adj.shape[0]))
            labels = torch.LongTensor(labels)

            adj = torch.FloatTensor(np.array(adj.todense()))
            adj_propg = torch.FloatTensor(np.array(adj_propg))
            #features = torch.FloatTensor(np.array(features.todense()))

            ALL_DATA_X_L = torch.FloatTensor(ALL_DATA_X_L)
            ALL_DATA_X_L = ALL_DATA_X_L.reshape(ALL_DATA_X_L.shape[0], 1,
                                                ALL_DATA_X_L.shape[1],
                                                ALL_DATA_X_L.shape[2])

    return adj, ALL_DATA_X_L, labels, adj_propg