Code covered by the BSD License  

Highlights from
Retrogressive "Classic Game" Image Processing

image thumbnail

Retrogressive "Classic Game" Image Processing

by

 

Ever wanted to turn your images into "classic game"-like images? This is it!

projectGUI(varargin)
%Daniel Page
%6855569
%Project:  Retrogressive "Classic Game" Image Processing

function varargout = projectGUI(varargin)
% projectGUI MATLAB code for projectGUI.fig
%      projectGUI, by itself, creates a new projectGUI or raises the existing
%      singleton*.
%
%      H = projectGUI returns the handle to a new projectGUI or the handle to
%      the existing singleton*.
%
%      projectGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in projectGUI.M with the given input arguments.
%
%      projectGUI('Property','Value',...) creates a new projectGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before projectGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to projectGUI_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 projectGUI

% Last Modified by GUIDE v2.5 14-Dec-2012 20:06:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @projectGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @projectGUI_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
global imageOrig;
imageOrig=zeros(256,224);
global imageProcessed;
imageProcessed=zeros(256,224);
global imOpenFlag;
imOpenFlag=0;
% --- Executes just before projectGUI is made visible.
function projectGUI_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 projectGUI (see VARARGIN)
global imOpenFlag;
imOpenFlag=0;
% Choose default command line output for projectGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes projectGUI wait for user response (see UIRESUME)
% uiwait(handles.figureProj);

function [imageOut,colourM] = processImage(hObject,eventdata,handles)
im1=handles.imageOrig;
%im1=1.5*im1;
val=0;
%check filters for transform! 
switch get(handles.radioDCTTransform,'Value')
     case 1,  val=1;
 end

 switch get(handles.radioHEQButton,'Value')
     case 1,  val=2;
 end
 switch get(handles.radioIAButton,'Value')
     case 1,  val=3;
 end
 
 %check palette choice!
 choicePal=0;
 switch get(handles.radioTurbo,'Value')
     case 1,  choicePal=5;
 end
 switch get(handles.radioNES,'Value')
     case 1,  choicePal=1;
 end
 switch get(handles.radioGameboy,'Value')
     case 1,  choicePal=2;
 end
 switch get(handles.radioSegaMS,'Value')
     case 1,  choicePal=3;
 end
 switch get(handles.radioSNES,'Value')
     case 1,  choicePal=4;
 end
 
[R C D]=size(im1);

if (get(findobj('Tag','checkResizeBox'),'Value') == get(findobj('Tag','checkResizeBox'),'Max'))
%resize image to 256x224 (if larger than these dimensions)
if(C>256 || R>224)
    if(R>C)
        ratioW=R/224.0;
        im1=imresize(im1,(1/ratioW));
    else
        ratioW=C/256.0;
        im1=imresize(im1,(1/ratioW));
    end
end
end
[R C D]=size(im1);

%RETROGRESSIVE TRANSFORM EFFECTS
if(val==1)
%DCT EFFECT
%Comment:  This filter provides a varying rainbow filter which can for
%large frequencies provide good results, or if the palette is small smaller
%or moderate values for the frequency high pass, can work as lighting.
imTemp=im1;
im1R=im1(:,:,1);
im1G=im1(:,:,2);
im1B=im1(:,:,3);

%perform DCT-2D to each colour channel
im1R=dct2(im1R);
im1G=dct2(im1G);
im1B=dct2(im1B);

LowPass= str2double(get(findobj('Tag','editCoefficient'),'string'));
%the low pass parameter from GUI to only let lower frequencies through
im1R(abs(im1R)>LowPass)=0;%red
im1G(abs(im1G)>LowPass)=0;%green
im1B(abs(im1B)>LowPass)=0;%blue

%convert back to original channels
im1R=idct2(im1R);
im1G=idct2(im1G);
im1B=idct2(im1B);
im1=cat(3,im1R,im1G,im1B);
im1=im2double(abs(im1>=0));

end


if(val==2)
%HISTOGRAM EQUALIZATION - This will perform histogram equalization, for
%certain images where equalization would be suitable can vastly improve the
%quality before mapping.
for i=1:3
    im1(:,:,i)=histeq(im1(:,:,i));
end

end

if(val==3)
%IMAGE INTENSITY ADJUSTMENT - Especially useful for low contrast images,
%this improves the contrast of the colours.
for i=1:3
   im1(:,:,i)=imadjust(im1(:,:,i));
