def AddNoiseMosai_random(x,I,B,Iinv,Binv): # default value channel = np.size(x,2) sigma_s=np.ones(3) sigma_c=np.ones(3) sigma_s[0] = 0.16*random.random() # original 0.16 sigma_s[1] = 0.16*random.random() sigma_s[2] = 0.16*random.random() sigma_c[0] = 0.06*random.random() # original 0.16 sigma_c[1] = 0.06*random.random() sigma_c[2] = 0.06*random.random() rand_index = np.random.permutation(201) crf_index = rand_index[0] pattern = np.random.permutation(5) temp_x = x # # x -> L temp_x = ICRF_Map(temp_x,Iinv[crf_index,:],Binv[crf_index,:]) noise_s_map = np.multiply(sigma_s[np.newaxis,np.newaxis,:], temp_x) noise_s = np.multiply(norm.ppf(np.random.rand(np.size(x,0), np.size(x,1),np.size(x,2))),noise_s_map) temp_x = temp_x + noise_s noise_c_map = np.tile(sigma_c[np.newaxis,np.newaxis,:],(np.size(x,0),np.size(x,1),1)) noise_c = np.multiply(noise_c_map, norm.ppf(np.random.rand(np.size(temp_x,0), np.size(temp_x,1),np.size(temp_x,2)))) temp_x = temp_x + noise_c noise_level_map=noise_s_map+noise_c_map # add Mosai if pattern[0] == 0: B_b,_ ,mask= mosaic_bayer(temp_x, 'gbrg', 0) elif pattern[0] == 1: B_b,_ ,_= mosaic_bayer(temp_x, 'grbg', 0) elif pattern[0] == 2: B_b,_ ,_= mosaic_bayer(temp_x, 'bggr', 0) elif pattern[0] == 3: B_b,_ ,_= mosaic_bayer(temp_x, 'rggb', 0) else: B_b = temp_x temp_x = B_b.astype(np.float32) # DeMosai if pattern[0] == 0: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='GBRG') elif pattern[0] == 1: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='GRBG') elif pattern[0] == 2: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='BGGR') elif pattern[0] == 3: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='RGGB') else: lin_rgb = temp_x temp_x=lin_rgb.astype(np.float32) # L -> x temp_x = CRF_Map(temp_x,I[crf_index,:],B[crf_index,:]) return temp_x, noise_level_map
def bay2rgb(self, method=2): # print status self.sta.status_msg('Debayering', self.cfg.params[self.cfg.opt_prnt]) self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt]) # Bayer to RGB conversion if method == 0: self._rgb_img = demosaicing_CFA_Bayer_bilinear( self._bay_img.astype(np.float32), self.cfg.lfpimg['bay']) elif method == 1: self._rgb_img = demosaicing_CFA_Bayer_Malvar2004( self._bay_img.astype(np.float32), self.cfg.lfpimg['bay']) else: self._rgb_img = demosaicing_CFA_Bayer_Menon2007( self._bay_img.astype(np.float32), self.cfg.lfpimg['bay']) # clip intensities above and below previous limits (removing dead and hot outliers yields much better contrast) self._rgb_img[ self._rgb_img < self._bay_img.min()] = self._bay_img.min() self._rgb_img[ self._rgb_img > self._bay_img.max()] = self._bay_img.max() # print "Progress: Done!" self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt]) return True
def bay2rgb(self, method=2): # print status self.sta.status_msg('Debayering', self.cfg.params[self.cfg.opt_prnt]) self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt]) # Bayer to RGB conversion if method == 0: self._rgb_img = demosaicing_CFA_Bayer_bilinear( self._bay_img, self.cfg.lfpimg['bay']) elif method == 1: self._rgb_img = demosaicing_CFA_Bayer_Malvar2004( self._bay_img, self.cfg.lfpimg['bay']) else: self._rgb_img = demosaicing_CFA_Bayer_Menon2007( self._bay_img, self.cfg.lfpimg['bay']) # normalize image min = np.percentile(self._rgb_img, 0.05) max = np.max(self.rgb_img) self._rgb_img = misc.Normalizer(self._rgb_img, min=min, max=max).type_norm() # update status message self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt]) return True
def on_message(client, userdata, msg): try: data = json.loads(msg.payload.decode('utf-8')) print('got message ' + data['_meta']['topic'] + ' on ' + msg.topic) if data['_meta']['topic'] == 'image_jpeg': if 'image_is_demosaiced' in data and data['image_is_demosaiced']: # skip color images return jpeg = Image.open(BytesIO(base64.b64decode(data['image_jpeg']))) image_arr = np.array(jpeg.getdata()).reshape( jpeg.size[0], jpeg.size[1]) / 0xff image_arr = cctf_encoding( demosaicing_CFA_Bayer_Menon2007(image_arr, 'BGGR')) image_arr = exposure.rescale_intensity(image_arr, (0, 1)) image_arr = exposure.adjust_gamma(image_arr, 1.5) im = Image.fromarray((image_arr * 255).astype(np.uint8)) buffered = BytesIO() im.save(buffered, format="JPEG") im_str = base64.b64encode(buffered.getvalue()).decode('ascii') data['image_jpeg'] = im_str data['image_is_demosaiced'] = True new_topic = args.topic.replace('#', data['_meta']['device_id']) publish.single(new_topic, json.dumps(data), hostname=args.publish_host) except Exception as e: print(e)
def get_demosaiced(img: ndarray, pattern: str = 'GRBG', method: str = 'bilinear') -> ndarray: """Get a demosaiced RGB image from a raw image. This function is a wrapper of the demosaicing functions supplied by the ``color_demosaicing`` package. Args: img: Input image, greyscale, of shape (x,y). pattern: Bayer filter pattern that the input image is modulated with. Patterns are: 'RGGB', 'BGGR', 'GRBG', 'GBRG'. Default: 'GRBG' method: Algorithm used to calculate the demosaiced image.\n * 'bilinear': Simple bilinear interpolation of color values * 'malvar2004': Algorithm introduced by Malvar et. al. [R3]_ * 'menon2007': Algorithm introduced by Menon et. al. [R4]_, Returns: Demosaiced RGB-color image of shape (x,y,3) of dtype :class:`numpy.float64`. References: .. [R3] H.S. Malvar, Li-wei He, and R. Cutler (2004). High-quality linear interpolation for demosaicing of Bayer-patterned color images. IEEE International Conference on Acoustics, Speech, and Signal Processing, Proceedings. (ICASSP '04). DOI: 10.1109/ICASSP.2004.1326587 .. [R4] D. Menon, S. Andriani, G. Calvagno (2007). Demosaicing With Directional Filtering and a posteriori Decision. IEEE Transactions on Image Processing (Volume: 16, Issue: 1) DOI: 10.1109/TIP.2006.884928 """ param_list = ["bilinear", "malvar2004", "menon2007"] # Do demosaicing with specified method if method not in param_list: raise ValueError( f"The specified method {method} is none of the supported " f"methods: {param_list}.") elif method == "bilinear": return demosaicing_CFA_Bayer_bilinear(img.astype(np.float64), pattern=pattern) elif method == "malvar2004": return demosaicing_CFA_Bayer_Malvar2004(img.astype(np.float64), pattern=pattern) elif method == "menon2007": return demosaicing_CFA_Bayer_Menon2007(img.astype(np.float64), pattern=pattern)
def debayer_image_array(data, algorithm='bilinear', pattern='GRBG'): """ Returns the RGB data after bilinear interpolation on the given array. ---------- parameters ---------- data : The input array containing the image data. Array like of shape (rows,columns) algorithm : The algorithm to use for the debayering operation. {'bilinear','malvar2004','menon2007'} ---------- returns ---------- numpy array of shape (rows,columns,3) """ # Check to see if data is two dimensional try: assert len(data.shape) == 2, 'Shape is not 2 dimensional' except AssertionError: log_error_exit('Image data input to debayer is not 2 dimensional') if algorithm == 'bilinear': rgb_data = demosaicing_CFA_Bayer_bilinear(data, pattern) elif algorithm == 'malvar2004': rgb_data = demosaicing_CFA_Bayer_Malvar2004(data, pattern) elif algorithm == 'menon2007': rgb_data = demosaicing_CFA_Bayer_Menon2007(data, pattern) return rgb_data.astype(np.uint16)
def AddNoiseMosai(x,I,B,Iinv,Binv,sigma_s,sigma_c,crf_index, pattern): temp_x = x # x -> L temp_x = ICRF_Map(temp_x,Iinv[crf_index,:],Binv[crf_index,:]) noise_s_map = np.multiply(sigma_s[np.newaxis,np.newaxis,:], temp_x) noise_s = np.multiply(norm.ppf(np.random.rand(np.size(x,0), np.size(x,1),np.size(x,2))),noise_s_map) temp_x = temp_x + noise_s noise_c_map = np.tile(sigma_c[np.newaxis,np.newaxis,:],(np.size(x,0),np.size(x,1),1)) noise_c = np.multiply(noise_c_map, norm.ppf(np.random.rand(np.size(temp_x,0), np.size(temp_x,1),np.size(temp_x,2)))) temp_x = temp_x + noise_c noise_level_map=noise_s_map+noise_c_map # add Mosai if pattern == 0: B_b,_ ,mask= mosaic_bayer(temp_x, 'gbrg', 0) elif pattern == 1: B_b,_ ,_= mosaic_bayer(temp_x, 'grbg', 0) elif pattern == 2: B_b,_ ,_= mosaic_bayer(temp_x, 'bggr', 0) elif pattern == 3: B_b,_ ,_= mosaic_bayer(temp_x, 'rggb', 0) else: B_b = temp_x temp_x = B_b.astype(np.float32) # DeMosai if pattern == 0: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='GBRG') elif pattern == 1: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='GRBG') elif pattern == 2: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='BGGR') elif pattern == 3: lin_rgb=colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(temp_x, pattern='RGGB') else: lin_rgb = temp_x temp_x=lin_rgb.astype(np.float32) # L -> x temp_x = CRF_Map(temp_x,I[crf_index,:],B[crf_index,:]) return temp_x, noise_level_map
def demosaicking(image: np.ndarray, method: str = "bilinear", pattern: str = "RGGB") -> np.ndarray: """Returns the demosaicked image given a method. Parameters ------------------- image: np.ndarray, The image to be demosaicked. method: str, Demosaicking method to be applied. pattern: str, Arrangement of the colour filters on the pixel array. Possible patterns are: {RGGB, BGGR, GRBG, GBRG}. Raises ------------------ ValueError, If given method does not exist. Returns ------------------- Returns the demosaicked image. """ image_rgb = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) image_cfa = mosaicing_CFA_Bayer(image_rgb, pattern=pattern) / 255 if method == 'bilinear': image_demo = ((cctf_encoding( demosaicing_CFA_Bayer_bilinear(image_cfa, pattern=pattern))) * 255).astype(np.uint8) elif method == 'malvar': image_demo = ((cctf_encoding( demosaicing_CFA_Bayer_Malvar2004(image_cfa, pattern=pattern))) * 255).astype(np.uint8) elif method == 'menon': image_demo = ((cctf_encoding( demosaicing_CFA_Bayer_Menon2007(image_cfa, pattern=pattern))) * 255).astype(np.uint8) else: raise ValueError( 'Given method \'{}\' does not belong to possible methods. ' 'Valid methods are: \'bilinear\', \'malvar\' and \'menon\'.'. format(method)) return cv2.cvtColor(image_demo, cv2.COLOR_RGB2GRAY)
def demosaic_NEF(imgpath, white_level=1023, black_level=0, awb=[1, 1, 1], gamma=2.2): # read image raw = rawpy.imread(imgpath) bayer_raw = raw.raw_image # normalization bayer_raw = (bayer_raw.astype(np.float64) - black_level) / (white_level - black_level) # awb bayer_raw[0::2, 0::2] *= awb[0] bayer_raw[1::2, 1::2] *= awb[2] bayer_raw = (np.clip(bayer_raw, 0, 1) * 255.0).astype(np.uint8) # demosaic demosaicked_rgb = colour_demosaicing.demosaicing_CFA_Bayer_Menon2007( bayer_raw, 'BGGR') # gamma correction demosaicked_rgb = adjust_gamma(demosaicked_rgb, gamma) demosaicked_rgb = (demosaicked_rgb * 255.0).astype(np.uint8) return demosaicked_rgb
def demosaic(cfaimg): return demosaicing_CFA_Bayer_Menon2007(cfaimg, pattern='BGGR')
def process(filename, use_srgb=True, use_gamma=True, brightness='percentile', demosaicing='menon'): """ A simple imaging pipeline implemented from scratch. :param filename: input RAW image :param use_srgb: set to False to disable camera RGB to sRGB conversion :param use_gamma: set to False to disable gamma correction :param brightness: global brightness correction method (percentile, shift or None) :param demosaicing: demosaicing method (menon, bilinear) """ # Sanity checks if brightness not in ['percentile', 'shift', None]: raise ValueError('Unsupported brightness correction mode!') if demosaicing not in ['menon', 'bilinear']: raise ValueError('Unsupported demosaicing method!') with Raw(filename) as raw: raw.unpack() log.debug('Model : {} {}'.format(raw.metadata.make.decode(), raw.metadata.model.decode())) log.debug('CFA : {}'.format(raw.color_description.decode())) image_raw = np.array(raw.raw_image(), dtype=np.float32) # Normalization and calibration black = raw.data.contents.color.black saturation = raw.data.contents.color.maximum image_raw = image_raw.astype(np.float32) image_raw -= black uint14_max = 1 image_raw *= uint14_max / (saturation - black) image_raw = np.clip(image_raw, 0, uint14_max) # White balancing cam_mul = np.array(raw.data.contents.color.cam_mul, dtype=np.float32) cam_mul /= cam_mul[1] # Set the multiplier for G to be 1 cfa_pattern = ''.join([''.join(x) for x in raw.color_filter_array]) if cfa_pattern == 'GBRG': image_raw[1::2, 0::2] *= cam_mul[0] image_raw[0::2, 1::2] *= cam_mul[2] elif cfa_pattern == 'RGGB': image_raw[0::2, 0::2] *= cam_mul[0] image_raw[1::2, 1::2] *= cam_mul[2] elif cfa_pattern == 'BGGR': image_raw[1::2, 1::2] *= cam_mul[0] image_raw[0::2, 0::2] *= cam_mul[2] image_raw = image_raw.clip(0, uint14_max) # Demosaicing if demosaicing == 'menon': image_rgb = colour_demosaicing.demosaicing_CFA_Bayer_Menon2007(image_raw, pattern=cfa_pattern) elif demosaicing == 'bilinear': image_rgb = colour_demosaicing.demosaicing_CFA_Bayer_bilinear(image_raw, pattern=cfa_pattern) # Color space conversion if use_srgb: cam2srgb = np.array(raw.data.contents.color.rgb_cam, dtype=np.float).reshape((3,4))[:, 0:3] shape = image_rgb.shape pixels = image_rgb.reshape(-1, 3).T pixels = cam2srgb.dot(pixels) image_rgb = pixels.T.reshape(shape) image_rgb = image_rgb.clip(0, uint14_max) # Deallocate del pixels # Brightness correction if brightness == 'percentile': percentile = 0.5 image_rgb -= np.percentile(image_rgb, percentile) image_rgb /= np.percentile(image_rgb, 100 - percentile) elif brightness == 'shift': mult = 0.25 / np.mean(image_rgb) image_rgb *= mult image_rgb = image_rgb.clip(0, 1) # Gamma correction if use_gamma: image_rgb = np.power(image_rgb, 1/2.2) # Clip invisible pixels image_rgb = image_rgb[0:raw.metadata.height, 0:raw.metadata.width, :] # Clip & rotate canvas, if needed if raw.metadata.orientation == 5: image_rgb = np.rot90(image_rgb) elif raw.metadata.orientation == 6: image_rgb = np.rot90(image_rgb, 3) return image_rgb
from matplotlib import pyplot as plt from colour.plotting import * from colour_demosaicing import ( EXAMPLES_RESOURCES_DIRECTORY, demosaicing_CFA_Bayer_bilinear, demosaicing_CFA_Bayer_Malvar2004, demosaicing_CFA_Bayer_Menon2007, mosaicing_CFA_Bayer) cctf_encoding = colour.cctf_encoding image = np.fromfile('image.raw', 'uint8')[:320*320]/0xff print(image[0]) print(np.average(image)) print(image.shape) print(image.shape[0]/320) lines = int(image.shape[0]/320) image = np.reshape(image, (lines, 320)) print(image.shape) im = Image.open("image.jpeg") image_jpeg = np.array(im.getdata()).reshape(im.size[0], im.size[1])/0xff encoded = cctf_encoding(image) encoded_jpeg = cctf_encoding(image_jpeg) plot_image(encoded) plot_image(encoded_jpeg) plot_image(cctf_encoding(demosaicing_CFA_Bayer_Menon2007(image, 'BGGR'))) plot_image(cctf_encoding(demosaicing_CFA_Bayer_Menon2007(image_jpeg, 'BGGR')))
# eq = cv2.equalizeHist(pnm) # # convert to 3 channel # cv2.imwrite('root/wangxin/eq.jpg',demosaicing_CFA_Bayer_Menon2007(eq)) names = [] save_folder = '/root/wangxin/test/' with open('test_dataset.csv', 'r') as f: r = f.readlines() for line in r: line = line.split(',')[0] path = line.split('/') folder = path[0].split('_') folder = '_'.join(folder[:-1]) + '/' + folder[-1] name = folder + '/' + path[-1].split('.')[0] + '.pnm' names.append(name) for index, name in enumerate(names): pnm_file = name pnm = cv2.imread('/dataset/training/' + pnm_file, 0) eq = cv2.equalizeHist(pnm) # convert to 3 channel #check folder if not os.path.exists(save_folder + '/'.join(name.split('/')[:-1])): os.mkdir('/'.join(name.split('/')[:-2])) os.mkdir('/'.join(name.split('/')[:-1])) #print(eq.shape) print( cv2.imwrite(save_folder + '.'.join(name.split('.')[:-1]) + '.jpg', demosaicing_CFA_Bayer_Menon2007(eq))) print(index / len(names))
#print(names) num = 1 t1 = time.time() for epoch in tqdm(range(int(len(names) / num))): pnms = np.zeros([2048, 2448 * num]) for index, name in enumerate(names[epoch * num:(epoch + 1) * num]): pnm_file = name pnm = cv2.imread('/dataset/training/' + pnm_file, 0) eq = cv2.equalizeHist(pnm) #print(pnm.shape) pnms[:, index * 2448:(index + 1) * 2448] = eq #print('read over') #print('eq over') pnms_3 = demosaicing_CFA_Bayer_Menon2007(pnms) #print('demosaicing_CFA_Bayer_Menon2007 over') #print('write') for index, name in enumerate(names[epoch * num:(epoch + 1) * num]): #print(pnms_3[:,index*2448:index*2448+2448,:].shape) image = pnms_3[:, index * 2448:index * 2448 + 2448, :] #print(image.shape) if not os.path.exists(save_folder + '/'.join(name.split('/')[:-1])): os.mkdir(save_folder + '/'.join(name.split('/')[:-2])) os.mkdir(save_folder + '/'.join(name.split('/')[:-1])) cv2.imwrite(save_folder + '.'.join(name.split('.')[:-1]) + '.jpg', image) #print(index/num) pnms = np.zeros([2048, 2448 * num]) for index, name in enumerate(names[(epoch + 1) * num:]): pnm_file = name
def pnm_read(name): name = name[:-3] + 'pnm' pnm = cv2.imread(name, 0) eq = cv2.equalizeHist(pnm) return demosaicing_CFA_Bayer_Menon2007(eq)