def runMSRCP(imgPath): with open('config.json', 'r') as f: config = json.load(f) img = cv2.imread(imgPath, 1) img_msrcp = retinex.MSRCP(img, config['sigma_list'], config['low_clip'], config['high_clip']) m3F.imshow(img_msrcp, "MSRCP") return img_msrcp
def retinex_shadow_removal(image, retinex_type): with open('config.json', 'r') as f: config = json.load(f) if retinex_type == 'automatedMSRCR': return retinex.automatedMSRCR(image, config['sigma_list']) elif retinex_type == 'MSRCRP': return retinex.MSRCP(image, config['sigma_list'], config['low_clip'], config['high_clip']) elif retinex_type == 'MSRCR': return retinex.MSRCR(image, config['sigma_list'], config['G'], config['b'], config['alpha'], config['beta'], config['low_clip'], config['high_clip']) else: return image
def runMSRCP(imgPath): with open('config.json', 'r') as f: config = json.load(f) img = cv2.imread(imgPath, 1) shape = img.shape img_msrcp = retinex.MSRCP(img, config['sigma_list'], config['low_clip'], config['high_clip']) img_msrcp = cv2.cvtColor(img_msrcp, cv2.COLOR_RGB2BGR) plt.title("MSRCP") plt.imshow(img_msrcp) plt.show() return img_msrcp
with open('config.json', 'r') as f: config = json.load(f) # 遍历读入的图像 for img_name in img_list: if img_name == '.gitkeep': continue # 读入图像 img = cv2.imread(os.path.join(data_path, img_name)) img_histogram = coloredHistoEqual(img) img_gamma = adjust_gamma(img) # 多尺度Retinex带色彩恢复 img_msrcr = retinex.MSRCR(img, config['sigma_list'], config['G'], config['b'], config['alpha'], config['beta'], config['low_clip'], config['high_clip']) img_amsrcr = retinex.automatedMSRCR(img, config['sigma_list']) # MSR with chromaticity preservation img_msrcp = retinex.MSRCP(img, config['sigma_list'], config['low_clip'], config['high_clip']) # 实验结果 shape = img.shape cv2.imshow('Image', img) cv2.imshow('retinex', img_msrcr) cv2.imshow('Histogram equalization', img_histogram) cv2.imshow('Gamma Correction', img_gamma) cv2.imshow('Automated retinex', img_amsrcr) cv2.imshow('MSRCP', img_msrcp) cv2.waitKey()
def LOMO(img, config): ''' Extract LOMO features from image Input: img: numpy array of shape (height, width, channels): image to extract features from config: dict: dictionary of configuration parameters Output: lomo: numpy array: LOMO feature vector (length depending on image size and configuration parameters) ''' # load relevant configuration parameters from config dictionary sigma_list = config['retinex']['sigma_list'] G = config['retinex']['G'] b = config['retinex']['b'] alpha = config['retinex']['alpha'] beta = config['retinex']['beta'] low_clip = config['retinex']['low_clip'] high_clip = config['retinex']['high_clip'] R_list = config['lomo']['R_list'] tau = config['lomo']['tau'] hsv_bin_size = config['lomo']['hsv_bin_size'] block_size = config['lomo']['block_size'] block_step = config['lomo']['block_step'] # perform retinex transformation on image img_retinex = retinex.MSRCP(img, sigma_list, low_clip, high_clip) # initialize SILTP feature vector and HSV color feature vector siltp_feat = np.array([]) hsv_feat = np.array([]) # process original image and two downsampled versions by pooling for pool in range(3): # determine the number of subwindows to process row- and column-wise row_num = (img.shape[0] - (block_size - block_step)) / block_step col_num = (img.shape[1] - (block_size - block_step)) / block_step # iterate over all subwindows for row in range(int(row_num)): for col in range(int(col_num)): # extract subwindow from image img_block = img[row * block_step:row * block_step + block_size, col * block_step:col * block_step + block_size] # initialize array of multiple SILTP histograms siltp_hist = np.array([]) # multiple SILTP histograms are computed for different radii given by configuration for R in R_list: # compute SILTP features for every pixel in the subwindow siltp4 = siltp.SILTP4(img_block, R, tau) # get bins and frequency for each bin unique, count = np.unique(siltp4, return_counts=True) # initialize histogram with zeros siltp_hist_r = np.zeros([3**4]) # fill histogram for u, c in zip(unique, count): siltp_hist_r[u] = c # concatenate SILTP for all radii siltp_hist = np.concatenate([siltp_hist, siltp_hist_r], 0) # extract subwindow from retinex image img_block = img_retinex[row * block_step:row * block_step + block_size, col * block_step:col * block_step + block_size] # transform retinex image to HSV color space for color feature extraction img_hsv = cv2.cvtColor(img_block, cv2.COLOR_BGR2HSV) # build color histogram based on HSV image hsv_hist = channel_histogram.jointHistogram( img_hsv, [0, 255], hsv_bin_size) # compute maximal occurrence over all subwindows of the same row if col == 0: siltp_feat_col = siltp_hist hsv_feat_col = hsv_hist else: # maximal occurrence for SILTP histogram siltp_feat_col = np.maximum(siltp_feat_col, siltp_hist) # maximal occurrence for HSV histogram hsv_feat_col = np.maximum(hsv_feat_col, hsv_hist) # concatenate the histograms over all rows siltp_feat = np.concatenate([siltp_feat, siltp_feat_col], 0) hsv_feat = np.concatenate([hsv_feat, hsv_feat_col], 0) # apply pooling and perform feature extraction with downsampled image img = averagePooling(img) img_retinex = averagePooling(img_retinex) # log transform siltp_feat = np.log(siltp_feat + 1.0) # normalize to unit length siltp_feat[:siltp_feat.shape[0] // 2] /= np.linalg.norm( siltp_feat[:siltp_feat.shape[0] // 2]) siltp_feat[siltp_feat.shape[0] // 2:] /= np.linalg.norm( siltp_feat[siltp_feat.shape[0] // 2:]) # log transform hsv_feat = np.log(hsv_feat + 1.0) # normalize to unit length hsv_feat /= np.linalg.norm(hsv_feat) # concatenate all features to final LOMO descriptor lomo = np.concatenate([siltp_feat, hsv_feat], 0) return lomo
def LOMO(img, config): sigma_list = config['retinex']['sigma_list'] G = config['retinex']['G'] b = config['retinex']['b'] alpha = config['retinex']['alpha'] beta = config['retinex']['beta'] low_clip = config['retinex']['low_clip'] high_clip = config['retinex']['high_clip'] R_list = config['lomo']['R_list'] tau = config['lomo']['tau'] hsv_bin_size = config['lomo']['hsv_bin_size'] block_size_col = config['lomo']['block_size_col'] block_step_col = config['lomo']['block_step_col'] block_size_row = config['lomo']['block_size_row'] block_step_row = config['lomo']['block_step_row'] img_retinex = retinex.MSRCP(img, sigma_list, low_clip, high_clip) siltp_feat = np.array([]) hsv_feat = np.array([]) for pool in range(3): row_num = int((img.shape[0] - (block_size_row - block_step_row)) / block_step_row) col_num = int((img.shape[1] - (block_size_col - block_step_col)) / block_step_col) for row in range(row_num): for col in range(col_num): img_block = img[row * block_step_row:row * block_step_row + block_size_row, col * block_step_col:col * block_step_col + block_size_col] siltp_hist = np.array([]) for R in R_list: siltp4 = siltp.SILTP4(img_block.astype(np.float32), R, tau) unique, count = np.unique(siltp4, return_counts=True) siltp_hist_r = np.zeros([3**4]) for u, c in zip(unique, count): siltp_hist_r[u] = c siltp_hist = np.concatenate([siltp_hist, siltp_hist_r], 0) img_block = img_retinex[row * block_step_row:row * block_step_row + block_size_row, col * block_step_col:col * block_step_col + block_size_col] img_hsv = cv2.cvtColor(img_block.astype(np.float32), cv2.COLOR_BGR2HSV) hsv_hist = channel_histogram.jointHistogram( img_hsv, [0, 255], hsv_bin_size) if col == 0: siltp_feat_col = siltp_hist hsv_feat_col = hsv_hist else: siltp_feat_col = np.maximum(siltp_feat_col, siltp_hist) hsv_feat_col = np.maximum(hsv_feat_col, hsv_hist) siltp_feat = np.concatenate([siltp_feat, siltp_feat_col], 0) hsv_feat = np.concatenate([hsv_feat, hsv_feat_col], 0) img = averagePooling(img) img_retinex = averagePooling(img_retinex) siltp_feat = np.log(siltp_feat + 1.0) if int(siltp_feat.shape[0]) % 2 == 1: print("ATTENTION") siltp_feat[:int(siltp_feat.shape[0] / 2)] /= np.linalg.norm( siltp_feat[:int(siltp_feat.shape[0] / 2)]) siltp_feat[int(siltp_feat.shape[0] / 2):] /= np.linalg.norm( siltp_feat[int(siltp_feat.shape[0] / 2):]) hsv_feat = np.log(hsv_feat + 1.0) hsv_feat /= np.linalg.norm(hsv_feat) lomo = np.concatenate([siltp_feat, hsv_feat], 0) return lomo