No BSD License  

Highlights from
coloriz.m

from coloriz.m by Marcel Dubois de Cadouin
A slideshow where you can see all the colors a picture (grayscale or truecolor) may have.

coloriz(varargin)
function varargout = coloriz(varargin)
% COLORIZ M-file for coloriz.fig
%The goal of this application is to change the colors of a picture, 
%(truecolor or grayscale, *.jpg, *.jpeg), without moving the mouse, 
%looking at them in a slideshow.
%1)Choose a picture with 'Load Image'.
%2)You can set the number of slides with the left slider.
%3)Start colorizing with 'Go'.
%4)You can adjust the display time with the right slider.
%5)You can save/no save the current slide with 'Save Image'.
%6)You can save/no save systematically each slide with 'Save Each'.
%7)Save directory=> 'c:\ImagesColoriz'. You can change in SauveDir.
%8)'Vue' inform you on the progression.
%9)Click 'Close' to exit.
%
%
%WARNING:Don't go too fast if you want to save the pictures,
%time to save is dependant of the power your computer, you can adjust it
%in handles.TempoSauv
%NEW:Compact code, Add comments and TooltipStrings.  22 septembre 2007
%NEW:Gui improvement. 18 octobre 2007
%
%        Marcel Dubois de Cadouin,        dubois.ml@club-internet.fr
% 

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @coloriz_OpeningFcn, ...
                   'gui_OutputFcn',  @coloriz_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

%--------------------------------------------------------------------------
% --- Executes just before coloriz is made visible.
function coloriz_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 coloriz (see VARARGIN)

global SaveEach  SaveOne;
SauveDir='c:\ImagesColoriz\'; %Choose your pictures backup directory
handles.SauveDir=SauveDir; 
SaveEach=0; SaveOne=0;
handles.Lb=.08;
handles.Hb=.07;

InitPos(handles);

set(handles.slider1_nbrevues,'Enable', 'on');
handles.NbrVues=round(get(handles.slider1_nbrevues,'Value'));
x=num2str(handles.NbrVues);
set(handles.edit4_N,'String',x);
handles.Sauve=0;

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes coloriz wait for user response (see UIRESUME)
% uiwait(handles.Toile);

%--------------------------------------------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = coloriz_OutputFcn(hObject, eventdata, handles) 

% hObject    handle to figure
% Get default command line output from handles structure

varargout{1} = handles.output;

%--------------------------------------------------------------------------
function pushbutton1_help_Callback(hObject, eventdata, handles)

helpdlg({...
    'The goal of this application is to change the colors', ...
    'of a picture, (truecolor or grayscale,  *.jpg, *.jpeg)',...
    'without moving the mouse,looking at them in a slideshow.', ...
    '',...
    '1)Choose a picture with ''Load Image''',...
    '2)You can set the number of slides with the left slider'...
    '3)Start colorizing with ''Go''',...
    '4)You can adjust the display time with the right slider ',...
    '5)You can save/no save the current slide with ''Save Image''',...
    '6)You can save/no save systematically each slide with ''Save Each''',...
    '7)Save directory=> c:\ImagesColoriz. You can change in SauveDir',...
    '8)''Vue'' inform you on the progression',...
    '9)Click ''Close'' to exit',...
    'WARNING:Don''t go too fast if you want to save the pictures.',...
    '','',...
    'Marcel Dubois de Cadouin',...
    'dubois.ml@club-internet.fr'},'Help Coloriz');
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
function pushbutton2_load_Callback(hObject, eventdata, handles)
%Load a picture *.jpg, *.jpeg, grayscale or truecolor

TailleEcran=get(0,'ScreenSize');
RatioEcran=TailleEcran(4)/TailleEcran(3);
set(handles.Toile, 'Resize', 'On');
set(handles.Toile,...
    'Name', 'TOILE   (Reduce the windows if resolution seems too low)');
set(handles.edit7_num, 'String','0');
set(handles.pushbutton5_saveimage, 'Enable', 'Off');
set(handles.pushbutton6_saveeach, 'Enable', 'Off');

