File size: 3,404 Bytes
4526133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from torch.utils.data.dataset import Dataset
import numpy as np
import cv2
import os
from einops import rearrange, repeat, reduce
from spectral import *
from spectral import open_image
import random
import math
from sklearn.metrics import normalized_mutual_info_score
import warnings
warnings.filterwarnings('ignore')

def cutting_img_mask(img, mask, cutting=224):
    while 1:
        xx = random.randint(0, img.shape[0] - cutting)
        yy = random.randint(0, img.shape[1] - cutting)
        patch_img = img[xx:xx + cutting, yy:yy + cutting]
        patch_mask = mask[xx:xx + cutting, yy:yy + cutting]
        if patch_mask.sum() != 0:
            break
    img = patch_img
    mask = patch_mask
    return (img, mask)

def crop_spectral(img, spectral_channel=32):
    orign_channel = img.shape[-1]
    if orign_channel <= spectral_channel:
        return img
    else:
        start_idx = np.random.choice(np.arange(orign_channel - 32))
        img = img[:, :, start_idx:start_idx + spectral_channel]
    return img

class Data_Generate_Bile(Dataset):

    def __init__(self, img_paths, seg_paths, transform=None, channels=None, principal_bands_num=-1, cutting=-1, hyperhsi=False, cropspectral=-1):
        self.img_paths = img_paths
        self.seg_paths = seg_paths
        self.hyperhsi = hyperhsi
        self.transform = transform
        self.channels = channels
        self.principal_bands_num = principal_bands_num
        self.cutting = cutting
        self.cropspectral = cropspectral

    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = envi.open(img_path)[:, :, :]
        mask_path = self.seg_paths[index]
        mask = (cv2.imread(mask_path, 0) / 255).astype(np.uint8)
        if img.shape[:2] != mask.shape[:2]:
            mask = cv2.resize(mask, (img.shape[1], img.shape[0]))
        if self.transform != None:
            (img, mask) = self.transform((img, mask))
        if self.cutting > 0:
            (img, mask) = cutting_img_mask(img, mask, self.cutting)
        mask = mask.astype(np.uint8)
        img = img[:, :, self.channels] if self.channels is not None else img
        if self.cropspectral > 0:
            img = crop_spectral(img, self.cropspectral)
        if self.hyperhsi:
            pc = principal_components(img)
            pc_0999 = pc.reduce(fraction=0.999)
            img_pc = pc_0999.transform(img)
            img_pca = img_pc[:, :, 0]
            Ws = dict()
            for i in range(img.shape[-1]):
                Ws[i] = normalized_mutual_info_score(img_pca.reshape(-1), img[:, :, i].reshape(-1))
            indexs = sorted(Ws.items(), key=lambda x: x[1], reverse=True)
            channels = [indexs[i][0] for i in range(16)]
            img = img[:, :, channels]
        if self.principal_bands_num < img.shape[-1] and self.principal_bands_num > 0:
            pc = principal_components(img)
            pc_0999 = pc.reduce(fraction=0.999)
            img_pc = pc_0999.transform(img)
            img = img_pc[:, :, :self.principal_bands_num]
            img = np.transpose(img, (2, 0, 1))
        else:
            img = np.transpose(img, (2, 0, 1))
        img = img.astype(np.float32)
        mask = mask.astype(np.float32)
        return (img, mask[None])

    def __len__(self):
        return len(self.img_paths)