Example #1
0
    def __getitem__(self, index):

        # read image and simulate Wishart noise
        img = cv2.imread(self.files_path[index]).astype(np.float32)
        # print(f'file path {self.files_path[index]}')
        img = img[:256, :256, :]  # ensure can be divede by 32
        img[img < 1] += 1  # avoid zero value

        if self.augments:
            img = self.augments(torch.from_numpy(img))

        if isinstance(img, torch.Tensor):
            img = img.numpy()
        img, noise = simulate.generate_Wishart_noise_from_img(img, self.ENL)

        # pauli_img = psr.rgb_by_c3(img, type='sinclair')
        # pauli_noise = psr.rgb_by_c3(noise, type='sinclair')
        # cv2.imwrite(osp.join(_TMP_PATH, 'pauli_img.jpg'), cv2.cvtColor((255*pauli_img).astype(np.uint8), cv2.COLOR_RGB2BGR))
        # cv2.imwrite(osp.join(_TMP_PATH, 'pauli_noise.jpg'), cv2.cvtColor((255*pauli_noise).astype(np.uint8), cv2.COLOR_RGB2BGR))

        # hoekman decomposition
        img = psr.Hokeman_decomposition(img)
        noise = psr.Hokeman_decomposition(noise)

        # log transform
        if self.log:
            img = np.log(img) + self.log_compensation
            noise = np.log(noise) + self.log_compensation

        # to pytorch
        img = torch.from_numpy(img)
        noise = torch.from_numpy(noise)

        return img, noise, self.files_path[index]
Example #2
0
def hoekman(src_path: str, norm=False) -> None:
    ''' Hoekman decomposition and save to file, the hoekman_and_norm() is an older version of this func, and is a subset of this func
    @in     -src_path       -source path, where should contains 'C3' folder
    @in     -norm           -normalize or not, default: false
    '''
    if 'C3' in os.listdir(src_path):
        # read C3 file
        print(f'hoekman on dir (norm={norm}): {src_path}', end='')
        c3 = psr.read_c3(osp.join(src_path, 'C3'))
        h = psr.Hokeman_decomposition(c3)

        dst_path = osp.join(src_path, 'Hoekman')
        fu.mkdir_if_not_exist(dst_path)
        # np.save(osp.join(dst_path, 'ori'), h)                 # save the unnormalized file

        # normalize
        if norm:
            for ii in range(9):
                h[ii, :, :] = psr.min_max_contrast_median_map(
                    10 * np.log10(h[ii, :, :]))
                # cv2.imwrite(osp.join(dst_path, f'{ii}.jpg'), (h[0, :, :]*255).astype(np.uint8))
                # plt.hist() can take very long time to process a 2D array, but little time to process a 1D array, so flatten the array if possible
                # plt.hist(h[ii, :, :].flatten())
                # plt.savefig(osp.join(dst_path, f'log-hist-{ii}.jpg'))

        # save to file
        if norm:
            np.save(osp.join(dst_path, 'normed'), h)
        else:
            np.save(osp.join(dst_path, 'unnormed'), h)
        print('\tdone')

    else:
        raise ValueError('wrong src path')
Example #3
0
    def __getitem__(self, index: int):
        label, mask = self.get_label_and_mask(index)

        # get the file path
        label_path = self.labels_path[index]
        label_dir = osp.split(label_path)[0]
        # two date time display format
        if osp.isfile(osp.join(label_dir, 'pin.txt')):  # mode 1
            re_exp = r'20\d{6}'
        elif osp.isfile(osp.join(label_dir, 'pin-2.txt')):  # mode 2
            re_exp = r'\d{2}[A-Z][a-z]{2}\d{4}'
        else:
            raise ValueError('unknown mode of label format')
        [a_time, b_time] = re.findall(re_exp, label_path)

        file_path = []
        file_path.append(
            osp.join(label_dir.replace('label', 'data'), a_time, 'C3'))
        file_path.append(
            osp.join(label_dir.replace('label', 'data'), b_time, 'C3'))

        # get the file data
        slice_idx = re.search(r'-\d{4}-', label_path)
        if slice_idx is None:
            raise ValueError('can not find the wave code')
        slice_idx = int(slice_idx.group()[1:5])
        file = []
        for ii in range(2):
            tmp = psr.read_c3(osp.join(file_path[ii], str(slice_idx)))
            tmp = psr.Hokeman_decomposition(tmp)
            # for jj in range(9):
            #     tmp[jj, :, :] = psr.min_max_contrast_median_map(10*np.log10(tmp[jj, :, :]))
            tmp = np.log10(tmp)
            file.append(tmp)

        if self.time_shuffle and np.random.binomial(1, 0.5):
            return torch.from_numpy(file[0]), torch.from_numpy(
                file[1]), label, mask
        else:
            return torch.from_numpy(file[1]), torch.from_numpy(
                file[0]), label, mask
def hoekman_and_norm(src_path: str) -> None:
    ''' hoekman decomposition, normalization as pauliRGB, and save to file
    @in     -src_path       -source path, where should contains 'C3' folder
    '''
    if 'C3' in os.listdir(src_path):
        print(f'hoekman and norm on dir: {src_path}', end='')
        c3 = psr.read_c3(osp.join(src_path, 'C3'))
        h = psr.Hokeman_decomposition(c3)

        dst_path = osp.join(src_path, 'Hoekman')
        fu.mkdir_if_not_exist(dst_path)
        # np.save(osp.join(dst_path, 'ori'), h)                 # save the unnormalized file

        for ii in range(9):
            h[ii, :, :] = psr.min_max_contrast_median_map(
                10 * np.log10(h[ii, :, :]))
            # cv2.imwrite(osp.join(dst_path, f'{ii}.jpg'), (h[0, :, :]*255).astype(np.uint8))
            # plt.hist() can take very long time to process a 2D array, but little time to process a 1D array, so flatten the array if possible
            # plt.hist(h[ii, :, :].flatten())
            # plt.savefig(osp.join(dst_path, f'log-hist-{ii}.jpg'))
        np.save(osp.join(dst_path, 'normed'), h)
        print('\tdone')
    else:
        raise ValueError('wrong src path')