[FileName, PathName] = uigetfile( ...
{'*.jpg;*.jpeg','Pictures (*.jpg,*.jpeg)';
   '*.jpg',  'Pictures (*.jpg)'; ...
   '*.jpeg','Pictures (*.jpeg)'},...
   'Select image',...
   'c:\');
if isequal ([FileName,PathName],[0,0])
return
end


Adresse=strcat(PathName,FileName);
handles.DossierImages=PathName;
handles.NomImage=FileName;
set(handles.edit13_nomimage, 'String', FileName);
handles.Vue=imread(Adresse);
handles.NouvVue=handles.Vue;
info=imfinfo(Adresse);
handles.TempoSauv=abs(ceil(log10(info.FileSize/1000))-.5);
RatioImage=info.Height/info.Width;
%Screen max for the picture
if info.Height >= info.Width 
    HImage=1;
    LImage=RatioEcran/RatioImage; 
    if LImage > 1
        d=1/LImage;
        HImage=HImage*d; LImage=LImage*d;
    end
else
    LImage=1; 
    HImage=RatioImage/(RatioEcran); 
    if HImage > 1
        d=1/HImage;
        HImage=HImage*d; LImage=LImage*d;
    end
    xToile=1-(handles.Lb*1.1);
    if LImage > xToile
        LImage=LImage*xToile; HImage=HImage*xToile;
    end
end
    
if strcmp(info.ColorType, 'grayscale')
%Make a virtual RGB
  handles.NouvVue=...
      cat(3,handles.Vue,handles.Vue,handles.Vue);
end

set(handles.axes1,...
'Visible', 'off',...
'Units', 'normalized',...
'Position', [0 0 LImage HImage],...
'XLimMode', 'manual',...
'YLimMode', 'manual',...
'xlim', [1 info.Width],...
'ylim', [1 info.Height]);

image(handles.NouvVue);

%Make a basic virtual RGB
if strcmp(info.ColorType, 'grayscale')
    Rgs=handles.Vue;
    Ggs=255-handles.Vue;
    Bgs=round(mod(128+handles.Vue,255));
  handles.Vue=...
      cat(3,Rgs,Ggs,Bgs);
end

set(handles.pushbutton3_go,'Enable','On');
guidata(hObject, handles);
%-------------------------Fin Choix Image ---------------------------------

%--------------------------------------------------------------------------
function pushbutton3_go_Callback(hObject, eventdata, handles)
%Run the slideshow

global SaveEach  SaveOne;
SaveEach=0;   SaveOne=0;
rand('state',sum(100*clock));
set(handles.pushbutton3_go,'BackgroundColor',[1, 0, 0]);
set(handles.pushbutton3_go,'Enable','Off');
set(handles.pushbutton2_load,'Enable','Off');
set(handles.slider1_nbrevues,'Enable', 'Off');
set(handles.edit7_num, 'String','0');
DimCM=256;   Plage=round(DimCM/10);
Ecartm=15;  EcartM=70;       Acrm=50; AcrM=120; 
Fenetrm=1; FenetrM=DimCM; FenetrMP=FenetrM+Plage;
CoefLiss=[1 2 1]; %size odd , can change
SizeLiss=size(CoefLiss,2);
LongLiss=floor(SizeLiss/2);
x=cumsum(CoefLiss(:));
SumCoef=x(SizeLiss,1);
Gabarit(1,1:DimCM)=0:255;
RougeV=Gabarit; VertV=Gabarit;  BleuV=Gabarit;    
GabaritV=Gabarit; 
Vue=double(handles.Vue);
pause(.001);

for k=1:handles.NbrVues   
      kn=num2str(k);
      
      [handles,RougeV,VertV,BleuV]=...
         CalculVue(handles,...
           Vue,RougeV,VertV,BleuV,...
           DimCM,Ecartm,EcartM,Acrm,AcrM,...
           CoefLiss,LongLiss,SumCoef,Fenetrm,FenetrM,FenetrMP);
set(handles.pushbutton5_saveimage, 'Enable', 'On');
set(handles.pushbutton6_saveeach, 'Enable', 'On');
pause(.001);
     image(handles.NouvVue); 
     set(handles.edit7_num, 'String',kn);
     Dtl=ceil(get(handles.slider2_pause, 'Value')*100)/100;
     t=timer('StartDelay',Dtl,'TimerFcn','Nulle=[];');
     start(t); 
     wait(t);   %Time to look at picture
     delete(t);
     if (SaveOne==1 || SaveEach==1)
         Sauvegarde(handles);
     end   
     
set(handles.pushbutton5_saveimage, 'Enable', 'Off');
set(handles.pushbutton6_saveeach, 'Enable', 'Off');
pause(.001);
end %k

set(handles.slider1_nbrevues,'Enable', 'On');
set(handles.pushbutton6_saveeach,'BackgroundColor',[1, 1, 0]);
set(handles.pushbutton3_go,'BackgroundColor',[0, 1, 0]);
set(handles.pushbutton3_go,'Enable','On');
set(handles.pushbutton2_load,'Enable','On');
guidata(hObject, handles);
%---------------------------------- Fin Go --------------------------------

%--------------------------------------------------------------------------
function[handles,RougeV,VertV,BleuV]=...
 CalculVue(handles,...
           Vue,RougeV,VertV,BleuV,...
           DimCM,Ecartm,EcartM,Acrm,AcrM,...
           CoefLiss,LongLiss,SumCoef,Fenetrm,FenetrM,FenetrMP)
%Apply three new R,V,B filters to the original picture
   
     [RougeV]=Filtre(...
   RougeV,DimCM,Fenetrm,FenetrM,Ecartm,EcartM,Acrm,AcrM,FenetrMP);   
     [VertV]=Filtre(...
   VertV,DimCM,Fenetrm,FenetrM,Ecartm,EcartM,Acrm,AcrM,FenetrMP);
     [BleuV]=Filtre(...
   BleuV,DimCM,Fenetrm,FenetrM,Ecartm,EcartM,Acrm,AcrM,FenetrMP);

% g=plot(RougeV);  %Show one filter
% NumImage=get(handles.edit7_num, 'String');
% Chemin=strcat(g,NumImage,'.jpg');
% Dt=handles.TempoSauv;  %Time to convert and write the new picture
%   t=timer('StartDelay',Dt,'TimerFcn','Nulle=[];');
%   start(t);
%   saveas(g,NumImage,'jpg');
%   wait(t);
%   delete(t)

  
  
     [RougeV]=Liss(...
   RougeV,CoefLiss,LongLiss,SumCoef,Fenetrm,FenetrM,DimCM);
     [VertV]=Liss(...
   VertV,CoefLiss,LongLiss,SumCoef,Fenetrm,FenetrM,DimCM);
     [BleuV]=Liss(...
   BleuV,CoefLiss,LongLiss,SumCoef,Fenetrm,FenetrM,DimCM);

% g=plot(RougeV);  %Show one filter
% NumImage=get(handles.edit7_num, 'String'); NumImage=strcat(NumImage,'Lis');
% Chemin=strcat(g,NumImage,'.jpg');
% Dt=handles.TempoSauv;  %Time to convert and write the new picture
%   t=timer('StartDelay',Dt,'TimerFcn','Nulle=[];');
%   start(t);
%   saveas(g,NumImage,'jpg');
%   wait(t);
%   delete(t);

    
I=size(Vue,1); J=size(Vue,2);  
NouvVueR=zeros(I,J);  NouvVueV=zeros(I,J);   NouvVueB=zeros(I,J);
 for i=1:I    
        NouvVueR(i,:,1)=RougeV(1,1+Vue(i,:,1)); % Vue 0  255       
 end
 for i=1:I
        NouvVueV(i,:,1)=VertV(1,1+Vue(i,:,2)); % Vue 0  255       
 end
 for i=1:I    
        NouvVueB(i,:,1)=BleuV(1,1+Vue(i,:,3)); % Vue 0  255       
 end
NouvVue=uint8(cat(3,NouvVueR,NouvVueV,NouvVueB));
handles.NouvVue= NouvVue;
guidata(gcf, handles);

%------------------------------ Fin Calcul --------------------------------

%--------------------------------------------------------------------------
function[GabaritN]=Filtre(GabaritV,DimCM,Fenetrm,FenetrM,...
    Ecartm,EcartM,Acrm,AcrM,FenetrMP)
%Change a part of the filter (segment between two points)

 GabaritN=GabaritV; 
 %Modify a little part of the filter
 %Choose two points, not too far, not too much near!
 Rx=zeros(1,2);  Ry=zeros(1,2);
 ax=fix(Fenetrm+(FenetrMP-Fenetrm+1)*rand);
        Decal=2*DimCM;
        while Decal<Ecartm || Decal>EcartM
            bx=fix(Fenetrm+(FenetrMP-Fenetrm+1)*rand);
            Decal=round(abs(bx-ax));
        end

         Rx(1,1)=ax; Rx(1,2)=bx;
         Rx=sort(Rx); 
         iRx1=round(1+mod(DimCM-Rx(1,1),DimCM));  Ry(1,1)=GabaritV(1,iRx1);
         iRx2=round(1+mod(DimCM-Rx(1,2),DimCM));  Ry(1,2)=GabaritV(1,iRx2);
       
        t=ceil(DimCM*rand-1);     
        Val=Ry(1,2);
        z=abs(Val-t);
        while z<Acrm || z>AcrM
           t=ceil(DimCM*rand-1);
           z=abs(Val-t);
        end        
        GabaritN(1,iRx2)=t;
        Ry(1,2)=t;
                
        t=ceil(DimCM*rand-1);     
        Val=Ry(1,1);
        z=abs(Val-t);
        while z<Acrm || z>AcrM
           t=ceil(DimCM*rand-1);
           z=abs(Val-t);
        end        
        GabaritN(1,iRx1)=t;
        Ry(1,1)=t;
        
        %Linear interpolation 
        p1=(Ry(1,2)-Ry(1,1))/(Rx(1,2)-Rx(1,1));
        i=Rx(1,1); 
        E=Ry(1,1);
        while i<Rx(1,2)   
            i=i+1;
            ix=round(1+mod(DimCM-i,DimCM));
            E=E+p1;
            GabaritN(1,ix)=round((E));
            %GabaritN(1,ix)=round((E+GabaritV(1,ix))/2);%other approach
        end
%----------------------------- Fin Filtre ---------------------------------


%--------------------------------------------------------------------------        
function[GabaritN]=Liss(GabaritV,CoefLiss,LongLiss,...
    SumCoef,Fenetrm,FenetrM,DimCM)
%Smouth the filter

GabaritN=GabaritV;

for i=Fenetrm:FenetrM
    L=double(0);
    k=1;
    for j=-LongLiss:LongLiss
        ix=round(1+mod(DimCM-(i+j),DimCM)); %cadre et inverse
        L=L+CoefLiss(1,k)*GabaritV(1,ix);  
        k=k+1;
    end
    GabaritN(1,i)=floor(L/SumCoef);
end
%------------------------------- Fin Liss ---------------------------------

%--------------------------------------------------------------------------
function pushbutton4_close_Callback(hObject, eventdata, handles)
%Close and Exit

delete(handles.Toile);

%--------------------------------------------------------------------------
function pushbutton5_saveimage_Callback(hObject, eventdata, handles)
%Save the current picture
%Request for save: Red/Yes, Orange/No

global SaveOne;
if SaveOne==0;
    SaveOne=1;
set(hObject,'BackgroundColor',[1, 0, 0]);
set(hObject,'String','Save Image');
else
    SaveOne=0;
set(hObject,'BackgroundColor',[1, 0.5, 0.25]);
set(hObject,'String','Save Image');
end
guidata(hObject, handles);
%--------------------------------------------------------------------------

function pushbutton6_saveeach_Callback(hObject, eventdata, handles)
%Save each picture when you want
%Request for save: Red/Yes, Yellow/No

global SaveEach;
if SaveEach == 0 %Save
    SaveEach=1;
    set(hObject,'BackgroundColor',[1, 0, 0]);
    set(hObject,'String','Save Each');
else         %No save
    SaveEach=0;
    set(hObject,'BackgroundColor',[1, 1, 0]);
    set(hObject,'String','Save Each');
end
guidata(hObject, handles);

%--------------------------------------------------------------------------
function Sauvegarde(handles)
%Save picture

global SaveOne SaveEach;

 if SaveOne==1
  set(handles.pushbutton5_saveimage,'String','Save Runing');
 end
 if SaveEach==1
  set(handles.pushbutton6_saveeach,'String','Save Runing');
 end
  
hr=clock; %To distinguish the pictures
Ident=['(',num2str(hr(1)),'_',num2str(hr(2)),'_',num2str(hr(3)),'_',...
    num2str(hr(4)),'_',num2str(hr(5)),'_',num2str(hr(6)),')'];

NumImage=get(handles.edit7_num, 'String');
Chemin=strcat(handles.SauveDir,NumImage,'_',Ident,handles.NomImage);
Dt=handles.TempoSauv; %Estimate time to convert and write the new picture
  t=timer('StartDelay',Dt,'TimerFcn','Nulle=[];');
  start(t);
  imwrite(handles.NouvVue,Chemin,'jpg','Quality',100);
  wait(t);
  delete(t);

  if SaveOne==1
      SaveOne=0;
  set(handles.pushbutton5_saveimage,'BackgroundColor',[1, 0.5, 0.25]);
  set(handles.pushbutton5_saveimage,'String','Save Image');
  end
  if SaveEach==1
  set(handles.pushbutton6_saveeach,'String','Save Each');
  end
%--------------------------------------------------------------------------
function slider1_nbrevues_Callback(hObject, eventdata, handles)
%Number of slides you want to see

handles.NbrVues=round(get(hObject,'Value'));
x=num2str(handles.NbrVues);
set(handles.edit4_N,'String',x);
guidata(hObject, handles);


function slider1_nbrevues_CreateFcn(hObject, eventdata, handles)

usewhitebg = 1;
if usewhitebg
  set(hObject,'BackgroundColor',[.9 .9 .9]);
else
  set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%--------------------------------------------------------------------------
function slider2_pause_Callback(hObject, eventdata, handles)
%The time you have to look at one slide


function slider2_pause_CreateFcn(hObject, eventdata, handles)

usewhitebg = 1;
if usewhitebg
   set(hObject,'BackgroundColor',[.9 .9 .9]);
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%--------------------------------------------------------------------------
function edit4_N_Callback(hObject, eventdata, handles)
%Number of slides to show

function edit4_N_CreateFcn(hObject, eventdata, handles)

if ispc
 set(hObject,'BackgroundColor','white');
else
 set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%--------------------------------------------------------------------------
function edit7_num_Callback(hObject, eventdata, handles)
%Numero of the current slide

function edit7_num_CreateFcn(hObject, eventdata, handles)

if ispc
   set(hObject,'BackgroundColor','white');
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

%--------------------------------------------------------------------------
function InitPos(handles)
%Aspect of the GUI

Hb=handles.Hb;  Lb=handles.Lb;
    set(handles.Toile, 'position', [0 0 1 1]);
    set(handles.axes1, 'position', [0 0 1-Lb 1]);
    set(handles.pushbutton1_help, 'position', [1-Lb 1-Hb Lb Hb]);
    set(handles.edit13_nomimage, 'position', [1-Lb 1-1.5*Hb Lb Hb/2]);
    set(handles.pushbutton2_load, 'position', [1-Lb 1-2.5*Hb Lb Hb]);
    set(handles.pushbutton3_go, 'position', [1-Lb 1-3.5*Hb Lb Hb]);
    set(handles.pushbutton4_close, 'position', [1-Lb 1-4.5*Hb Lb Hb]);
    set(handles.pushbutton5_saveimage, 'position', [1-Lb 1-7.5*Hb Lb Hb]);
    set(handles.pushbutton6_saveeach, 'position', [1-Lb 1-8.5*Hb Lb Hb]);
    set(handles.slider1_nbrevues, 'position', [1-7*Lb/8 1-13*Hb Lb/4 2.5*Hb]);
    set(handles.slider2_pause, 'position', [1-3*Lb/8 1-13*Hb Lb/4 2.5*Hb]);
    set(handles.text1_plus, 'position', [1-5*Lb/8 1-11*Hb Lb/4 Hb/2]);
    set(handles.text2_moins, 'position', [1-5*Lb/8 1-13*Hb Lb/4 Hb/2]); 
    set(handles.text4_totalvues, 'position', [1-Lb 1-10.25*Hb Lb/2 Hb/2]);
    set(handles.edit4_N, 'position', [1-Lb 1-10.5*Hb Lb/2 Hb/3]);
    set(handles.text5_pause, 'position', [1-Lb/2 1-10.5*Hb Lb/2 Hb/3]);  
    set(handles.text3_vue, 'position',     [1-3*Lb/4 1-5.7*Hb Lb/2 Hb/3]);
    set(handles.edit7_num, 'position', [1-3*Lb/4 1-6*Hb   Lb/2 Hb/3]);
   
%--------------------------------------------------------------------------
function edit13_nomimage_Callback(hObject, eventdata, handles)
%Name of the original picture

function edit13_nomimage_CreateFcn(hObject, eventdata, handles)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
%--------------------------------------------------------------------------

function text3_vue_CreateFcn(hObject, eventdata, handles)
%'Vue:'
%--------------------------------------------------------------------------

Contact us at files@mathworks.com