end

end

%PRE-MAPPING Optional Filtering
%gamma adjustment
if (get(findobj('Tag','checkGammaBox'),'Value') == get(findobj('Tag','checkGammaBox'),'Max'))
    im1=im2double(im1);
    Gamma= str2double(get(findobj('Tag','editGamma'),'string'));
    im1=im1.^double(Gamma);
end
%bilateral smoothing
if (get(findobj('Tag','checkBilateralBox'),'Value') == get(findobj('Tag','checkBilateralBox'),'Max'))
    %bilateral smoothing
    im1=im2double(im1);
    imAvg=bfilter2(im1,1,1);
    im1=(imAvg+im1)./2;%apply averaging
end
%edge emphasize
if (get(findobj('Tag','checkEdgeBox'),'Value') == get(findobj('Tag','checkEdgeBox'),'Max'))
    %bilateral smoothing
    im1=im2double(im1);
    if(val==1)
        edgeBox=edge(rgb2gray(imTemp),'sobel');%we want edges from image before DCT effects
    else
        edgeBox=edge(rgb2gray(im1),'sobel');%Use sobel filter.
    end
    [m n]=size(edgeBox);
    edgeCol=zeros(m,n,3);
    edgeCol(:,:,1)=edgeBox;
    edgeCol(:,:,2)=edgeCol(:,:,1);
    edgeCol(:,:,3)=edgeCol(:,:,1);%making grayscale edges to RGB
    edgeBox=edgeCol;
    
    im1=im1-256*im2double(edgeBox);%subtract edge from original
end
    
imOrig=im1;

%PALETTE ASSIGNMENT FOR MAPPING
%TurboGrafx-16
if(choicePal==5)
num=512;
fid=fopen('palettes/Turbopalette.txt','r');
palette=zeros(512,3);
for i=1:num
    for j=1:3
        palette(i,j)=fscanf(fid,'%d',1)+1;
    end
end
fclose(fid);
[R C D] = size(im1);
sizePalette=size(palette);
palette=palette/256;
end

%NES
if(choicePal==1)
num=55;
fid=fopen('palettes/NESpalette.txt','r');
palette=zeros(55,3);
for i=1:num
    for j=1:3
        palette(i,j)=fscanf(fid,'%d',1)+1;
    end
end
fclose(fid);
[R C D] = size(im1);
sizePalette=size(palette);
palette=palette/256;
end

%Nintendo Gameboy
if(choicePal==2)
num=4;
fid=fopen('palettes/Gameboypalette.txt','r');
palette=zeros(4,3);
for i=1:num
    for j=1:3
        palette(i,j)=fscanf(fid,'%d',1)+1;
    end
end
fclose(fid);
[R C D] = size(im1);
sizePalette=size(palette);
palette=palette/256;
end

%Sega Master System
if(choicePal==3)
num=64;
fid=fopen('palettes/SMSpalette.txt','r');
palette=zeros(64,3);
for i=1:num
    for j=1:3
        palette(i,j)=fscanf(fid,'%d',1)+1;
    end
end
fclose(fid);
[R C D] = size(im1);
sizePalette=size(palette);
palette=palette/256;
end

%SNES- super nintendo entertainment system
if(choicePal==4)
num=32768;
fid=fopen('palettes/SNESpalette.txt','r');
palette=zeros(32768,3);
for i=1:num
    for j=1:3
        palette(i,j)=fscanf(fid,'%d',1)+1;
    end
end
fclose(fid);
[R C D] = size(im1);
sizePalette=size(palette);
palette=palette/256;
end

%convert to indexed image
[im1,map] = rgb2ind(imOrig,palette,'nodither');


%make even dimensions
if(mod(R,2) ~=0)
    im1=im1(1:(R-1),1:C);
end
[R C D]=size(im1);
if(mod(C,2) ~=0)
    im1=im1(1:R,1:(C-1));
end

%output our transforms and the colour map.
imageOut=im1;
colourM=palette;


% --- Outputs from this function are returned to the command line.
function varargout = projectGUI_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)


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


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


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


