No BSD License  

Highlights from
Spatial Error Concealment

image thumbnail

Spatial Error Concealment

by

 

16 Jan 2008 (Updated )

Implements video and static image spatial error concealment techniques with block error simulation i

Spat_Conceal(varargin)
function varargout = Spat_Conceal(varargin)
% SPAT_CONCEAL M-file for Spat_Conceal.fig
%      SPAT_CONCEAL, by itself, creates a new SPAT_CONCEAL or raises the existing
%      singleton*.
%
%      H = SPAT_CONCEAL returns the handle to a new SPAT_CONCEAL or the handle to
%      the existing singleton*.
%
%      SPAT_CONCEAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SPAT_CONCEAL.M with the given input arguments.
%
%      SPAT_CONCEAL('Property','Value',...) creates a new SPAT_CONCEAL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Spat_Conceal_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Spat_Conceal_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Spat_Conceal

% Last Modified by GUIDE v2.5 25-Oct-2007 15:02:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Spat_Conceal_OpeningFcn, ...
                   'gui_OutputFcn',  @Spat_Conceal_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Spat_Conceal is made visible.
function Spat_Conceal_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Spat_Conceal (see VARARGIN)

% Choose default command line output for Spat_Conceal
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Spat_Conceal wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Spat_Conceal_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global IMG;

text1=findobj(gcbf, 'Tag', 'edit1');
fileName=handles.activex1.FileName;
try
    IMG=imread(fileName);
    temp=get(handles.axes1, 'ButtonDownFcn');
    imagesc(IMG, 'Parent', handles.axes1);
    colormap('gray');
    prop=get(handles.axes1);
    set(handles.axes1, 'ButtonDownFcn',temp);
    set(prop.Children, 'ButtonDownFcn',temp);
catch
end


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global IMG;
% try
%     imaqreset();
%     obj = videoinput('winvideo', 1);
%     IMG=getsnapshot(obj);
%     temp=get(handles.axes1, 'ButtonDownFcn');
%     image(IMG, 'Parent', handles.axes1);
%     prop=get(handles.axes1);
%     set(handles.axes1, 'ButtonDownFcn',temp);
%     set(prop.Children, 'ButtonDownFcn',temp);
%     delete (obj);
% catch
%     error ('Error in the frame capture. Verify your capture device');
% end


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global IMG;
global BS;
global CONC_TYPE;
global IMAGE_COUNT;



if(isempty(BS))
    BS=8;
end

