function [ResIm , ResIm_OMPonly] = ksvd_inpaint(trueIm, Im , Mask , patch)

% Params
param.patchSize = patch;
param.noiseSig = 0;
param.errorGoal = 0.04;
param.maxAtoms = 20;
param.nAtoms = 100;
param.IgnoreUnmasked = 1;

% Break image to columns (mask as well)
trueimPatches = im2col(trueIm , param.patchSize , 'sliding');
imPatches = im2col(Im , param.patchSize , 'sliding');
maskPatches = im2col(Mask , param.patchSize , 'sliding');

% Run specialized OMP

% Dictionary training param
param.method = 'KSVD';
param.nAtoms = 100; %64

param.nIterations = 4; 
param.useLessAtoms = [0.1 0.1 0.2 0.2 0.4 0.4 0.7 0.7 1 1 1 1 1 1 1];

param.initType = 'RandomPatches'; 
param.initType = 'DCT'; 


DCTdict = Extrapolate_Dictionary(trueimPatches, param);

% DCTdict = Build_DCT_Overcomplete_Dictionary(param.nAtoms , param.patchSize);


resPatches = OMP_Inpaint(DCTdict , imPatches , maskPatches, param);
resPatches=min(max(resPatches,0),255);

% Reconstruct image
ResIm_OMPonly = Average_Overlapping_Patches(resPatches, size(Im) , param.patchSize);

% Set the known pixels to the input values
% This should be used only when the noise is very weak
ResIm = ResIm_OMPonly;
ResIm(Mask) = Im(Mask);

% Finished
return;