% --------------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles)
% hObject    handle to Open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Browse for the image file.
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
global imOpenFlag;
global imageOrig;
% Create the full file name.
fullImageFileName = fullfile(folder, baseFileName);
im_original=imread(fullImageFileName );
imOpenFlag=1;
handles.imOpenFlag=imOpenFlag;
imageOrig=im_original;
handles.imageOrig=imageOrig;
axes(handles.axesBefore);
image(im_original);
axis equal;
axis off;
axes(handles.axesAfter);
matFill=zeros(256,224);
matFill(:,:)=256;
image(matFill);
axis equal;
axis off;

guidata(hObject, handles); 
% --------------------------------------------------------------------
function Save_Callback(hObject, eventdata, handles)
% hObject    handle to Save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path] = uiputfile('*.png','Save Processed Image As...');
if isequal(file,0) || isequal(path,0)
   disp('Cancelled Save Operation')
else
    RGBImage = ind2rgb(handles.afterImage,handles.colourM);
    imwrite(RGBImage,[path,file]);
    %imwrite(handles.afterImage,handles.colourM,[path,file])
end

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

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


% --------------------------------------------------------------------
function Load_Callback(hObject, eventdata, handles)
% hObject    handle to Load (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 buttonProcess.
function buttonProcess_Callback(hObject, eventdata, handles)
% hObject    handle to buttonProcess (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%global imOpenFlag;
%if isstruct(imOpenFlag)
%if(handles.imOpenFlag==1)
%    disp('I can use it');
%else
%    disp('I cannot use any image');
%end
%else
%    disp('Please open a file');
%end
axes(handles.axesAfter);
[afterImage map] = processImage(hObject,eventdata,handles);
RGBImageOut= ind2rgb(afterImage,map);
%handles.afterImage=afterImage;
%colormap(map);
%image(afterImage);
image(RGBImageOut);
axis equal;
axis off;
handles.afterImage=afterImage;
handles.colourM=map;
handles.RGBImageOut= RGBImageOut;
guidata(hObject, handles);

% --- Executes on button press in buttonLoad.
function buttonLoad_Callback(hObject, eventdata, handles)
% hObject    handle to buttonLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Browse for the image file.
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
global imOpenFlag;
global imageOrig;
% Create the full file name.
fullImageFileName = fullfile(folder, baseFileName);
im_original=imread(fullImageFileName );
imOpenFlag=1;
handles.imOpenFlag=imOpenFlag;
imageOrig=im_original;
handles.imageOrig=imageOrig;
axes(handles.axesBefore);
image(im_original);
axis equal;
axis off;
axes(handles.axesAfter);
matFill=zeros(256,224);
matFill(:,:)=256;
image(matFill);
axis equal;
axis off;

guidata(hObject, handles); 

% --- Executes on button press in buttonSave.
function buttonSave_Callback(hObject, eventdata, handles)
% hObject    handle to buttonSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path] = uiputfile('*.png','Save Processed Image As...');
if isequal(file,0) || isequal(path,0)
   disp('Cancelled Save Operation')
else
    RGBImage = ind2rgb(handles.afterImage,handles.colourM);
    imwrite(RGBImage,[path,file]);
    %imwrite(handles.afterImage,handles.colourM,[path,file])
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over buttonLoad.
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse press over axes background.
function axesBefore_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axesBefore (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
figure,
imshow(handles.imageOrig);


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


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

figure,
imshow(handles.afterImage,handles.colourM);


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

% Hint: get(hObject,'Value') returns toggle state of checkBilateralBox



function editCoefficient_Callback(hObject, eventdata, handles)
% hObject    handle to editCoefficient (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 editCoefficient as text
%        str2double(get(hObject,'String')) returns contents of editCoefficient as a double


% --- Executes during object creation, after setting all properties.
function editCoefficient_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editCoefficient (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 checkGammaBox.
function checkGammaBox_Callback(hObject, eventdata, handles)
% hObject    handle to checkGammaBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkGammaBox



function editGamma_Callback(hObject, eventdata, handles)
% hObject    handle to editGamma (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 editGamma as text
%        str2double(get(hObject,'String')) returns contents of editGamma as a double


% --- Executes during object creation, after setting all properties.
function editGamma_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editGamma (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 checkResizeBox.
function checkResizeBox_Callback(hObject, eventdata, handles)
% hObject    handle to checkResizeBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkResizeBox


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

% Hint: get(hObject,'Value') returns toggle state of checkEdgeBox

Contact us