def m3(): #加载字典 mat = scipy.io.loadmat('./Dictionary/dictionary.mat') Dl_dict = np.asarray(mat['Dl'], dtype='float64') Dh_dict = np.asarray(mat['Dh'], dtype='float64') #提取出L通道进行计算其他通道使用Bicubic进行计算 src = data.lena() src_yCrCb = colormanage.rgb2ycbcr(src) src_Ycbcr_img_Y = bicubic_2d.bicubic2d(src_yCrCb[:, :, 0], 1 / 3.0) src_Ycbcr_img_Cb = bicubic_2d.bicubic2d(src_yCrCb[:, :, 1], 1 / 3.0) src_Ycbcr_img_Cr = bicubic_2d.bicubic2d(src_yCrCb[:, :, 2], 1 / 3.0) src_Ycbcr_img_Y = src_Ycbcr_img_Y[:src_Ycbcr_img_Y.shape[0] - src_Ycbcr_img_Y.shape[0] % 3, :src_Ycbcr_img_Y.shape[1] - src_Ycbcr_img_Y.shape[0] % 3] src_Ycbcr_img_Cb = src_Ycbcr_img_Cb[:src_Ycbcr_img_Cb.shape[0] - src_Ycbcr_img_Cb.shape[0] % 3, :src_Ycbcr_img_Cb.shape[1] - src_Ycbcr_img_Cb.shape[0] % 3] src_Ycbcr_img_Cr = src_Ycbcr_img_Cr[:src_Ycbcr_img_Cr.shape[0] - src_Ycbcr_img_Cr.shape[0] % 3, :src_Ycbcr_img_Cr.shape[1] - src_Ycbcr_img_Cr.shape[0] % 3] #不同的L通道来源 #dst_Lab_img_L = readSR() #dst_Lab_img_L = imresize(src_Lab_img_L,3.0,'bicubic')/2.55 dst_YCbCr_img_Y = SR(src_Ycbcr_img_Y, Dh_dict, Dl_dict) dst_YCbCr_img_Cb = bicubic_2d.bicubic2d(src_Ycbcr_img_Cb, 3.0) dst_YCbCr_img_Cr = bicubic_2d.bicubic2d(src_Ycbcr_img_Cr, 3.0) img_YCbCr = np.zeros( (dst_YCbCr_img_Y.shape[0], dst_YCbCr_img_Y.shape[1], 3)) img_YCbCr[:, :, 0] = dst_YCbCr_img_Y img_YCbCr[:, :, 1] = dst_YCbCr_img_Cb img_YCbCr[:, :, 2] = dst_YCbCr_img_Cr dst = colormanage.ycbcr2rgb(img_YCbCr) src = src[:dst.shape[0], :dst.shape[1]] print psnr(dst[10:-15, 10:-15, 1], src[10:-15, 10:-15, 1]) print np.mean((dst[10:-15, 10:-15] - src[10:-15, 10:-15])**2) print np.mean(np.abs(dst[10:-15, 10:-15] * 255 - src[10:-15, 10:-15])) print psnr(dst[10:-15, 10:-15], src[10:-15, 10:-15]) plt.imshow(dst, interpolation="none") plt.show()
def IBP(imgh, imgl, scale): for i in range(1): lr = bicubic_2d.bicubic2d(imgh, 1 / 3.0) df = imgl - lr p = np.mean(np.abs(df)) print i, p if p < 0.001: break df = bicubic_2d.bicubic2d(df, 3.0) imgf = gaussian_filter(df, 0.5) imgh = imgh + imgf return imgh
def test_(): src = data.lena() src_rgb_img = src[100:150,100:150,:] src_YCrCb_img = rgb2ycbcr(src_rgb_img) img_lab = np.zeros((src_YCrCb_img.shape[0]*3,src_YCrCb_img.shape[1]*3,3)) img_lab[:,:,0] = bicubic_2d.bicubic2d(src_YCrCb_img[:,:,0],3.0) img_lab[:,:,1] = bicubic_2d.bicubic2d(src_YCrCb_img[:,:,1],3.0) img_lab[:,:,2] = bicubic_2d.bicubic2d(src_YCrCb_img[:,:,2],3.0) p = ycbcr2rgb(img_lab) c =np.asarray(p,dtype='uint8') plt.imshow(c) plt.show() # test_()
def m1(): #加载字典 mat = scipy.io.loadmat('./Dictionary/dictionary.mat') Dl_dict = np.asarray(mat['Dl'], dtype='float64') Dh_dict = np.asarray(mat['Dh'], dtype='float64') #提取出L通道进行计算其他通道使用Bicubic进行计算 #src_rgb_img = io.imread('./Data/Child_input.png') src_rgb_img = data.lena() src = src_rgb_img[:src_rgb_img.shape[0] - src_rgb_img.shape[0] % 3, :src_rgb_img.shape[1] - src_rgb_img.shape[0] % 3, :] src_rgb_img = src #需要变称散的倍数才能处理 #src_rgb_img = bicubic_2d.bicubic2d(src,1/3.0) src_rgb_img = bicubic_2d.bicubic2d(src_rgb_img, 1 / 3.0) src_rgb_img = src_rgb_img[:src_rgb_img.shape[0] - src_rgb_img.shape[0] % 3, :src_rgb_img.shape[1] - src_rgb_img.shape[0] % 3, :] #print src_rgb_img.shape src_Lab_img = color.rgb2lab(src_rgb_img) src_Lab_img_L = src_Lab_img[:, :, 0] src_Lab_img_a = src_Lab_img[:, :, 1] src_Lab_img_b = src_Lab_img[:, :, 2] #不同的L通道来源 #dst_Lab_img_L = readSR() #m = dst_Lab_img_L<0 #dst_Lab_img_L[m] = 0 #print np.max(dst_Lab_img_L),np.min(dst_Lab_img_L) #dst_Lab_img_L = SR(src_Lab_img_L,Dh_dict,Dl_dict) dst_Lab_img_L = imresize(src_Lab_img_L, 3.0, 'bicubic') #dst_Lab_img_L= dst_Lab_img_L/255.0*(np.max(src_Lab_img_L)-np.min(src_Lab_img_L))+np.min(src_Lab_img_L) dst_Lab_img_a = imresize(src_Lab_img_a, 3.0, 'bicubic') dst_Lab_img_a = dst_Lab_img_a / 255.0 * ( np.max(src_Lab_img_a) - np.min(src_Lab_img_a)) + np.min(src_Lab_img_a) dst_Lab_img_b = imresize(src_Lab_img_b, 3.0, 'bicubic') dst_Lab_img_b = dst_Lab_img_b / 255.0 * ( np.max(src_Lab_img_b) - np.min(src_Lab_img_b)) + np.min(src_Lab_img_b) img_lab = np.zeros((dst_Lab_img_L.shape[0], dst_Lab_img_L.shape[1], 3)) img_lab[:, :, 0] = dst_Lab_img_L img_lab[:, :, 1] = dst_Lab_img_a img_lab[:, :, 2] = dst_Lab_img_b dst = color.lab2rgb(img_lab) #src = src[:dst.shape[0],:dst.shape[1]] #print np.mean(dst[10:-15,10:-15]*255-src[10:-15,10:-15]) #print psnr(dst[10:-15,10:-15]*255,src[10:-15,10:-15]) plt.imshow(dst, interpolation="none") plt.show()
def m2(): #加载字典 mat = scipy.io.loadmat('./Dictionary/dictionary.mat') Dl_dict = np.asarray(mat['Dl'], dtype='float64') Dh_dict = np.asarray(mat['Dh'], dtype='float64') #提取出L通道进行计算其他通道使用Bicubic进行计算 src_rgb_img = io.imread('./Data/Child_input.png') src_yCrCb = colormanage.rgb2ycbcr(src_rgb_img) src_Ycbcr_img_Y = src_yCrCb[:, :, 0] src_Ycbcr_img_Cb = src_yCrCb[:, :, 1] src_Ycbcr_img_Cr = src_yCrCb[:, :, 2] src_Ycbcr_img_Y = src_Ycbcr_img_Y[:src_yCrCb.shape[0] - src_yCrCb.shape[0] % 3, :src_yCrCb.shape[1] - src_yCrCb.shape[1] % 3] src_Ycbcr_img_Cb = src_Ycbcr_img_Cb[:src_yCrCb.shape[0] - src_yCrCb.shape[0] % 3, :src_yCrCb.shape[1] - src_yCrCb.shape[1] % 3] src_Ycbcr_img_Cr = src_Ycbcr_img_Cr[:src_yCrCb.shape[0] - src_yCrCb.shape[0] % 3, :src_yCrCb.shape[1] - src_yCrCb.shape[1] % 3] #不同的L通道来源 #dst_Lab_img_L = readSR() #dst_YCbCr_img_Y = SR(src_Ycbcr_img_Y,Dh_dict,Dl_dict) dst_YCbCr_img_Y = bicubic_2d.bicubic2d(src_Ycbcr_img_Y, 3.0) dst_YCbCr_img_Cb = bicubic_2d.bicubic2d(src_Ycbcr_img_Cb, 3.0) dst_YCbCr_img_Cr = bicubic_2d.bicubic2d(src_Ycbcr_img_Cr, 3.0) img_YCbCr = np.zeros( (dst_YCbCr_img_Y.shape[0], dst_YCbCr_img_Y.shape[1], 3)) img_YCbCr[:, :, 0] = dst_YCbCr_img_Y img_YCbCr[:, :, 1] = dst_YCbCr_img_Cb img_YCbCr[:, :, 2] = dst_YCbCr_img_Cr dst = colormanage.ycbcr2rgb(img_YCbCr) joblib.dump(dst, 'child_result_bicubic_3x_ycbcr.pkl') plt.imshow(np.asarray(dst, dtype='uint8'), interpolation="none") plt.show()
def SR(img, Dh, Dl, l_da=0.0001, scale=3.0, patch_sizel=3.0, overlap=1.0, img_size=1.0): #l_da是经验值 #得到特征域 feat_scale = 2.0 patch_sizeh = patch_sizel * scale patch_sizem = patch_sizel * feat_scale imgm = bicubic_2d.bicubic2d(img, feat_scale) imgh = np.zeros((img.shape[0] * 3, img.shape[1] * 3), dtype='float64') img_bicubic = bicubic_2d.bicubic2d(img, 3.0) normalizationMat = np.zeros((img.shape[0] * 3, img.shape[1] * 3)) print img.shape, imgm.shape, imgh.shape f1 = np.asarray([[-1, 0, 1]], dtype='float64') f2 = np.asarray([[-1], [0], [1]], dtype='float64') f3 = np.asarray([[1, 0, -2, 0, 1]], dtype='float64') f4 = np.asarray([[1], [0], [-2], [0], [1]], dtype='float64') F = np.zeros((imgm.shape[0], imgm.shape[1], 4)) F[:, :, 0] = convolve2d(imgm, f1, mode='same') F[:, :, 1] = convolve2d(imgm, f2, mode='same') F[:, :, 2] = convolve2d(imgm, f3, mode='same') F[:, :, 3] = convolve2d(imgm, f4, mode='same') xgrid = np.ogrid[np.ceil(patch_sizel / 2.0):img.shape[1] - patch_sizel:patch_sizel - overlap] ygrid = np.ogrid[np.ceil(patch_sizel / 2.0):img.shape[0] - patch_sizel:patch_sizel - overlap] xgrid = np.asarray(xgrid) ygrid = np.asarray(ygrid) xgrid = np.append(xgrid, img.shape[1] - patch_sizel) ygrid = np.append(ygrid, img.shape[0] - patch_sizel) xgridm = (xgrid - 1) * feat_scale + 1 ygridm = (ygrid - 1) * feat_scale + 1 xgridh = (xgrid - 1) * scale + 1 ygridh = (ygrid - 1) * scale + 1 for i in range(len(xgridm)): for j in range(len(ygridm)): x, y = xgridm[i], ygridm[j] xh, yh = xgridh[i], ygridh[j] patchm = imgm[y:y + patch_sizem, x:x + patch_sizem] avg_patch = np.mean(patchm) featpatch = np.transpose(F[y:y + patch_sizem, x:x + patch_sizem, :], axes=(2, 0, 1)).reshape(patch_sizem * patch_sizem * 4) normalization_m = math.sqrt(np.sum(featpatch**2)) yy = [] if normalization_m > 1: yy = featpatch / normalization_m else: yy = featpatch clf = linear_model.Lasso(alpha=l_da) clf.fit(Dl, yy) w = clf.coef_ patchh = [] if normalization_m > 1: patchh = np.dot(Dh, w) * normalization_m else: patchh = np.dot(Dh, w) patchh = patchh.reshape([patch_sizeh, patch_sizeh]) + avg_patch imgh[yh:yh + patch_sizeh, xh:xh + patch_sizeh] = imgh[yh:yh + patch_sizeh, xh:xh + patch_sizeh] + patchh normalizationMat[yh:yh + patch_sizeh, xh:xh + patch_sizeh] = normalizationMat[ yh:yh + patch_sizeh, xh:xh + patch_sizeh] + 1 print x, y mask = normalizationMat == 0 normalizationMat[mask] = 1 imgh = imgh / normalizationMat imgh[mask] = img_bicubic[mask] joblib.dump(imgh, 'img_h0005.pkl') return imgh
def getTrainSet(img_lib,patch_dic_num): img_num = len(img_lib) #图像个数 #抽取字典~这个字典应该是HR-LRduo堆叠起来的结果 scale = 3.0 #放大倍数 feat_scale=2.0 patch_sizel = 3 #高清patch的尺寸, 对应底分辨率的patch就是 patch_size/scale patch_sizeh = patch_sizel*scale patch_sizem = patch_sizel*feat_scale HRpatch_lib = [] Fpatch_1_lab = [] Fpatch_2_lab = [] Fpatch_3_lab = [] Fpatch_4_lab = [] F_1_lab = [] F_2_lab = [] F_3_lab = [] F_4_lab = [] H_lib = [] f1=np.asarray([[-1,0,1]],dtype='float64') f2=np.asarray([[-1],[0],[1]],dtype='float64') f3=np.asarray([[1,0,-2,0,1]],dtype='float64') f4=np.asarray([[1],[0],[-2],[0],[1]],dtype='float64') #准备高清图像的高频部分,和LR的四种特征 for i in range(img_num): tmp = bicubic_2d.bicubic2d(img_lib[i],1/3.0) tmp2 = bicubic_2d.bicubic2d(tmp,2.0) tmp3 = bicubic_2d.bicubic2d(tmp,3.0) F_1_lab.append(convolve2d(tmp2,f1,mode='same')) F_2_lab.append(convolve2d(tmp2,f2,mode='same')) F_3_lab.append(convolve2d(tmp2,f3,mode='same')) F_4_lab.append(convolve2d(tmp2,f4,mode='same')) Hm = img_lib[i] Hm =Hm[ :Hm.shape[0]-Hm.shape[0]%3,:Hm.shape[1]-Hm.shape[1]%3] H_lib.append(Hm - tmp3) for i in range(patch_dic_num): img_i =random.randint(0,img_num-1) #得到去那一张图片。 img_temp = img_lib[img_i] y,x = [random.randint(0,img_temp.shape[d]/3 - 4) for d in (0,1)] ym = y * 2 xm = x * 2 yh = y * 3 xh = x * 3 HRpatch = H_lib[img_i][yh:yh+patch_sizeh,xh:xh+patch_sizeh] F1 = F_1_lab[img_i][ym:ym+patch_sizem,xm:xm+patch_sizem] F2 = F_2_lab[img_i][ym:ym+patch_sizem,xm:xm+patch_sizem] F3 = F_3_lab[img_i][ym:ym+patch_sizem,xm:xm+patch_sizem] F4 = F_4_lab[img_i][ym:ym+patch_sizem,xm:xm+patch_sizem] #高清patch HRpatch_lib.append(HRpatch) Fpatch_1_lab.append(F1) Fpatch_2_lab.append(F2) Fpatch_3_lab.append(F3) Fpatch_4_lab.append(F4) ylist = [] xlist = [] for i in range(len(HRpatch_lib)): H=HRpatch_lib[i] F=np.zeros((patch_sizem,patch_sizem,4)) F[:,:,0]= Fpatch_1_lab[i] F[:,:,1]= Fpatch_2_lab[i] F[:,:,2]= Fpatch_3_lab[i] F[:,:,3]= Fpatch_4_lab[i] xx=[] normalization_m = math.sqrt(np.sum(F**2)) if normalization_m > 1: xx = F/normalization_m else: xx = F yy=H-np.mean(H) ylist.append(yy.reshape((patch_sizeh*patch_sizeh))) xlist.append(xx.reshape((patch_sizem*patch_sizem*4))) return (xlist,ylist)