def compute(self, img):
averages = np.zeros((self.rows,self.cols,3))
imgH, imgW, _ = img.shape
for row in range(self.rows):
for col in range(self.cols):
slice = img[imgH/self.rows * row: imgH/self.rows * (row+1), imgW/self.cols*col : imgW/self.cols*(col+1)]
average_color_per_row = np.mean(slice, axis=0)
average_color = np.mean(average_color_per_row, axis=0)
average_color = np.uint8(average_color)
averages[row][col][0] = average_color[0]
averages[row][col][1] = average_color[1]
averages[row][col][2] = average_color[2]
icon = cv2.cvtColor(np.array(averages, dtype=np.uint8), cv2.COLOR_BGR2YCR_CB)
y, cr, cb = cv2.split(icon)
dct_y = cv2.dct(np.float32(y))
dct_cb = cv2.dct(np.float32(cb))
dct_cr = cv2.dct(np.float32(cr))
dct_y_zigzag = []
dct_cb_zigzag = []
dct_cr_zigzag = []
flip = True
flipped_dct_y = np.fliplr(dct_y)
flipped_dct_cb = np.fliplr(dct_cb)
flipped_dct_cr = np.fliplr(dct_cr)
for i in range(self.rows + self.cols -1):
k_diag = self.rows - 1 - i
diag_y = np.diag(flipped_dct_y, k=k_diag)
diag_cb = np.diag(flipped_dct_cb, k=k_diag)
diag_cr = np.diag(flipped_dct_cr, k=k_diag)
if flip:
diag_y = diag_y[::-1]
diag_cb = diag_cb[::-1]
diag_cr = diag_cr[::-1]
dct_y_zigzag.append(diag_y)
dct_cb_zigzag.append(diag_cb)
dct_cr_zigzag.append(diag_cr)
flip = not flip
return np.concatenate([np.concatenate(dct_y_zigzag), np.concatenate(dct_cb_zigzag), np.concatenate(dct_cr_zigzag)])
python类COLOR_BGR2YCR_CB的实例源码
def test_lapsrn():
img=cv2.imread(("E:\\DevProj\\Datasets\\SuperResolution\\SR_testing_datasets"
"\\Set14\\GT\\zebra.png"),cv2.IMREAD_COLOR)
nh,nw,nc=img.shape
# imghr=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
img_lr, img_pryd=img_preprocess(img,2)
one_batch=LapSRNDataBatch(img_lr,img_pryd)
net, arg_params, aux_params = mx.model.load_checkpoint("checkpoint\\lapsrn", 100)
mod = mx.mod.Module(symbol=net, context=mx.gpu())
provide_data=[('imglr', img_lr.shape)]
provide_label=[]
for s in range(2):
provide_label.append(("loss_s{}_imggt".format(s),img_pryd[s].shape))
mod.bind(for_training=False,
data_shapes=provide_data,
label_shapes=provide_label)
mod.set_params(arg_params, aux_params,allow_missing=True)
mod.forward(one_batch)
img_sr=mod.get_outputs()
# img_sr=img_recover(img_sr)
img_lr=img_recover(img_lr)
img_hr=img_recover(img_pryd[-1])
cv2.imwrite("results\\lapsrn_imglr.bmp",img_lr)
cv2.imwrite("results\\lapsrn_imghr.bmp",img_hr)
for s in range(2):
img_temp=img_recover(img_sr[s].asnumpy())
cv2.imwrite("results\\lapsrn_imgsr{}.bmp".format(s),img_temp)
def next(self):
nrow=0
ncol=0
crop_size=self._crop_size
while (nrow<crop_size or ncol<crop_size) \
and self.cur_batch < self.batch_num:
img_path=os.path.join(self._datadir, self._img_list[self.cur_batch])
img=cv2.imread(img_path, cv2.IMREAD_COLOR)
# img=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
nrow,ncol=img.shape[0:2]
self.cur_batch+=1
if self.cur_batch < self.batch_num:
# img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_ds=cv2.resize(img,(ncol/self._scale_factor, nrow/self._scale_factor),
interpolation=cv2.INTER_CUBIC)
img_lr=cv2.resize(img_ds,(ncol, nrow),interpolation=cv2.INTER_CUBIC)
img=img.astype(npy.float32)
img_lr=img_lr.astype(npy.float32)
sub_img_lr=npy.zeros(self._provide_data[0][1],dtype=npy.float32)
sub_img_hr=npy.zeros(self._provide_label[0][1],dtype=npy.float32)
for i in range(self._crop_num):
nrow_start=npy.random.randint(0,nrow-crop_size)
ncol_start=npy.random.randint(0,ncol-crop_size)
img_crop=img_lr[nrow_start:nrow_start+crop_size,
ncol_start:ncol_start+crop_size,:]
img_crop=(img_crop-128) /128.0
img_crop = npy.swapaxes(img_crop, 0, 2)
img_crop = npy.swapaxes(img_crop, 1, 2)
sub_img_lr[i,:,:,:]=img_crop
img_crop=img[nrow_start:nrow_start+crop_size,
ncol_start:ncol_start+crop_size,:]
img_crop=(img_crop-128) /128.0
img_crop = npy.swapaxes(img_crop, 0, 2)
img_crop = npy.swapaxes(img_crop, 1, 2)
sub_img_hr[i,:,:,:]=img_crop
return SRDataBatch(sub_img_lr,sub_img_hr,0)
else:
raise StopIteration
def next(self):
nrow=0
ncol=0
crop_size=self._crop_size
while (nrow<crop_size or ncol<crop_size) \
and self.cur_batch < self.batch_num:
img_path=os.path.join(self._datadir, self._img_list[self.cur_batch])
img=cv2.imread(img_path, cv2.IMREAD_COLOR)
# img=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
nrow,ncol=img.shape[0:2]
self.cur_batch+=1
if self.cur_batch < self.batch_num:
sub_img_lr=npy.zeros(self._provide_data[0][1],dtype=npy.float32)
sub_img_pryd=[]
for item in self._provide_label:
sub_img_pryd.append(npy.zeros(item[1],dtype=npy.float32))
for i in range(self._crop_num):
nrow_start=npy.random.randint(0,nrow-crop_size)
ncol_start=npy.random.randint(0,ncol-crop_size)
img_crop=img[nrow_start:nrow_start+crop_size,
ncol_start:ncol_start+crop_size,:]
imggt_size=crop_size
for s in range(self._num_scales):
img_temp=img_crop.astype(npy.float32)
img_temp=(img_temp-128)/128.0
img_temp = npy.swapaxes(img_temp, 0, 2)
img_temp = npy.swapaxes(img_temp, 1, 2)
sub_img_pryd[self._num_scales-s-1][i,:,:,:]=img_temp
imggt_size=imggt_size/2
img_crop=cv2.resize(img_crop,(imggt_size, imggt_size),
interpolation=cv2.INTER_CUBIC)
img_temp=img_crop.astype(npy.float32)
img_temp=(img_temp-128)/128.0
img_temp = npy.swapaxes(img_temp, 0, 2)
img_temp = npy.swapaxes(img_temp, 1, 2)
sub_img_lr[i,:,:,:]=img_temp
return LapSRNDataBatch(sub_img_lr,sub_img_pryd,0)
else:
raise StopIteration
def hisEqulColor(img):
ycrcb=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
channels=cv2.split(ycrcb)
# create a CLAHE object
clahe = cv2.createCLAHE()
channels[0] = clahe.apply(channels[0])
cv2.merge(channels,ycrcb)
cv2.cvtColor(ycrcb,cv2.COLOR_YCR_CB2BGR,img)