<jupyter_start><jupyter_text>Extract Image&Label Patches For Training and TestThis notebook is to extract Image&Label patches from original images and labels.The only thing you need to do, is to set up the `img_dir`, `ann_dir` and `out_dir`.Note: Please Run `Gen_BDist_Map.ipynb` at first.<jupyter_code>import glob
import os
from shutil import copyfile

import scipy.io as sio

import cv2
import numpy as np
import itertools
import matplotlib.pyplot as plt
from tqdm import tqdm

from misc.patch_extractor import PatchExtractor
from misc.utils import rm_n_mkdir

from config import Config
def bounding_box(img):
    rows = np.any(img, axis=1)
    cols = np.any(img, axis=0)
    rmin, rmax = np.where(rows)[0][[0, -1]]
    cmin, cmax = np.where(cols)[0][[0, -1]]
    # due to python indexing, need to add 1 to max
    # else accessing will be 1px in the box, not out 
    rmax += 1
    cmax += 1
    return [rmin, rmax, cmin, cmax]

def draw_contours(mask, ann_inst, line_thickness=1):
    overlay = np.copy((mask).astype(np.uint8))

    label_map = ann_inst
    instances_list = list(np.unique(label_map))  # get list of instances
    instances_list.remove(0)  # remove background
    contours = []
    for inst_id in instances_list:
        instance_map = np.array(
            ann_inst == inst_id, np.uint8)  # get single object
        y1, y2, x1, x2 = bounding_box(instance_map)
        y1 = y1 - 2 if y1 - 2 >= 0 else y1
        x1 = x1 - 2 if x1 - 2 >= 0 else x1
        x2 = x2 + 2 if x2 + 2 <= ann_inst.shape[1] - 1 else x2
        y2 = y2 + 2 if y2 + 2 <= ann_inst.shape[0] - 1 else y2
        inst_map_crop = instance_map[y1:y2, x1:x2]
        contours_crop = cv2.findContours(
            inst_map_crop, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        index_correction = np.asarray([[[[x1, y1]]]])
        for i in range(len(contours_crop[0])):
            contours.append(
                list(np.asarray(contours_crop[0][i].astype('int32')) + index_correction))
    contours = list(itertools.chain(*contours))
    cv2.drawContours(overlay, np.asarray(contours), -1, 2, line_thickness)
    return overlay
cfg = Config()

img_ext = '.png'
label_ext = '.mat'

extract_type = 'mirror' # 'valid' for fcn8 segnet etc.
                        # 'mirror' for u-net etc.
# check the patch_extractor.py 'main' to see the different

# orignal size (win size) - input size - output size (step size)
# 512x512 - 256x256 - 256x256 fcn8, dcan, segnet
# 536x536 - 268x268 - 84x84   unet, dist
# 540x540 - 270x270 - 80x80   xy, hover
# 504x504 - 252x252 - 252x252 micronetcd tr
step_size = [256, 256] # should match self.train_mask_shape (config.py) 
win_size  = [512, 512] # should be at least twice time larger than 
                       # self.train_base_shape (config.py) to reduce 
                       # the padding effect during augmentation

xtractor = PatchExtractor(win_size, step_size)

### Paths to data - these need to be modified according to where the original data is stored
img_ext = '.png'
img_dir = '/home1/gzy/NucleiSegmentation/High_CCRCC/Test/Images/'
ann_dir = '/home1/gzy/NucleiSegmentation/High_CCRCC/Test/Labels/' 
####
out_dir = "/home1/gzy/NucleiSegmentation/High_CCRCC/Test/%dx%d_%dx%d_dist" % \
                    (win_size[0], win_size[1], step_size[0], step_size[1])

file_list = glob.glob('%s/*%s' % (img_dir, img_ext))
file_list.sort()

rm_n_mkdir(out_dir)
for filename in tqdm(file_list):
    filename = os.path.basename(filename)
    basename = filename.split('.')[0]
    #print(filename)

    img = cv2.imread(img_dir + basename + img_ext)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
    labels = sio.loadmat(ann_dir + basename + '.mat')
    ann_inst = labels['instance_map']   
    ann_type = labels['class_map']
    ann_marker = labels['marker_map']*255
    
    ann = np.dstack([ann_inst, ann_type, ann_marker])
    ann = ann.astype('int32')
    
    img = np.concatenate([img, ann], axis=-1)
    sub_patches = xtractor.extract(img, extract_type)
    for idx, patch in enumerate(sub_patches):
        np.save("{0}/{1}_{2:03d}.npy".format(out_dir, basename, idx), patch)<jupyter_output>100%|██████████| 200/200 [00:57<00:00,  3.49it/s]
{'fraction_non_alphanumeric': 0.12263925412383457, 'fraction_numerical': 0.04590007171886206, 'mean_word_length': 3.1756487025948106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '611712', 'license': 'MIT', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1531, 'n_words': 393, 'path': 'src/.ipynb_checkpoints/Extract_Patches_Own_data-checkpoint.ipynb', 'repo_name': 'ZeyuGaoAi/Composite_High_Resolution_Network', 'size': '4183'}