├── README.md ├── SetPath.m ├── data ├── Color Denoising Result.png ├── Gray Denoising Result.png ├── Inpainting Result-Random Occlusion .png ├── Inpainting Result-Text Mask.png ├── Super-resolution result.png └── dict_jdsr.mat ├── denoising ├── ColorDenoisingBySC.m ├── DemoColorDenoising.m ├── DemoGrayDenoising.m └── GrayDenoisingBySC.m ├── inpainting ├── ColorInpaintingBySC.m └── DemoInpiantingBySC.m ├── patch_match └── readme.md ├── style_transfer ├── DemoStyleTrans.m ├── TSST_AddNoise.m ├── TSST_PatchMatchAndAggr.m ├── TSST_PerformST.m ├── TSST_Preprocess.m └── TSST_SetParameters.m ├── super_res ├── ANR │ ├── ANR_ComputeProjMat.m │ ├── ANR_Reconstruct.m │ └── ANR_SetParams.m ├── Demo_ANR.m ├── Demo_JDSR.m ├── GetSRColorImage.m └── JDSR │ ├── JDSR_GlobalSolve.m │ ├── JDSR_LocalSolve.m │ ├── JDSR_PreProcess.m │ ├── JDSR_SetParams.m │ └── JDSR_Train.m └── utils ├── BiuldGaussianPyramid.m ├── CleanFileList.m ├── ImgFiltering.m ├── ImgPad.m ├── PatchExtraction.m ├── SearchNN.m └── VectorIndexing3D.m /README.md: -------------------------------------------------------------------------------- 1 | # PatchBasedImgProc 2 | 3 | patch based image processing, include denosing, super-resolution, inpainting and style transfer, etc. The purpose is for my self-education of those fileds. 4 | 5 | # Denoising 6 | 7 | A simple implementation of the sparse representation based methods. 8 | 9 | ![Gray Image Denosing Example](https://github.com/galad-loth/ImageRecovTrans/blob/master/data/Gray%20Denoising%20Result.png) 10 | 11 | ![Color Image Denosing Example](https://github.com/galad-loth/ImageRecovTrans/blob/master/data/Color%20Denoising%20Result.png) 12 | 13 | # Super-resolution 14 | 15 | Two methods are tried: sparse representation/anchor neighborhood regression with jointly learned dictionary . 16 | 17 | ![Super-Resolution Example](https://github.com/galad-loth/ImageRecovTrans/blob/master/data/Super-resolution%20result.png) 18 | 19 | # Inpainting 20 | Currently, only sparse coding base inpainting method is implemented, which can restore images patially corrupted by random noise or thin-structrures(e.g., text). 21 | 22 | ![Random-Occlusion Inpainting Example](https://github.com/galad-loth/ImageRecovTrans/blob/master/data/Inpainting%20Result-Random%20Occlusion%20.png) 23 | 24 | ![Text Removal Example](https://github.com/galad-loth/ImageRecovTrans/blob/master/data/Inpainting%20Result-Text%20Mask.png) 25 | 26 | It is also intended to try exempler based inpainting method in future. 27 | 28 | # style transfer 29 | 30 | It is intended to implement the style-transfer methods proposed recently by Michael Elad and Peyman Milanfar, which is a non-CNN based method. 31 | 32 | # References 33 | 34 | 1. Elad M, Aharon M. Image denoising via sparse and redundant representations over learned dictionaries[J]. IEEE Transactions on Image processing, 2006, 15(12): 3736-3745. 35 | 36 | 2. Yang J, Wright J, Huang T, et al. Image super-resolution as sparse representation of raw image patches[C]//Computer Vision and Pattern Recognition, 2008. CVPR 2008. IEEE Conference on. IEEE, 2008: 1-8. 37 | 38 | 3. Yang J, Wright J, Huang T S, et al. Image super-resolution via sparse representation[J]. IEEE transactions on image processing, 2010, 19(11): 2861-2873. 39 | 40 | 4. Timofte R, De Smet V, Van Gool L. Anchored neighborhood regression for fast example-based super-resolution[C]//Proceedings of the IEEE International Conference on Computer Vision. 2013: 1920-1927. 41 | 42 | 5. Barnes C, Shechtman E, Finkelstein A, et al. PatchMatch: A randomized correspondence algorithm for structural image editing[J]. ACM Trans. Graph., 2009, 28(3): 24:1-24:11. 43 | 44 | 6. Korman S, Avidan S. Coherency sensitive hashing[C]//Computer Vision (ICCV), 2011 IEEE International Conference on. IEEE, 2011: 1607-1614. 45 | 46 | 7. Arias P, Facciolo G, Caselles V, et al. A variational framework for exemplar-based image inpainting[J]. International journal of computer vision, 2011, 93(3): 319-347. 47 | 48 | 8. Liu Y, Caselles V. Exemplar-based image inpainting using multiscale graph cuts[J]. IEEE transactions on image processing, 2013, 22(5): 1699-1711. 49 | 50 | 9. Elad M, Milanfar P. Style Transfer Via Texture Synthesis[J]. IEEE Transactions on Image Processing, 2017, 26(5): 2338-2351. 51 | -------------------------------------------------------------------------------- /SetPath.m: -------------------------------------------------------------------------------- 1 | function SetPath(action) 2 | PathStr=genpath(pwd); 3 | if strcmp(action,'add') 4 | addpath(PathStr); 5 | elseif strcmp(action,'remove') 6 | rmpath(PathStr); 7 | end -------------------------------------------------------------------------------- /data/Color Denoising Result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/Color Denoising Result.png -------------------------------------------------------------------------------- /data/Gray Denoising Result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/Gray Denoising Result.png -------------------------------------------------------------------------------- /data/Inpainting Result-Random Occlusion .png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/Inpainting Result-Random Occlusion .png -------------------------------------------------------------------------------- /data/Inpainting Result-Text Mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/Inpainting Result-Text Mask.png -------------------------------------------------------------------------------- /data/Super-resolution result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/Super-resolution result.png -------------------------------------------------------------------------------- /data/dict_jdsr.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/galad-loth/PatchBasedImgProc/3be527baafbcc326c5d8bf72ebfed53b0c42cc34/data/dict_jdsr.mat -------------------------------------------------------------------------------- /denoising/ColorDenoisingBySC.m: -------------------------------------------------------------------------------- 1 | function img_out=ColorDenoisingBySC(img_in, params) 2 | 3 | [nrow, ncol, nchl]=size(img_in); 4 | img_out=zeros(nrow, ncol,nchl); 5 | aggr_times=zeros(nrow, ncol); 6 | 7 | stride=params.stride; 8 | patch_size=params.patch_size; 9 | batch_size=params.batch_size; 10 | dict=params.dict; 11 | proj_mat=params.proj_mat; 12 | dict_rev=proj_mat\dict; 13 | 14 | nrow_lu=1:stride:(nrow-patch_size+1); 15 | ncol_lu=1:stride:(ncol-patch_size+1); 16 | [ncol_lu, nrow_lu]=meshgrid(ncol_lu, nrow_lu); 17 | nrow_lu=nrow_lu(:); 18 | ncol_lu=ncol_lu(:); 19 | 20 | patch_num=length(nrow_lu); 21 | batch_num=1; 22 | if patch_num>batch_size 23 | batch_num=ceil(patch_num/batch_size); 24 | end 25 | 26 | if strcmp(params.sc_method, 'lasso') 27 | sc_params.mode=0; 28 | sc_params.lambda=params.lambda; 29 | else 30 | sc_params.L=params.lambda; 31 | end 32 | 33 | for idx_batch=1:batch_num 34 | if (batch_num>1 && params.verbose) 35 | disp(['Batch processing: ', num2str(idx_batch),'/', num2str(batch_num)]); 36 | end 37 | idx_patch=(idx_batch-1)*batch_size+1:min(patch_num,idx_batch*batch_size); 38 | batch_data=PatchExtraction(img_in, patch_size, 0, 'fixloc',nrow_lu(idx_patch),ncol_lu(idx_patch)); 39 | batch_data=proj_mat*batch_data; 40 | % batch_mean=mean(batch_data, 1); 41 | % batch_data=bsxfun(@minus, batch_data, batch_mean); 42 | % batch_norm=sqrt(sum(batch_data.*batch_data,1)); 43 | % batch_data=bsxfun(@times,batch_data, 1./(batch_norm+eps)); 44 | if strcmp(params.sc_method, 'lasso') 45 | alpha=mexLasso(batch_data, dict,sc_params); 46 | else 47 | alpha=mexOMP(batch_data, dict,sc_params); 48 | end 49 | batch_data=dict_rev*full(alpha); 50 | % batch_data=bsxfun(@times,batch_data, batch_norm); 51 | % batch_data=bsxfun(@plus, batch_data,batch_mean); 52 | for i=1:length(idx_patch) 53 | nrlu=nrow_lu(idx_patch(i)); 54 | nclu=ncol_lu(idx_patch(i)); 55 | img_out(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1,:)=... 56 | img_out(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1,:)... 57 | +reshape(batch_data(:,i),[patch_size, patch_size, nchl]); 58 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)=... 59 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)+1; 60 | end 61 | end 62 | 63 | img_out=img_out./(eps+repmat(aggr_times,[1,1,nchl])); 64 | img_out=(img_out+params.beta*img_in)/(1+params.beta); 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /denoising/DemoColorDenoising.m: -------------------------------------------------------------------------------- 1 | clear; close all;clc 2 | img=imread('E:\DevProj\_Datasets\Denoising\color\bird.png'); 3 | [nrow, ncol, nchl]=size(img); 4 | verbose=true; 5 | 6 | img=double(img); 7 | signa_noise=25; 8 | img_noise=img+signa_noise*randn([nrow, ncol, nchl]); 9 | 10 | img=img/256; 11 | img_noise=img_noise/256; 12 | img_gf=ImgFiltering(img_noise,'gaussian', 5, 1.5); 13 | figure;set(gcf, 'position',[400, 100, 800, 800]) 14 | subplot(2,2,1);imagesc(img);colormap(gray);title('Clean image') 15 | subplot(2,2,2);imagesc(img_noise);colormap(gray);title('Noise image') 16 | subplot(2,2,3);imagesc(img_gf);title('Gaussian filtering image') 17 | pause(0.05) 18 | 19 | if (verbose) 20 | disp('Training dictionary...'); 21 | end 22 | 23 | patch_size=13; 24 | pad_flag=0; 25 | crop_type='rand'; 26 | num_patch=15000; 27 | corr_gamma=1.5; 28 | patch_size_sq=patch_size*patch_size; 29 | proj_mat=zeros(patch_size_sq*nchl,patch_size_sq*nchl); 30 | for i=1:nchl 31 | idx_range=((i-1)*patch_size_sq+1):i*patch_size_sq; 32 | proj_mat(idx_range,idx_range)=1; 33 | end 34 | proj_mat=eye(patch_size_sq*nchl)... 35 | +corr_gamma/patch_size_sq*proj_mat; 36 | train_data=PatchExtraction(img_noise, patch_size, pad_flag, crop_type, num_patch); 37 | train_data=proj_mat*train_data; 38 | % train_data=bsxfun(@minus, train_data, mean(train_data,1)); 39 | 40 | train_params.K=256; 41 | train_params.mode=3; 42 | train_params.lambda=5; 43 | train_params.iter=150; 44 | dict=mexTrainDL(train_data,train_params); 45 | 46 | if (verbose) 47 | disp('Perform denoising...'); 48 | end 49 | params.patch_size=patch_size; 50 | params.dict=dict; 51 | params.stride=1; 52 | params.beta=0.25; % weight of the reconstructed image 53 | params.batch_size=10000; 54 | params.verbose=verbose; 55 | params.sc_method='omp'; 56 | params.proj_mat=proj_mat; 57 | if strcmp(params.sc_method, 'lasso') 58 | params.lambda=0.4;%L1 penalty 59 | else 60 | params.lambda=5;%L0 penalty 61 | end 62 | img_denoise=ColorDenoisingBySC(img_noise, params); 63 | img_denoise(img_denoise<0)=0; 64 | img_denoise(img_denoise>1)=1; 65 | subplot(2,2,4);imagesc(img_denoise);title('SC denoising image') 66 | 67 | figure(2);displayPatches(dict);title('Learned dictionary'); 68 | pause(0.05) -------------------------------------------------------------------------------- /denoising/DemoGrayDenoising.m: -------------------------------------------------------------------------------- 1 | clear all;close all;clc 2 | img=imread('E:\DevProj\_Datasets\MiscData\Barbara.png'); 3 | if (size(img,3)==3) 4 | img=double(rgb2gray(img)); 5 | else 6 | img=double(img); 7 | end 8 | [nrow, ncol]=size(img); 9 | verbose=true; 10 | 11 | signa_noise=15; 12 | img_noise=img+signa_noise*randn([nrow, ncol]); 13 | figure;set(gcf, 'position',[400, 100, 800, 800]) 14 | subplot(2,2,1);imagesc(img,[0 255]);colormap(gray);title('Clean image') 15 | subplot(2,2,2);imagesc(img_noise,[0,255]);colormap(gray);title('Noise image') 16 | pause(0.05) 17 | 18 | if (verbose) 19 | disp('Training dictionary...'); 20 | end 21 | img_noise=img_noise/256; 22 | patch_size=13; 23 | pad_flag=0; 24 | crop_type='rand'; 25 | num_patch=10000; 26 | train_data=PatchExtraction(img_noise, patch_size, pad_flag, crop_type, num_patch); 27 | train_data=bsxfun(@minus, train_data, mean(train_data,1)); 28 | 29 | train_params.K=128; 30 | train_params.mode=3; 31 | train_params.lambda=5; 32 | train_params.iter=150; 33 | dict=mexTrainDL(train_data,train_params); 34 | subplot(2,2,3);displayPatches(dict);colormap(gray);title('Learned dictionary'); 35 | pause(0.05) 36 | 37 | if (verbose) 38 | disp('Perform denoising...'); 39 | end 40 | params.patch_size=patch_size; 41 | params.dict=dict; 42 | params.stride=1; 43 | params.beta=0.01; % weight of the reconstructed image 44 | params.batch_size=10000; 45 | params.verbose=verbose; 46 | params.sc_method='omp'; 47 | if strcmp(params.sc_method, 'lasso') 48 | params.lambda=0.4;%L1 penalty 49 | else 50 | params.lambda=5;%L0 penalty 51 | end 52 | img_denoise=GrayDenoisingBySC(img_noise, params); 53 | img_denoise=img_denoise*256; 54 | subplot(2,2,4);imagesc(img_denoise,[0 255]);colormap(gray);title('Denoising image') 55 | -------------------------------------------------------------------------------- /denoising/GrayDenoisingBySC.m: -------------------------------------------------------------------------------- 1 | function img_out=GrayDenoisingBySC(img_in, params) 2 | 3 | [nrow, ncol]=size(img_in); 4 | img_out=zeros(nrow, ncol); 5 | aggr_times=zeros(nrow, ncol); 6 | 7 | stride=params.stride; 8 | patch_size=params.patch_size; 9 | batch_size=params.batch_size; 10 | dict=params.dict; 11 | 12 | nrow_lu=1:stride:(nrow-patch_size+1); 13 | ncol_lu=1:stride:(ncol-patch_size+1); 14 | [ncol_lu, nrow_lu]=meshgrid(ncol_lu, nrow_lu); 15 | nrow_lu=nrow_lu(:); 16 | ncol_lu=ncol_lu(:); 17 | 18 | patch_num=length(nrow_lu); 19 | batch_num=1; 20 | if patch_num>batch_size 21 | batch_num=ceil(patch_num/batch_size); 22 | end 23 | 24 | if strcmp(params.sc_method, 'lasso') 25 | sc_params.mode=0; 26 | sc_params.lambda=params.lambda; 27 | else 28 | sc_params.L=params.lambda; 29 | end 30 | 31 | for idx_batch=1:batch_num 32 | if (batch_num>1 && params.verbose) 33 | disp(['Batch processing: ', num2str(idx_batch),'/', num2str(batch_num)]); 34 | end 35 | idx_patch=(idx_batch-1)*batch_size+1:min(patch_num,idx_batch*batch_size); 36 | batch_data=PatchExtraction(img_in, patch_size, 0, 'fixloc',nrow_lu(idx_patch),ncol_lu(idx_patch)); 37 | batch_mean=mean(batch_data, 1); 38 | batch_data=bsxfun(@minus, batch_data, batch_mean); 39 | batch_norm=sqrt(sum(batch_data.*batch_data,1)); 40 | batch_data=bsxfun(@times,batch_data, 1./(batch_norm+eps)); 41 | if strcmp(params.sc_method, 'lasso') 42 | alpha=mexLasso(batch_data, dict,sc_params); 43 | else 44 | alpha=mexOMP(batch_data, dict,sc_params); 45 | end 46 | batch_data=dict*full(alpha); 47 | batch_data=bsxfun(@times,batch_data, batch_norm); 48 | batch_data=bsxfun(@plus, batch_data,batch_mean); 49 | for i=1:length(idx_patch) 50 | nrlu=nrow_lu(idx_patch(i)); 51 | nclu=ncol_lu(idx_patch(i)); 52 | img_out(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)=... 53 | img_out(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)... 54 | +reshape(batch_data(:,i),[patch_size patch_size]); 55 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)=... 56 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)+1; 57 | end 58 | end 59 | 60 | img_out=img_out./(eps+aggr_times); 61 | img_out=(img_out+params.beta*img_in)/(1+params.beta); 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /inpainting/ColorInpaintingBySC.m: -------------------------------------------------------------------------------- 1 | function img_out=ColorInpaintingBySC(img_in, params) 2 | 3 | [nrow, ncol, nchl]=size(img_in); 4 | img_out=zeros(nrow, ncol, nchl); 5 | 6 | img_mask=params.img_mask; 7 | 8 | stride=params.stride; 9 | patch_size=params.patch_size; 10 | dict=params.dict; 11 | % proj_mat=params.proj_mat; 12 | dict_rev=dict; 13 | 14 | if strcmp(params.sc_method, 'lasso') 15 | sc_params.mode=0; 16 | sc_params.lambda=params.lambda; 17 | else 18 | sc_params.L=params.lambda; 19 | end 20 | 21 | aggr_times=zeros(nrow, ncol); 22 | for nrlu=1:stride:(nrow-patch_size+1) 23 | if (params.verbose) 24 | disp(['Sparse coding based inpainting, processling line ', num2str(nrlu)]); 25 | end 26 | for nclu=1:stride:(ncol-patch_size+1) 27 | local_patch=img_in(nrlu:nrlu+patch_size-1, nclu:nclu+patch_size-1,:); 28 | local_mask=img_mask(nrlu:nrlu+patch_size-1, nclu:nclu+patch_size-1); 29 | % local_aggrtimes=aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1); 30 | local_patch_proj=local_patch(:); 31 | local_mask=repmat(local_mask,[1,1, nchl]); 32 | local_validflag=(local_mask(:)~=0); 33 | local_signal=sum(local_validflag); 34 | 35 | local_dict=dict(local_validflag, :); 36 | w_norm_dict=1./sqrt(eps+diag(local_dict'*local_dict)); 37 | local_dict=local_dict*diag(w_norm_dict); 38 | 39 | if strcmp(params.sc_method, 'lasso') 40 | alpha=mexLasso(local_patch_proj(local_validflag), local_dict,sc_params); 41 | else 42 | alpha=mexOMP(local_patch_proj(local_validflag), local_dict ,sc_params); 43 | end 44 | local_patch_recov=dict_rev*(w_norm_dict.*full(alpha)); 45 | local_patch_recov=reshape(local_patch_recov, [patch_size, patch_size, nchl]); 46 | img_out(nrlu:nrlu+patch_size-1, nclu:nclu+patch_size-1,:)=... 47 | img_out(nrlu:nrlu+patch_size-1, nclu:nclu+patch_size-1,:)+... 48 | local_patch_recov*local_signal; 49 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)=... 50 | aggr_times(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)+local_signal; 51 | end 52 | end 53 | 54 | img_out=img_out./(eps+repmat(aggr_times,[1,1,nchl])); 55 | 56 | 57 | -------------------------------------------------------------------------------- /inpainting/DemoInpiantingBySC.m: -------------------------------------------------------------------------------- 1 | clear; close all;clc 2 | img=imread('E:\DevProj\_Datasets\Denoising\color\house.png'); 3 | [nrow, ncol, nchl]=size(img); 4 | verbose=true; 5 | img=double(img)/256; 6 | 7 | img_mask=imread('E:\DevProj\_Datasets\Denoising\color\text_mask.png'); 8 | % img_mask=uint8(rand(nrow, ncol)>0.6); 9 | [nrowm, ncolm]=size(img_mask); 10 | if (nrowm>nrow || ncolm>ncol) 11 | error('The size of mask is larger than the size of image') 12 | else 13 | img_mask1=uint8(ones(nrow, ncol)); 14 | nrow0=floor(nrow/2-nrowm/2)+1; 15 | ncol0=floor(ncol/2-ncolm/2)+1; 16 | img_mask1(nrow0:(nrow0+nrowm-1), ncol0:(ncol0+ncolm-1))=img_mask; 17 | img_mask=img_mask1; 18 | end 19 | 20 | img_corrput=reshape(img,[nrow*ncol, nchl]); 21 | img_corrput(img_mask==0,:)=0; 22 | img_corrput=reshape(img_corrput, [nrow, ncol, nchl]); 23 | 24 | figure;set(gcf, 'position',[400, 100, 800, 800]) 25 | subplot(2,2,1);imagesc(img);title('Original image') 26 | subplot(2,2,2);imagesc(img_corrput);title('Corrupted image') 27 | pause(0.05) 28 | 29 | if (verbose) 30 | disp('Training dictionary...'); 31 | end 32 | 33 | patch_size=7; 34 | pad_flag=0; 35 | crop_type='rand'; 36 | num_patch=10000; 37 | corr_gamma=1.5; 38 | patch_size_sq=patch_size*patch_size; 39 | proj_mat=zeros(patch_size_sq*nchl,patch_size_sq*nchl); 40 | for i=1:nchl 41 | idx_range=((i-1)*patch_size_sq+1):i*patch_size_sq; 42 | proj_mat(idx_range,idx_range)=1; 43 | end 44 | proj_mat=eye(patch_size_sq*nchl)... 45 | +corr_gamma/patch_size_sq*proj_mat; 46 | train_data=PatchExtraction(img, patch_size, pad_flag, crop_type, num_patch); 47 | % train_data=proj_mat*train_data; 48 | % train_data=bsxfun(@minus, train_data, mean(train_data,1)); 49 | 50 | train_params.K=256; 51 | train_params.mode=3; 52 | train_params.lambda=10; 53 | train_params.iter=150; 54 | dict=mexTrainDL(train_data,train_params); 55 | subplot(2,2,3);displayPatches(dict);title('Learned dictionary'); 56 | pause(0.05) 57 | 58 | 59 | if (verbose) 60 | disp('Perform denoising...'); 61 | end 62 | params.patch_size=patch_size; 63 | params.dict=dict; 64 | params.stride=1; 65 | params.verbose=verbose; 66 | params.sc_method='omp'; 67 | params.proj_mat=proj_mat; 68 | params.img_mask=img_mask; 69 | if strcmp(params.sc_method, 'lasso') 70 | params.lambda=0.4;%L1 penalty 71 | else 72 | params.lambda=8;%L0 penalty 73 | end 74 | img_inpaint=ColorInpaintingBySC(img_corrput, params); 75 | img_inpaint(img_inpaint<0)=0; 76 | img_inpaint(img_inpaint>1)=1; 77 | subplot(2,2,4);imagesc(img_inpaint);title('SC inpainting image') 78 | 79 | -------------------------------------------------------------------------------- /patch_match/readme.md: -------------------------------------------------------------------------------- 1 | code of patch match: http://gfx.cs.princeton.edu/pubs/Barnes_2009_PAR/index.php 2 | 3 | Connelly Barnes, Eli Shechtman, Adam Finkelstein, Dan B Goldman. PatchMatch: A Randomized Correspondence Algorithm for Structural Image Editing 4 | ACM Transactions on Graphics (Proc. SIGGRAPH), August 2009 5 | 6 | -------------------------------------------------------------------------------- /style_transfer/DemoStyleTrans.m: -------------------------------------------------------------------------------- 1 | clear;close all;clc 2 | params=TSST_SetParameters(); 3 | img1=imread(params.content_img); 4 | img2=imread(params.style_img); 5 | figure(1);set(gcf, 'position',[200, 100, 1200, 800]) 6 | subplot(2,2,1);imagesc(img1);title('content image') 7 | subplot(2,2,2);imagesc(img2);title('style image') 8 | 9 | data=TSST_Preprocess(img1, img2, params); 10 | subplot(2,2,3);imagesc(data.seg_mask);title('segmantation mask') 11 | imgst=TSST_PerformST(data, params); 12 | subplot(2,2,4);imagesc(uint8(imgst));title('style transfer image') -------------------------------------------------------------------------------- /style_transfer/TSST_AddNoise.m: -------------------------------------------------------------------------------- 1 | function img_out=TSST_AddNoise(img_in, xigma) 2 | [nrow, ncol, nchl]=size(img_in); 3 | img_out=img_in+xigma*randn([nrow, ncol, nchl]); 4 | minval=-xigma; 5 | maxval=255+xigma; 6 | 7 | for k=1:nchl 8 | imgk=img_out(:,:,k); 9 | idx1=imgkmaxval; 11 | imgk(idx1)=minval; 12 | imgk(idx2)=maxval; 13 | img_out(:,:,k)=(imgk-minval)*255/(maxval-minval); 14 | end 15 | 16 | 17 | -------------------------------------------------------------------------------- /style_transfer/TSST_PatchMatchAndAggr.m: -------------------------------------------------------------------------------- 1 | function imgout=TSST_PatchMatchAndAggr(imgin, style_patch, idx_patch_size, params) 2 | 3 | [nrow, ncol, nchl]=size(imgin); 4 | 5 | patch_size=params.patch_size(idx_patch_size); 6 | stride=params.stride(idx_patch_size); 7 | batch_size=params.nn_batchsize; 8 | r=params.r; 9 | 10 | nrow_lu=1:stride:(nrow-patch_size+1); 11 | ncol_lu=1:stride:(ncol-patch_size+1); 12 | [ncol_lu, nrow_lu]=meshgrid(ncol_lu, nrow_lu); 13 | nrow_lu=nrow_lu(:); 14 | ncol_lu=ncol_lu(:); 15 | 16 | patch_num=length(nrow_lu); 17 | batch_num=1; 18 | if patch_num>batch_size 19 | batch_num=ceil(patch_num/batch_size); 20 | end 21 | 22 | imgout=imgin; 23 | for iter=1:params.iter_irls 24 | if (params.verbose) 25 | disp(['IRSL iteration ', num2str(iter)]) 26 | end 27 | img_temp=zeros(nrow, ncol, nchl); 28 | aggr_weight=zeros(nrow, ncol); 29 | for idx_batch=1:batch_num 30 | idx_patch=(idx_batch-1)*batch_size+1:min(patch_num,idx_batch*batch_size); 31 | batch_data=PatchExtraction(imgout, patch_size, 0, 'fixloc',nrow_lu(idx_patch),ncol_lu(idx_patch)); 32 | dist_mat=pdist2(batch_data', style_patch'); 33 | [dist_sort, idx_sort]=sort(dist_mat, 2,'ascend'); 34 | for i=1:length(idx_patch) 35 | nrlu=nrow_lu(idx_patch(i)); 36 | nclu=ncol_lu(idx_patch(i)); 37 | idxnn=idx_sort(i,1); 38 | weightnn=(eps+dist_sort(i,1)).^(r-2); 39 | img_temp(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1,:)=... 40 | img_temp(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1,:)... 41 | +weightnn*reshape(style_patch(:,idxnn),[patch_size, patch_size, nchl]); 42 | aggr_weight(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)=... 43 | aggr_weight(nrlu:nrlu+patch_size-1,nclu:nclu+patch_size-1)+weightnn; 44 | end 45 | end 46 | aggr_weight=repmat(aggr_weight,[1,1,nchl]); 47 | imgout=img_temp./(aggr_weight+eps); 48 | end 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /style_transfer/TSST_PerformST.m: -------------------------------------------------------------------------------- 1 | function imgst=TSST_PerformST(data, params) 2 | imgst=data.imgc_pyd{params.num_level}; 3 | imgst=TSST_AddNoise(imgst, params.std_noise); 4 | imgst=imgst/255; 5 | 6 | for i=1:params.num_level 7 | imgs=data.imgs_pyd{params.num_level-i+1}/255; 8 | imgc=data.imgc_pyd{params.num_level-i+1}/255; 9 | seg_mask=data.mask_pyd{params.num_level-i+1}; 10 | imgc_weight=params.imgc_weight(i); 11 | [nr, nc, nd]=size(imgc); 12 | [nrs, ncs, nds]=size(imgc); 13 | seg_mask=repmat(seg_mask, [1,1, nd]); 14 | for j=1:params.num_patch_size 15 | patch_size=params.patch_size(j); 16 | if (params.verbose) 17 | disp(['TSST, level ', num2str(i), ', patch_size ', num2str(patch_size)]) 18 | end 19 | patch_num=min(params.patch_num, floor(nrs*ncs/patch_size)); 20 | style_patch=PatchExtraction(imgs, patch_size, 0, 'rand', patch_num); 21 | imgst=TSST_PatchMatchAndAggr(imgst, style_patch, j, params); 22 | if (params.save_inter_result) 23 | imwrite(uint8(imgst*255), ['aggr_result_l', num2str(i),'_p', num2str(patch_size),'.png']); 24 | end 25 | 26 | imgst=(imgst+imgc.*seg_mask*imgc_weight)./(1+seg_mask*imgc_weight); 27 | imgst=imhistmatch(imgst, imgs); 28 | % imgst=Denoising(imgst, params); 29 | if (params.save_inter_result) 30 | imwrite(uint8(imgst*255), ['st_result_l', num2str(i),'_p', num2str(patch_size),'.png']); 31 | end 32 | end 33 | if (i