Matlab

Matlab GUI for Image Processing, explained with examples

MATLAB GUI for image processing:

Matlab GUI for Image Processing– In this tutorial, we are going to discuss about image processing GUI using MATLAB. In order to make the GUI for the image processing, we will first write guide in the MATLAB command window.

Matlab gui for image processing

When we type command guide it will open a design layout we can adjust will the size of the layer. There are certain tools which will be helpful for designing a GUI.

Matlab gui for image processing


Insert button:

So first we will click on button group which will perform certain functionality.

Matlab gui for image processing

Now we have to insert buttons, so click on the button icon that is nothing but push buttons. You can just click and drop it wherever you want, so you can drop number of push buttons and you can place it wherever you want.

Matlab gui for image processing

You can increase or decrease the size of the button and to do the changes you just have to double click on those buttons there will be number of options to make the required changes.

Matlab gui for image processing



Now, in order to change the background color we will select the background color and we will keep as red.  I will be highlighting on the required changes you can go through all the characteristics of inspector. So foreground color is nothing but those text color so yellow are white you can view. Similarly, the important is string which is nothing but the written text about the button it will be helpful for user to tell what functionality it will do so here we will use upload picture.

Matlab gui for image processing

Similarly, when you change string you don’t forget to change the tag which is nothing but it is the function name. If we keep it as push button the same function name will be there in code but we for better readability we have to change and you have to follow the rules of identifiers that is you can’t initiate a tag name using a number or spaces are not allowed and underscores are allowed. So required changes are done and you make sure that visibility is on if this is not on in the output window. You would not see that button it will be hidden.

Matlab gui for image processing


The same changes operations you can perform on push buttons change the background color and if you want extra button so you can keep place it here and to change the pattern group background you just have to double click on that change the background color then title you can change instead of button group we will give simple operation group so the name has changed.

Matlab gui for image processing

Similarly, we will rename the other two push button as add noise and remove the noise.

Matlab gui for image processing

Then we will add three axes one axis will be used to display the upload picture. The second axis will be used to display the noise image and the third axes will be used to display the image when the noise is removed.

Matlab gui for image processing

Now save the project by clicking the save button and after saving the file a code will appear

Matlab gui for image processing


Now we will write the code:

Upload picture:

First of all we will write the command for the upload picture button.

function uploadpicture_Callback(hObject, eventdata, handles)
% hObject    handle to uploadpicture (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.*', 'Pick a MATLAB code file');
if isequal(filename,0) || isequal(pathname,0)
       disp('User pressed cancel')
else
% this command will be used to concate both the file name and path name.
 filename=strcat(pathname,filename);
% now to read the image we will use the command imread and save it in the variable “a”.
a=imread(filename);
Now in order to show it in the first axes we will write the command:
      axes(handles.axes1);
      imshow(a);
Now in order to use this image in other function also we will write the command:
      handles.a = a;

% Update handles structure
      guidata(hObject, handles);

Adding Noise to the image:

Now in order to add noise to the image in our case we are using salt and pepper noise whose values can be adjust according to the requirement and this image will be saved in the axes 2 for which we will write the command:

j=handles.a;
        j = imnoise(j,'salt & pepper', 0.4);
      axes(handles.axes2);
      imshow(j);
       handles.j = j;

% Update handles structure
      guidata(hObject, handles);


Removing noise:

Now in order to remove the noise from the image we will use the median filter. First of all we will save the image in the variable “j” then we will apply median filter to the noisy image whose size can be adjusted according to our requirement.

j=handles.j;
p=medfilt3(j,[5,5,3]);
      axes(handles.axes3);
      imshow(p);

Matlab GUI for Image Processing Complete code:

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

% Last Modified by GUIDE v2.5 04-Aug-2021 23:23:17

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @image_processing_OpeningFcn, ...
'gui_OutputFcn',  @image_processing_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 image_processing is made visible.
function image_processing_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 image_processing (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = image_processing_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 uploadpicture.
function uploadpicture_Callback(hObject, eventdata, handles)
% hObject    handle to uploadpicture (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile('*.*', 'Pick a MATLAB code file');
if isequal(filename,0) || isequal(pathname,0)
       disp('User pressed cancel')
else
      filename=strcat(pathname,filename);
      a=imread(filename);
      axes(handles.axes1);
      imshow(a);
      handles.a = a;

% Update handles structure
      guidata(hObject, handles);

end


% --- Executes on button press in addnoise.
function addnoise_Callback(hObject, eventdata, handles)
% hObject    handle to addnoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
j=handles.a;
        j = imnoise(j,'salt & pepper', 0.4);
      axes(handles.axes2);
      imshow(j);
       handles.j = j;

% Update handles structure
      guidata(hObject, handles);




% --- Executes on button press in removenoise.
function removenoise_Callback(hObject, eventdata, handles)
% hObject    handle to removenoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
j=handles.j;
p=medfilt3(j,[5,5,3]);
      axes(handles.axes3);
      imshow(p);



Working of the project:

Now in order to run the project we will click on the run button and then click on the upload picture in order to upload picture.

Matlab gui for image processing

The image will be displayed in the first axis

Matlab gui for image processing


Then click on the add noise button the noise will be added to the picture which will be displayed in the second axis

Matlab gui for image processing

Then we will click on the remove noise button in order to remove the noise from the image and will be shown in the third axis.

Matlab gui for image processing

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button