if(~isempty(IMG))
    prop=get(handles.axes1);
    if(strcmp(CONC_TYPE, 'free'))
        Xcoord=round(prop.CurrentPoint(1,1));
        Ycoord=round(prop.CurrentPoint(1,2));
    else
        Xcoord=BS*(fix(double(prop.CurrentPoint(1,1)-1)/BS))+1;
        Ycoord=BS*(fix(double(prop.CurrentPoint(1,2)-1)/BS))+1;
    end
    if((Xcoord-1>=1)&&(Xcoord+BS<=size(IMG,2))&&(Ycoord-1>=1)&&(Ycoord+BS<=size(IMG,1)))
        %Here the magic happens!!!
        img2=IMG;
        index=1;
        if(strcmp(CONC_TYPE, 'block'))
            for(i=2*BS+1:2*BS:size(IMG,1)-BS);
                for(j=2*BS+1:2*BS:size(IMG,2)-BS);
                    img2(i:i+BS-1, j:j+BS-1)=0;
                    Ycoord(index)=i;
                    Xcoord(index)=j;
                    index=index+1;
                end
            end
        end
                
        img2(Ycoord:Ycoord+BS-1, Xcoord:Xcoord+BS-1,:)=0;
        temp=get(handles.axes1, 'ButtonDownFcn');
        imagesc(img2, 'Parent', handles.axes1);
        colormap('gray');
        mkdir ('.\', get(handles.edit4, 'String') );
        imwrite(img2, strcat('.\', get(handles.edit4, 'String'), '\damaged_', num2str(IMAGE_COUNT), '.png'), 'png');
        tic;
        set(handles.edit3, 'String', ' ... ');
        drawnow;


        k = waitforbuttonpress;
                
        for(i=1:size(Ycoord,2))

            if(get(handles.radiobutton2, 'Value'))
                %Method 2 - applies for each pixel a mean value of the vertical and
                %horizontal neighbors (up, down, left and right), proportional to its distance to the
                %point
                 method_str = 'Pixel Interpolation';
                for(y=0:BS-1)
                    for(x=0:BS-1)
                        %all channels (gray or R,G,B)
                        pixel=double(img2(Ycoord(i)-1,Xcoord(i)+x,:))*0.5*(BS-y)/(BS+1); %up
                        pixel=pixel+double(img2(Ycoord(i)+BS,Xcoord(i)+x,:))*0.5*(y+1)/(BS+1); %down
                        pixel=pixel+double(img2(Ycoord(i)+y,Xcoord(i)-1,:))*0.5*(BS-x)/(BS+1); %left
                        pixel=pixel+double(img2(Ycoord(i)+y,Xcoord(i)+BS,:))*0.5*(x+1)/(BS+1); %right
                        img2(Ycoord(i)+y,Xcoord(i)+x,:)=uint8(pixel);
                    end
                end
            end

            if(get(handles.radiobutton4, 'Value'))
                %Method 3 - Frequency strapolation
                %sas -> suportArea size 
                 method_str = 'Frequency Extrapolation';
                if(BS==8)
                    sas=6;
                elseif (BS==16)
                    sas=13;
                end
                if(Ycoord(i)-sas>=1&&Ycoord(i)+BS+sas-1<=size(img2,1))
                    if(Xcoord(i)-sas>=1&&Xcoord(i)+BS+sas-1<=size(img2,2))
                        img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:)=spatExtrap( ...
                            img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:), BS, sas );
                    end
                end
            end
            if(get(handles.radiobutton5, 'Value'))
                %Method 2 - Directional Interpolation
                %sas -> suportArea size 
                method_str = 'Directional Interpolation';
                if(BS==8)
                    sas=6;
                elseif (BS==16)
                    sas=14;
                end
                if(Ycoord(i)-sas>=1&&Ycoord(i)+BS+sas-1<=size(img2,1))
                    if(Xcoord(i)-sas>=1&&Xcoord(i)+BS+sas-1<=size(img2,2))
                        img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:)=DI( ...
                            img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:), BS, sas );
                    end
                end
            end
            
            if(get(handles.radiobutton6, 'Value'))
                %Method 4 - Directional Interpolation full
                %sas -> suportArea size 
                method_str = 'DI_full';
                if(BS==8)
                    sas=6;
                elseif (BS==16)
                    sas=14;
                end
                if(Ycoord(i)-sas>=1&&Ycoord(i)+BS+sas-1<=size(img2,1))
                    if(Xcoord(i)-sas>=1&&Xcoord(i)+BS+sas-1<=size(img2,2))
                        img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:)=DI_full( ...
                            img2(Ycoord(i)-sas:Ycoord(i)+BS+sas-1, Xcoord(i)-sas:Xcoord(i)+BS+sas-1,:), BS, sas );
                    end
                end
            end
            
        end
        
        %Elapsed time
        set(handles.edit3, 'String', toc);

        %PSNR calculation

        mse=mean(mean(mean((double(img2)-double(IMG)).*(double(img2)-double(IMG)))));
        psnr=10*log10(256^2/mse);
        set(handles.edit2, 'Enable', 'on');
        set(handles.edit2, 'String', num2str(psnr));

        %display image

        imagesc(img2, 'Parent', handles.axes1);
        set(handles.axes1, 'ButtonDownFcn',temp);
        prop=get(handles.axes1); %atualizando o prop
        set(prop.Children, 'ButtonDownFcn',temp);
        %make log
        imwrite(img2, strcat('.\', get(handles.edit4, 'String'), '\result_', num2str(IMAGE_COUNT), '.png'), 'png');
        if(IMAGE_COUNT == 0)
            fid=fopen( strcat('.\', get(handles.edit4, 'String'), '\data.log'), 'w');
        else
            fid=fopen( strcat('.\', get(handles.edit4, 'String'), '\data.log'), 'a');
        end
        fprintf(fid, 'Image %d, method : %s - PSNR : %s, Execution time : %s \n', IMAGE_COUNT, method_str, get(handles.edit2, 'String'), get(handles.edit4, 'String'));
        fclose(fid);
        IMAGE_COUNT=IMAGE_COUNT+1;        

    end
end


function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function activex1_SelectionComplete(hObject, eventdata, handles)
% hObject    handle to activex1 (see GCBO)
% eventdata  structure with parameters passed to COM event listerner
% handles    structure with handles and user data (see GUIDATA)
global IMG;

text1=findobj(gcbf, 'Tag', 'edit1');
fileName=handles.activex1.FileName;
try
    IMG=imread(fileName);
    temp=get(handles.axes1, 'ButtonDownFcn');
    imagesc(IMG, 'Parent', handles.axes1);
    colormap('gray');
    prop=get(handles.axes1);
    set(handles.axes1, 'ButtonDownFcn',temp);
    set(prop.Children, 'ButtonDownFcn',temp);
catch
end



% --------------------------------------------------------------------
function Menu8x8_Callback(hObject, eventdata, handles)
% hObject    handle to Menu8x8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global BS

BS=8;

set(handles.Menu16x16, 'Checked','off');
set(handles.Menu8x8, 'Checked','on');

% --------------------------------------------------------------------
function Menu_Bloco_Callback(hObject, eventdata, handles)
% hObject    handle to Menu_Bloco (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Menu16x16_Callback(hObject, eventdata, handles)
% hObject    handle to Menu16x16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global BS

BS=16;
set(handles.Menu16x16, 'Checked','on');
set(handles.Menu8x8, 'Checked','off');



% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
global BS
global CONC_TYPE;
global IMAGE_COUNT;
CONC_TYPE='free';
BS=8;
IMAGE_COUNT=0;



% --------------------------------------------------------------------
function type_Callback(hObject, eventdata, handles)
% hObject    handle to type (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Free_click_Callback(hObject, eventdata, handles)
% hObject    handle to Free_click (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global CONC_TYPE;
CONC_TYPE='free';
set(handles.Free_click, 'Checked','on');
set(handles.Graded, 'Checked','off');
set(handles.Block_loss, 'Checked','off');

% --------------------------------------------------------------------
function Graded_Callback(hObject, eventdata, handles)
% hObject    handle to Graded (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global CONC_TYPE;
CONC_TYPE='graded';
set(handles.Free_click, 'Checked','off');
set(handles.Graded, 'Checked','on');
set(handles.Block_loss, 'Checked','off');


% --------------------------------------------------------------------
function Block_loss_Callback(hObject, eventdata, handles)
% hObject    handle to Block_loss (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global CONC_TYPE;
CONC_TYPE='block';
set(handles.Free_click, 'Checked','off');
set(handles.Graded, 'Checked','off');
set(handles.Block_loss, 'Checked','on');


function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function activex1_StateChanged(hObject, eventdata, handles)
% hObject    handle to activex1 (see GCBO)
% eventdata  structure with parameters passed to COM event listerner
% handles    structure with handles and user data (see GUIDATA)

% global IMG;
% 
button=findobj(gcbf, 'Tag', 'pushbutton1');
set(handles.pushbutton1, 'Enable', 'on');

Contact us