def rgb2all(img, freq_type): """Converts the RGB to a grey image and applies frequency decomposition. :param img: input image in RGB colour space. :param freq_type: type of a wavelength wavelet or fourier decomposition. :return: decomposed image with a size according to the decomposition type. """ img = img.copy() img = normalisations.rgb2double(img) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) return gry2all(img, freq_type)
def rgb2opponency(image_rgb, opponent_space='lab'): image_rgb = normalisations.rgb2double(image_rgb) if opponent_space is None: # it's already in opponency image_opponent = image_rgb elif opponent_space == 'lab': image_opponent = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2LAB) elif opponent_space == 'dkl': image_opponent = rgb012dkl(image_rgb) else: sys.exit('Not supported colour space %s' % opponent_space) return image_opponent
def gry2all(img, freq_type): """Converts applies frequency decomposition to a grey image. :param img: input grey-scale image. :param freq_type: type of a wavelength wavelet or fourier decomposition. :return: decomposed image with a size according to the decomposition type. """ img = img.copy() img = normalisations.rgb2double(img) if freq_type in SUPPORTED_WAVELETS: ll, (lh, hl, hh) = pywt.dwt2(img, freq_type) # NOTE: we convert them to a range of 0 to 1 img = np.zeros((*ll.shape, 4)) img[:, :, 0] = ll img[:, :, 1] = (lh + 1) img[:, :, 2] = (hl + 1) img[:, :, 3] = (hh + 1) img /= 2 else: sys.exit('frequency_domain.gry2all does not support %s.' % freq_type) return img
def rgb2all(img, dest_space): """Converting an RGB to image to the specified destination colour space. :param img: the input image in RGB colour space, :param dest_space: the destination colour space. :return: an img of the same spatial size with three dimensions. """ img = img.copy() img = normalisations.rgb2double(img) if 'rgb' in dest_space: if dest_space == 'rgb-r': img = img[:, :, 0] elif dest_space == 'rgb-g': img = img[:, :, 1] elif dest_space == 'rgb-b': img = img[:, :, 2] elif dest_space == 'lab': # TODO: this is really not nice to go back and forth img = (img * 255).astype('uint8') img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB) img = img.astype('float') / 255 elif dest_space == 'dkl': img = rgb2dkl01(img) elif dest_space == 'hsv': img = rgb2hsv01(img) elif dest_space == 'lms': img = rgb2lms01(img) elif dest_space == 'yog': img = rgb2yog01(img) elif dest_space == 'gry': img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) else: sys.exit('colour_spaces.rgb2all does not support %s.' % dest_space) # all matrices are of three dimensions if len(img.shape) == 2: img = np.repeat(img[:, :, np.newaxis], 1, axis=2) return img
def rgb2lms(x): return rgb012lms(normalisations.rgb2double(x))
def rgb2hsv01(x): x = x.copy() x = normalisations.rgb2double(x) x = np.float32(cv2.cvtColor(x.astype('float32'), cv2.COLOR_RGB2HSV)) x[:, :, 0] /= 360 return x
def rgb2yog(x): return rgb012yog(normalisations.rgb2double(x))
def rgb2dkl(x): return rgb012dkl(normalisations.rgb2double(x))
def rgb2xyz(x): return rgb012xyz(normalisations.rgb2double(x))
def report_vector_influence(img_org, img_full, img_lesion): img_org = normalisations.rgb2double(img_org) img_org_op = cv2.cvtColor(img_org, cv2.COLOR_RGB2LAB) img_org_hsv = cv2.cvtColor(img_org, cv2.COLOR_RGB2HSV) img_full = normalisations.rgb2double(img_full) img_full_op = cv2.cvtColor(img_full, cv2.COLOR_RGB2LAB) img_full_hsv = cv2.cvtColor(img_full, cv2.COLOR_RGB2HSV) img_lesion = normalisations.rgb2double(img_lesion) img_lesion_op = cv2.cvtColor(img_lesion, cv2.COLOR_RGB2LAB) img_lesion_hsv = cv2.cvtColor(img_lesion, cv2.COLOR_RGB2HSV) img_diff = img_full - img_lesion img_diff_op = img_full_op - img_lesion_op img_diff_hsv = img_full_hsv - img_lesion_hsv img_diff_hsv[:, :, 0] = angular_diff(img_full_hsv[:, :, 0], img_lesion_hsv[:, :, 0]) white_pixs = img_org_op[:, :, 0] > 75 black_pixs = img_org_op[:, :, 0] < 25 red_pixs = img_org_op[:, :, 1] > 0 green_pixs = img_org_op[:, :, 1] < 0 yellow_pixs = img_org_op[:, :, 2] > 0 blue_pixs = img_org_op[:, :, 2] < 0 satuated_pixs = img_org_hsv[:, :, 1] > 0.75 achromatic_pixs = img_org_hsv[:, :, 1] < 0.25 report = dict() report['num_pixels'] = { 'white_pixs': white_pixs.sum(), 'black_pixs': black_pixs.sum(), 'red_pixs': red_pixs.sum(), 'green_pixs': green_pixs.sum(), 'yellow_pixs': yellow_pixs.sum(), 'blue_pixs': blue_pixs.sum(), 'satuated_pixs': satuated_pixs.sum(), 'achromatic_pixs': achromatic_pixs.sum() } report['diff_rgb'] = img_diff.mean(axis=(0, 1)) report['hist_rgb_full'] = [ hist(img_full[:, :, 0], 10, [0, 1]), hist(img_full[:, :, 1], 10, [0, 1]), hist(img_full[:, :, 2], 10, [0, 1]) ] report['hist_rgb_lesion'] = [ hist(img_lesion[:, :, 0], 10, [0, 1]), hist(img_lesion[:, :, 1], 10, [0, 1]), hist(img_lesion[:, :, 2], 10, [0, 1]) ] ab_rng = [ -127, -40, -30, -20, -10, 0, 10, 20, 30, 40, 128 ] report['diff_op'] = img_diff_op.mean(axis=(0, 1)) report['hist_op_full'] = [ hist(img_full_op[:, :, 0], 10, [0, 100]), hist(img_full_op[:, :, 1], ab_rng, [-127, 128]), hist(img_full_op[:, :, 2], ab_rng, [-127, 128]) ] report['hist_op_lesion'] = [ hist(img_lesion_op[:, :, 0], 10, [0, 100]), hist(img_lesion_op[:, :, 1], ab_rng, [-127, 128]), hist(img_lesion_op[:, :, 2], ab_rng, [-127, 128]) ] report['white_pixs_op'] = img_diff_op[white_pixs].mean(axis=(0)) report['black_pixs_op'] = img_diff_op[black_pixs].mean(axis=(0)) report['red_pixs_op'] = img_diff_op[red_pixs].mean(axis=(0)) report['green_pixs_op'] = img_diff_op[green_pixs].mean(axis=(0)) report['yellow_pixs_op'] = img_diff_op[yellow_pixs].mean(axis=(0)) report['blue_pixs_op'] = img_diff_op[blue_pixs].mean(axis=(0)) report['saturated_pixs_op'] = img_diff_op[satuated_pixs].mean(axis=(0)) report['achromatic_pixs_op'] = img_diff_op[achromatic_pixs].mean(axis=(0)) report['diff_hsv'] = img_diff_hsv.mean(axis=(0, 1)) report['hist_hsv_full'] = [ hist(img_full_hsv[:, :, 0], 12, [0, 360]), hist(img_full_hsv[:, :, 1], 10, [0, 1]), hist(img_full_hsv[:, :, 2], 10, [0, 1]) ] report['hist_hsv_lesion'] = [ hist(img_lesion_hsv[:, :, 0], 12, [0, 360]), hist(img_lesion_hsv[:, :, 1], 10, [0, 1]), hist(img_lesion_hsv[:, :, 2], 10, [0, 1]) ] report['white_pixs_hsv'] = img_diff_hsv[white_pixs].mean(axis=(0)) report['black_pixs_hsv'] = img_diff_hsv[black_pixs].mean(axis=(0)) report['red_pixs_hsv'] = img_diff_hsv[red_pixs].mean(axis=(0)) report['green_pixs_hsv'] = img_diff_hsv[green_pixs].mean(axis=(0)) report['yellow_pixs_hsv'] = img_diff_hsv[yellow_pixs].mean(axis=(0)) report['blue_pixs_hsv'] = img_diff_hsv[blue_pixs].mean(axis=(0)) report['saturated_pixs_hsv'] = img_diff_hsv[satuated_pixs].mean(axis=(0)) report['achromatic_pixs_hsv'] = img_diff_hsv[achromatic_pixs].mean(axis=(0)) return report