You are on page 1of 11

GUI EXAMPLE N1 :

function Multi_images() %Simple GUI example to show how to use NEXT and BACK buttons. %Images should be of same size and class. i=1;%initialize the pointer figure('units','pixels','position',[350 150 500 380],'menubar','none','numbertitle','off',... 'name','GUI EXAMPLE(1)','resize','off'); NX = uicontrol('style','push','units','pixels','position',[265 10 150 30],... 'fontsize',14,'string','NEXT','callback',{@nextButton}); BK = uicontrol('style','push','units','pixels', 'position',[105 10 150 30],... 'fontsize',14,'string','BACK','callback',{@backButton}); set(BK,'visible','off');

FN = {'cameraman.tif','rice.png','testpat1.png'}'; no = numel(FN);%Returns the number of elements. A = imread(FN{1}); imshow(A); %Allocation: size, No of images, image type(uint8,uint16,logical,...) images = zeros([size(A) no],class(A)); images(:,:,1) = A; %Create multiple images array. for inc = 2:no images(:,:,inc) = imread(FN{inc}); end %Pushbutton function for NEXT function nextButton(source,eventdata) if(i < = no) i=i+1; set(BK,'Visible','on'); set(NX,'Visible','on'); else set(NX,'Visible','off'); end if(i==no) set(NX,'Visible','off'); end imshow(images(:,:,i)); end %Pushbutton function for BACK function backButton(source,eventdata) if(i < = 1) i=i-1; set(NX,'Visible','on'); set(BK,'Visible','on'); else %Set the Back button invisible set(BK,'Visible','off'); end if(i==1) %Set the Back button invisible set(BK,'Visible','off'); end imshow(images(:,:,i)); end end

Programming Fundamentals in Biomedical Engineering - Course Project 2


This project builds a GUI interface that can change the color and contrast of an image. This includes how an image is represented in MATLAB and how color is changed in MATLAB.

GUI interface requirements: 1. Image area: Axes object. Resize it to hold a picture. 2. Load Image Button: Push button object. 3. Red, Green and Blue: Slide bar. Please adjust the following properties using property inspector. a. Max: 1.0 b. Min: 1.0 c. When you slide the bar, the value will change between 1.0 and 1.0. You can retrieve the value using get function. 4. Contrast: Slide bar. Please adjust the following properties using property inspector. a. Max: 0.0 b. Min: 2.0 c. When you slide the bar, the value will change between 0.0 and 2.0. You can retrieve the value using get function. 5. Exit button: Push button

Program requirements: 1. Use "Load Image" button to load a JPEG file to a variable 'pic' and display it in the axes area. a. Functions used: uigetfile, fullfile, imread 2. Use handles structure to store the image for future use. E.g: a. handles.pic = pic b. guidata(hObject,handles) 3. Use slider red, yellow and blue to adjust the three colors. In the corresponding call back function, a. Retrieve the slider value to variable scale. b. Retrieve the image data using handles.pic c. Adjust the color by adding the value scale*128. d. Show the modified image 4. Use slider contrast to adjust the contrast. In the call back function, a. Retrieve the slider value to variable scale. b. Retrieve the image data using handles.pic c. Adjust the three colors by multiplying the value scale. d. Show the modified image 5. Exit button: when the button is push, close the GUI interface. Use google search to find the solution.

Project report: 1. Description of the project. 2. Description of image representation using MATLAB, especially how colors are represented. 3. Description of the procedure to create GUI. This includes how to lay out objects and the relationship between object and corresponding call back function. 4. Narratives of you approach to the project, i.e. the call back functions.

Background knowledge: A digital image is stored in an M N 3 unit8 array of pixel color value. The image is also color mapped (or indexed). The resulting color map stores the RGB color value for each item. Brightness is a control that adds or subtracts an offset into RGB signals. In this project, we will use the gamma correction command under the imadjust function to carry out the brightness control. The gamma correction basically determines the luminance of an image. Contrast is a control that applies a scale factor to the RGB signals. In this project, we will use the stretchlim command, which

specifies lower and upper limits of pixel values that can be used for contrast stretching image, under the imadjust function to perform the contrast control. Procedures to create GUI:

GUI Developing Environment in MATLAB

Adjust the listed properties with the Property Inspector in the GUIDE (Graphical User Interface Developing Environment). GUI Properties Table Functional Elements Pic Axes Callback functions (Tags) Max/Min Settings Pic N/A N/A N/A Max = 1.0; Min = -1.0 Max = 1.0; Min = -1.0 blue brightness contrast Max = 1.0; Min = -1.0 Max = 5.0; Min = -1.0 Max = 0.99; Min = 0.0

