def sample_patches_retouch_mask(img, mask=None, pshape=(224, 224), npos=10, nneg=1, pos=255, neg=0, patch_border=12): """ Generate patches from the interesting region of the OCT slice :param img: oct image slice :param mask: oct segmentation GT :param pshape: patch shape :param npos: Number of patches to sample from interesting region :param nneg: Number of patches to sample from non interesting region :param pos: Mask value indicating positives :param neg: Mask value indicating negative :param patch_border: boder to ignore when creating IRF,SRF,PED labels for patches (ignore border pixels for predicting labels) :return: """ assert mask.dtype == np.uint8 roi_mask = np.logical_not(mask == 0) it = ni.sample_patch_centers(roi_mask, pshape=pshape, npos=npos, nneg=nneg, pos=pos, neg=neg) for r, c, label in it: img_patch = ni.extract_patch(img, pshape, r, c) mask_patch = ni.extract_patch(mask, pshape, r, c) label_IRF = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == IRF_CODE)) label_SRF = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == SRF_CODE)) label_PED = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == PED_CODE)) yield img_patch, mask_patch, label_IRF, label_SRF, label_PED
def test_extract_patch(): expected = np.array([[5, 6, 7], [9, 10, 11]], dtype='uint8') image = np.reshape(np.arange(16, dtype='uint8'), (4, 4)) patch = ni.extract_patch(image, (2, 3), 2, 2) nt.assert_allclose(expected, patch)
def sample_patches_entropy_mask(img, mask=None, roimask=None, pshape=(224, 224), npos=10, nneg=1, pos=255, neg=0, patch_border=12): """ Generate patches from the interesting region of the OCT slice :param img: oct image slice :param mask: oct segmentation GT :param roimask: oct ROI :param pshape: patch shape :param npos: Number of patches to sample from interesting region :param nneg: Number of patches to sample from non interesting region :param pos: Mask value indicating positives :param neg: Mask value indicating negative :param patch_border: boder to ignore when creating IRF,SRF,PED labels for patches (ignore border pixels for predicting labels) :return: """ # y_min, y_max = calculate_oct_y_range(img) # roi_mask = np.zeros(mask.shape, dtype=np.int8) # # print y_min, y_max # roi_mask[y_min:y_max, :] = pos # # TODO : Why 32? # roi_mask[y_min:y_min + 32, :] = 0 # roi_mask[y_max - 32:y_max, :] = 0 # # plt.imshow(img) # # print np.max(roi_mask), np.min(roi_mask) roimask = roimask.astype(np.int8) # it = ni.sample_patch_centers(roimask, pshape=pshape, npos=npos, nneg=nneg, pos=pos, neg=neg) it = sample_oct_patch_centers(roimask, pshape=pshape, npos=npos, pos=pos, neg=neg) # TODO : check code it1 = ni.sample_patch_centers(roimask, pshape=pshape, npos=int(float(npos) * .2), nneg=0, pos=pos, neg=neg) for r, c, l in it1: it.append([r, c, l]) # h,w = roimask.shape # fig1 = plt.figure() # ax1 = fig1.add_subplot(111, aspect='equal') for r, c, label in it: img_patch = ni.extract_patch(img, pshape, r, c) mask_patch = ni.extract_patch(mask, pshape, r, c) label_IRF = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == IRF_CODE)) label_SRF = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == SRF_CODE)) label_PED = np.int8( np.any(mask_patch[patch_border:-patch_border, patch_border:-patch_border] == PED_CODE)) yield img_patch, mask_patch, label_IRF, label_SRF, label_PED