Load Image Push Button load Exit Push Button R Slider G Slider B Slider Brightness Slider Contrast Slider exit red green

MATLAB generates codes to create the GUI. Here I present the manually input codes with comments (in green):

%%% "Load Image" Push Button. function load_Callback(hObject, eventdata, handles)

[Filename, Pathname] = uigetfile('*.*', 'Pick an image'); % Data input; fetches for the image file. I = fullfile(Pathname, Filename); % Appends the file path and the file name together. img = imread(I); % Reads the image and store it to a variable. image(img); % Displays the image. handles.Pic = img; % Creates the handle for the image for modifications. guidata(hObject, handles); % Update handles structure.

%%% "G" Slider. function green_Callback(hObject, eventdata, handles)

gr = get(hObject, 'Value'); % Returns the position of green slider. J = handles.Pic; % Gets the image data. sz = size(J); % Gets the size of the image. K = J; % Duplicates the image data for modifications.

row = 1; % initiates the while loop. while row <= sz(1) % Logical condition; only needs the first value of the size. K(row, :, 2) = J(row, :, 2) + 256 * gr; % Indexes the matrix and changes the color accordingly. row = row + 1; % Iterates through the image data. end

image(K); % Displays the image. guidata(hObject, handles); % Update handles structure.

%%% "R" Slider. function red_Callback(hObject, eventdata, handles)

re = get(hObject, 'Value'); J = handles.Pic; sz = size(J); K = J;

row = 1; while row <= sz(1) K(row, :, 1) = J(row, :, 1) + 256 * re; row = row + 1; end

image(K); guidata(hObject, handles);

%%% "B" Slider. function blue_Callback(hObject, eventdata, handles)

bl = get(hObject, 'Value'); J = handles.Pic; sz = size(J); K = J;

row = 1; while row <= sz(1) K(row, :, 3) = J(row, :, 3) + 256 * bl; row = row + 1; end

image(K); guidata(hObject, handles);

%%% "Brightness" Slider. function brightness_Callback(hObject, eventdata, handles)

Gamma = get(hObject, 'Value') + 1; % Returns the position of brightness slider. J = handles.Pic; K = imadjust(J, [], [], Gamma); % Applies gamma corrections to the image. image(K); guidata(hObject, handles);

%%% "Contrast" Slider. function contrast_Callback(hObject, eventdata, handles)

T = [get(hObject, 'Value'), 1]; % Returns the position of contrast slider. J = handles.Pic; K = imadjust(J, stretchlim(J, T), [0 1]); % Changes color saturation limits. image(K); guidata(hObject, handles);

%%% "Exit" Push Button. function exit_Callback(hObject, eventdata, handles)

close % Exits the figure window.

ETIQUETAR BOTONES DE UNA GUI

Para colocar una imagen en un botn de una interfaz grfica colocamos el siguiente cdigo en la parte de inicializacin del programa:
%Carga la imagen de fondo (opcional) [x,map]=imread('hammerfall.jpg','jpg'); image(x),colormap(map),axis off,hold on %Coloca una imagen en cada botn [a,map]=imread('vol.jpg'); [r,c,d]=size(a); x=ceil(r/30); y=ceil(c/30); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton1,'CData',g); [a,map]=imread('stop.jpg'); [r,c,d]=size(a); x=ceil(r/30); y=ceil(c/30); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton2,'CData',g); [a,map]=imread('play.jpg'); [r,c,d]=size(a); x=ceil(r/30); y=ceil(c/30); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton3,'CData',g); [a,map]=imread('open_files.jpg'); [r,c,d]=size(a); x=ceil(r/30); y=ceil(c/30); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton4,'CData',g); [a,map]=imread('cd_eject.jpg'); [r,c,d]=size(a); x=ceil(r/35);

y=ceil(c/35); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton5,'CData',g); [a,map]=imread('pause.jpg'); [r,c,d]=size(a); x=ceil(r/100); y=ceil(c/80); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton6,'CData',g); [a,map]=imread('mute2.jpg'); [r,c,d]=size(a); x=ceil(r/30); y=ceil(c/30); g=a(1:x:end,1:y:end,:); g(g==255)=5.5*255; set(handles.pushbutton7,'CData',g);

GUARDAR IMAGEN Y GRFICA DE UNA GUI

Este programa es un ejemplo de cmo grabar una imagen procesada o bien una grfica en un GUI.
Las condiciones iniciales del programa son: function save_imag_plot_OpeningFcn(hObject, eventdata, handles, varargin) movegui(hObject,'center') img=imread('colores.tif'); axes(handles.axes1) imshow(img) axis off handles.img=img; El cdigo del botn de procesamiento es: % --- FUNCIN DEL BOTN "CONVERTIR" function convertir_Callback(hObject, eventdata, handles) % Convertir imagen img=handles.img; gris=rgb2gray(img); axes(handles.axes2) imshow(gris) % Mostrar el histograma I axes(handles.axes3) imhist(gris); % Mostrar el histograma II axes(handles.axes4) [counts,x] =imhist(gris); stem(x,counts) El botn para guardar la imagen y la grfica tiene el siguiente cdigo: % --- FUNCIN DEL BOTN "GUARDAR" function guardar_Callback(hObject, eventdata, handles)

% Guardar imagen % Obtener imagen del axes rgb = getimage(handles.axes2); if isempty(rgb), return, end % Guardar archivo formatos = {'*.jpg','JPEG (*.jpg)';'*.tif','TIFF (*.tif)'}; [nomb,ruta] = uiputfile(formatos,'GUARDAR IMAGEN'); if nomb==0, return, end fName = fullfile(ruta,nomb); imwrite(rgb,fName); % -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ formatos = {'*.bmp','BMP (*.bmp)'}; [nomb,ruta] = uiputfile(formatos,'GUARDAR HISTOGRAMA'); if nomb==0, return, end % Crear nueva figura figura = figure; % Unidades y posicin unidades = get(handles.axes4,'Units'); posicion = get(handles.axes4,'Position'); objeto_2 = copyobj(handles.axes4,figura); % Modificar la nueva figura set(objeto_2,'Units',unidades); set(objeto_2,'Position',[15 5 posicion(3) posicion(4)]); % Ajustar la nueva figura set(figura,'Units',unidades); set(figura,'Position',[15 5 posicion(3)+30 posicion(4)+10]); % Guardar la grfica saveas(figura,[ruta nomb]) %Cerrar figura close(figura) Puedes descargar el programa de ejemplo presionando el botn descargar.
Re: How to show image of a wavelet transformed image?
Hi; In recent years i have do something like below; In this code i use dwt2 but as i know wavedec2 gives similiar output matrix. You can approximate form this example. I think it can help.
%wavelet decomposition %5 level decomposition using db1 (haar) %scale level 1 [LL1,HL1,LH1,HH1]=dwt2(n_im,'haar'); %LL approximation HL horizontal |a_LL |h_HL| %LH vertical HH diagonal |-----|----| % |v_LH |d_HH| %scale level 2 [LL2,HL2,LH2,HH2]=dwt2(LL1,'haar'); %scale level 3 [LL3,HL3,LH3,HH3]=dwt2(LL2,'haar'); %scale level 4 [LL4,HL4,LH4,HH4]=dwt2(LL3,'haar'); %scale level 5 [LL5,HL5,LH5,HH5]=dwt2(LL4,'haar'); %to see the whole wavelet decomposition images figure colormap(gray) imagesc([[[[[LL5 HL5;LH5 HH5] HL4;LH4 HH4] HL3;LH3 HH3] HL2;LH2 HH2] HL1;LH1 HH1])

Re: How can I set low-pass filter ( frequency ) for an image ?


thank's all of you I writed the Function This way where im is an image matrix this is a butterworth filter is it true? ///////////////////////////////////////////// butterworth filter has an integer parameter and I ignored the parameter (n=1) the parameter is kind of sharp rate for an image ///////////////////////////////////////////// function lowpassFilter(im, cutoff) im=rgb2gray(im); sizeIm=size(im); if cutoff < 0 | cutoff > 0.5 error('cutoff frequency must be between 0 and 0.5'); end rows = sizeIm(1); cols = sizeIm(2); % X and Y matrices with ranges normalised to +/- 0.5 x = (ones(rows,1) * [1:cols] - (fix(cols/2)+1))/cols; y = ([1:rows]' * ones(1,cols) - (fix(rows/2)+1))/rows; radius = sqrt(x.^2 + y.^2); % A matrix with every pixel = radius relative to centre. f = 1 ./ (1.0 + (radius ./ cutoff).^(2)); % The filter fim=fftshift(fft2(im)); result=f.*fim; imshow(ifft2(ifftshift(result))); end