The paper presents Matlab code for three questions. The first question tackles image processing mechanisms. It touches on average, Gaussian and medial filtering techniques. The second question presents a detailed user interface program from which users can open and execute files. The file choice, in this case, is audio. The third question is open-ended and presents code for image sharpening. Matlab contains inbuilt functions which are used to produce more detailed on images, otherwise known as sharpening.
Image Filtering Mechanisms
Image filtering is a technique for altering the size, shape, color, depth, smoothness, and other aspects of pictures. Essentially, it modifies the pixels of an image to turn it into the desired shape by utilizing many forms of graphical editing procedures using graphic design and editing software [1]. There are primarily two sorts of filters, and depending on the topic, there are other filtering strategies. Image filters are commonly used to reduce noise and improve image quality. Matlab comes with several functions for image filtering, which take several parameters. In the program below, the image is first converted into a single channel from which different averages are calculated: average, gaussian and medial filters. The results obtained are then filtered and displayed for comparison with the original image.
Table 1. Image filtering code snippet.
clear all; close all; clc;
% read image file to be processed image_input = imread(test1.jpg);
% Convert input image to single channel image_input = rgb2gray(image_input);
% calculate the average filter value for the image avarage_filter_value = fspecial(average, [5 5]);
% Calculate the gausian filter value gausian_filter_value = fspecial(gaussian, [13 13]);
% Process image using the average filter value B_filter_average_value = imfilter(A, avarage_filter_value);
% Use gaussian filter value to process image B_gaussian_fulter_value = imfilter(A, gausian_filter_value);
% Use midian filter value to process image B_median_filter_value = medfilt2(A, [9 9]);
Graphical user interfaces (GUIs) allow users to operate software programs using a point-and-click interface, reducing the need for others to learn a language or input instructions in order to utilize it. MATLAB provides developers with the tools to develop GUI apps that can be shared with other Matlab projects, desktops, or even web applications. GUI is comprised of different visual objects such as windows, menus, toolbars, icons, texts, navigation menus, and many others. The user-program interaction is mainly achieved through mouse clicks, scrolling, screen touch, and toggle buttons. The source code for this section is provided in the appendix.
Open-Ended Question: Image Sharpening
Sharpening is a method of enhancing an images perceived sharpness. Photo editing programs of libraries cannot magically add any more features to an image once it has been captured; the real resolution remains set [1]. One may enlarge the file, but the techniques used by any image editor would reduce the clarity of the image features. In other words, increasing acutance is the only method to improve perceived sharpness. You should increase the edge contrast of your image if you want it to seem sharper.
Sharpening your image has three purposes: to eliminate blurring caused by camera faults or limitations, to bring attention to certain regions, and to improve readability. Any current cameras RAW data are always slightly blurred [1]. Blur is introduced at every stage of the image capture process. Some definition is lost when the light goes through the lens elements, no matter how finely crafted they are. When sharpening an image, photo editing software and libraries average out the sharpest transitions to produce better results. A modest amount of blur is generated when the three separate color channels are interpolated to form the final image.
MATLAB uses an inbuilt function (imsharpen) to sharpen images as shown in the code snippet below.
Table 2. Image sharpening using Matlab code.
clear all; close all; clc;
% open image file image_input_file = imread(test1.jpg);
The paper is comprised of three main sections: image filtering, GUI development using Matlab and image sharing. Image sharpening is employed in changing the size, shape, smoothness and other image characteristics. The Matlab feature for image filtering is used to perform Gaussian, average and median filtering. The second used Matlab to develop a graphical user interface that can be used for various applications. The application is full of features such as file opening, text zooming and a toolbar. The third section implements an image sharpening program in Matlab. Sharpening your image has three purposes: to eliminate blurring caused by camera faults or limitations, to bring attention to certain regions, and to improve readability.
% mouse over media EnterKeyFunction = @(figure_handle,currentPoint) set(figure_handle,Pointer,arrow); iptSetPointerBehavior(object_figure,EnterKeyFunction) iptSetPointerBehavior(axes_spectrogram,EnterKeyFunction) iptPointerManager(object_figure);
% initialize player media_player_audio = audioplayer(0,80);
% figure = visible object_figure.Visible = on;
% call back function for open button function openButtonClickedCallBackFunction(~,~) % dialog box to open files [audio_file_name,audio_file_path] = uigetfile({*.mp32;*.wav},& MP3 or WAV audio files); if isequal(audio_file_name,0) || isequal(audio_file_path,0) return end % Remove figures to allow creation of other objects object_figure.RequestCloseFunction = ; % busy pointer object_figure.Pointer = watch; drawnow % stop playing audio media if isplaying(media_player_audio) stop(media_player_audio) end % Clearaxes cla(axes_signal) axes_signal.Visible = off; cla(axes_spectrogram) axes_spectrogram.Visible = off; drawnow % Build name of file full_file_audio = fullfile(audio_file_path,audio_file_name); % audio and rate (Hz) [audio_file_signal,audio_file_sample_rate] = audioread(full_file_audio); % channels and samples [number_of_samples,number_of_channels] = size(audio_file_signal); % plot audio signal. disable mouse clicks plot(axes_signal,& 1/audio_file_sample_rate:1/audio_file_sample_rate:number_of_samples/audio_file_sample_rate,& audio_file_signal,& PickableParts,none); % Update axes properties axes_signal.XLim = [1,number_of_samples]/audio_file_sample_rate; axes_signal.YLim = [-1,1]; axes_signal.XGrid = on; axes_signal.Title.String = audio_file_name; axes_signal.Title.Interpreter = None; axes_signal.XLabel.String = Time (s); axes_signal.Layer = top; axes_signal.UserData.PlotXLim = [1,number_of_samples]/audio_file_sample_rate; axes_signal.UserData.SelectXLim = [1,number_of_samples]/audio_file_sample_rate; drawnow % Add the CQT toolbox folder to the search path addpath(CQT_toolbox_2013) % Min & max Hz, number of channels resolution_of_octave = 24; min_freq_value = 27.5; max_freq_value = audio_file_sample_rate/2; % Initialize the CQT object and the spectrogram cqt_audio_cell = cell(1,number_of_channels); audio_spectrogram = []; % Compute the CQT ,spectrogram for all channel for index_of_channel = 1:number_of_channels %#ok<*FXUP> cqt_audio_cell{index_of_channel}& = cqt(audio_file_signal(:,index_of_channel),resolution_of_octave,audio_file_sample_rate,min_freq_value,max_freq_value); audio_spectrogram = cat(3,audio_spectrogram,abs(cqt_audio_cell{index_of_channel}.c)); end % Number of frequency channels and time frames [frequency_number,number_of_times,~] = size(audio_spectrogram); % Update the maximum frequency in Hz max_freq_value = min_freq_value*2.^((frequency_number-1)/resolution_of_octave); % Time range in seconds media_play_time_range = [1,number_of_times]/number_of_times*number_of_samples/audio_file_sample_rate; % Display the audio spectrogram imagesc(axes_spectrogram,& media_play_time_range,& [(min_freq_value*2*frequency_number+max_freq_value)/(2*frequency_number+1),& (max_freq_value*2*frequency_number+min_freq_value)/(2*frequency_number+1)],& db(mean(audio_spectrogram,3)),& PickableParts,none); % Update spectrogram axes properties axes_spectrogram.YScale = log; axes_spectrogram.YDir = normal; axes_spectrogram.XGrid = on; axes_spectrogram.Colormap = jet; axes_spectrogram.Title.String = Log-spectrogram; axes_spectrogram.XLabel.String = Time (s); axes_spectrogram.YLabel.String = Frequency (Hz); axes_spectrogram.ButtonDownFcn = @SpectrogramAxesButtonCallFunction; drawnow % Color limits screen_color_limits = axes_spectrogram.CLim; % playing audio object media_player_audio = audioplayer(audio_file_signal,audio_file_sample_rate); % play line selectline(axes_signal) PlayLineOnAxis(axes_signal,media_player_audio,media_button_play); % click button call back function media_button_play.ClickedCallback = {@PlayButtonClickedCallBackFunction,media_player_audio,axes_signal}; % Add key-press call back function object_figure.KeyPressFcn = @keyPressedCallBackFunction; % uREPET button call back function main_button_urepet.ClickedCallback = @UrepetButtonClickedCallBackFunction; % rectagle object rect_obj = gobjects(0); % convert Hz to indices hertz_to_frequency = @(value_of_frequency) round(resolution_of_octave*log2(value_of_frequency/min_freq_value)+1); seconds_to_time = @(value_of_time) round(value_of_time/(number_of_samples/audio_file_sample_rate)*number_of_times); % Enable buttons media_button_play.Enable = on; media_button_select.Enable = on; media_button_zoom.Enable = on; media_button_pan.Enable = on; main_button_urepet.Enable = on; main_button_background.Enable = on; main_button_background.State = on; % activate select button media_button_select.State = on; % update mouse pointer object_figure.Pointer = arrow; drawnow % close request function object_figure.RequestCloseFunction = @figureRequestCloseFunction; % Key-press callback function to the figure function keyPressedCallBackFunction(~,~) % if escape character if ~strcmp( ,object_figure.CurrentCharacter) return end % if any media is playing if isplaying(media_player_audio) % stop stop(media_player_audio) else % camples and sample rate audio_file_sample_rate = media_player_audio.SampleRate; number_of_samples = media_player_audio.TotalSamples; % Plot data on axes limits_of_plots = axes_signal.UserData.PlotXLim; selected_limits = axes_signal.UserData.SelectXLim; % calculate sample rate for audio if selected_limits(1) == selected_limits(2) % If select line range_of_samples = [round((selected_limits(1)-limits_of_plots(1))*audio_file_sample_rate)+1,number_of_samples]; else % If select region range_of_samples = round((selected_limits-limits_of_plots(1))*audio_file_sample_rate+1); end % Play audio with given rates play(media_player_audio,range_of_samples) end end % Mouse-click function (spectrogram) function SpectrogramAxesButtonCallFunction(~,~) % mouse location current_mouse_pointer_location = axes_spectrogram.CurrentPoint; % if mouse is outside the axes if current_mouse_pointer_location(1,1) < media_play_time_range(1) || current_mouse_pointer_location(1,1) >media_play_time_range(2) ||& current_mouse_pointer_location(1,2) < min_freq_value || current_mouse_pointer_location(1,2) >max_freq_value return end % If left click if strcmp(object_figure.SelectionType,normal) % Delete rectangle object delete(rect_obj) % Draw RIO from specified point rect_obj = images.roi.Rectangle(Parent,axes_spectrogram,& DrawingArea,[media_play_time_range(1),min_freq_value,diff(media_play_time_range),max_freq_value-min_freq_value]); beginDrawingFromPoint(rect_obj,current_mouse_pointer_location(1,1:2)); end end % Clicked callback function for the uREPET button function UrepetButtonClickedCallBackFunction(~,~) % If rectangle not valid or empty if isempty(rect_obj) || ~isvalid(rect_obj) return end % Position of ROI position_of_rectangle = rect_obj.Position; %width=heithg=0 if all(~position_of_rectangle(3:4)) return end % Remove close request call back. Allows creation of other objects object_figure.RequestCloseFunction = ; % update pointer to busy object_figure.Pointer = watch; drawnow % stop playing audio player if isplaying(media_player_audio) stop(media_player_audio) end % add original audio to undo audio_file_signal0 = audio_file_signal; cqt_audio_cell0 = cqt_audio_cell; % Frequency & time indices indices_of_frequencies = hertz_to_frequency(position_of_rectangle(2)+[0,position_of_rectangle(4)]); indeces_of_time = seconds_to_time(position_of_rectangle(1)+[0,position_of_rectangle(3)]); % rectangle from spectrogram audio_from_rectangle = audio_spectrogram(indices_of_frequencies(1):indices_of_frequencies(2),& indeces_of_time(1):indeces_of_time(2),:); size_of_rectangle = size(audio_from_rectangle); % 2-D cross-correlation correlation_of_audios = normxcorr2(mean(audio_from_rectangle,3),mean(audio_spectrogram,3)); % Remove zero padding parts correlation_of_audios = correlation_of_audios(size_of_rectangle(1):end-size_of_rectangle(1)+1,& size_of_rectangle(2):end-size_of_rectangle(2)+1); correlation_size = size(correlation_of_audios); % Max repetitions, min freq number_of_repetitions = 10; separation_of_Frequencies = 1; separation_of_time = 1; separation_of_Frequencies = separation_of_Frequencies*resolution_of_octave; separation_of_time = seconds_to_time(separation_of_time); correlation_of_audios(max(indices_of_frequencies(1)-separation_of_Frequencies,1):min(indices_of_frequencies(1)+separation_of_Frequencies,correlation_size(1)),& max(indeces_of_time(1)-separation_of_time,1):min(indeces_of_time(1)+separation_of_time,correlation_size(2))) = 0; % Loop over repetitions for repet_index = 2:number_of_repetitions % Frequency,time indices of the min repetition [~,maximum_index] = max(correlation_of_audios(:)); [frequency_index,time_index] = ind2sub(correlation_size,maximum_index);
correlation_of_audios(max(frequency_index-separation_of_Frequencies,1):min(frequency_index+separation_of_Frequencies,correlation_size(1)),& max(time_index-separation_of_time,1):min(time_index+separation_of_time,correlation_size(2))) = 0; audio_from_rectangle = cat(4,audio_from_rectangle,& audio_spectrogram(frequency_index:frequency_index+size_of_rectangle(1)-1,& time_index:time_index+size_of_rectangle(2)-1,:)); end % rectangles mask audio_rectangle_mask = (min(median(audio_from_rectangle,4),audio_from_rectangle(:,:,:,1))+eps)./(audio_from_rectangle(:,:,:,1)+eps); % If the background button is off, invert the mask if strcmp(main_button_background.State,off) audio_rectangle_mask = audio_rectangle_mask-1; end % Apply the mask to the CQT and spectrogram audio_file_signal = zeros(number_of_samples,number_of_channels); for index_of_channel = 1:number_of_channels cqt_audio_cell{index_of_channel}.c(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2))& = audio_rectangle_mask(:,:,index_of_channel).*cqt_audio_cell{index_of_channel}.c(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2)); audio_spectrogram(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2),index_of_channel)& = audio_rectangle_mask(:,:,index_of_channel).*audio_spectrogram(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2),index_of_channel); audio_file_signali = icqt(cqt_audio_cell{index_of_channel}); audio_file_signal(:,index_of_channel) = audio_file_signali(1:number_of_samples); end % Update the signal axes index_of_channel = number_of_channels; for child_index = 1:numel(axes_signal.Children) if numel(axes_signal.Children(child_index).YData) == number_of_samples axes_signal.Children(child_index).YData = audio_file_signal(:,index_of_channel); index_of_channel = index_of_channel-1; end end drawnow % Update the spectrogram axes axes_spectrogram.Children(end).CData(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2))& = db(mean(audio_spectrogram(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2),:),3)); axes_spectrogram.CLim = screen_color_limits; drawnow % Update the audio player media_player_audio = audioplayer(audio_file_signal,audio_file_sample_rate); PlayLineOnAxis(axes_signal,media_player_audio,media_button_play); media_button_play.ClickedCallback = {@PlayButtonClickedCallBackFunction,media_player_audio,axes_signal}; media_button_save.ClickedCallback = @SaveButtonClickedCallBackFunction; media_undo_button.ClickedCallback = @UndoButtonClickedCallBackFunction; % Enable nuttons media_button_save.Enable = on; media_undo_button.Enable = on; object_figure.RequestCloseFunction = @figureRequestCloseFunction; % Change pointer object_figure.Pointer = arrow; % Clicked callback function for the save button function SaveButtonClickedCallBackFunction(~,~) % Open dialog box to save file [audio_file_name,audio_file_path] = uiputfile(*.wav*,& Save Audio as WAVE File,urepet_file.wav); if isequal(audio_file_name,0) || isequal(audio_file_path,0) return end % file name full_file_audio = fullfile(audio_file_path,audio_file_name); % write audio file audiowrite(audio_file,audio_file_signal,audio_file_sample_rate) end % undo button call back function function UndoButtonClickedCallBackFunction(~,~) % Disable button media_undo_button.Enable = off; audio_file_signal = audio_file_signal0; cqt_audio_cell = cqt_audio_cell0; audio_spectrogram = []; for index_of_channel = 1:number_of_channels audio_spectrogram = cat(3,audio_spectrogram,abs(cqt_audio_cell{index_of_channel}.c)); end % Update axes index_of_channel = number_of_channels; for child_index = 1:numel(axes_signal.Children) if numel(axes_signal.Children(child_index).YData) == number_of_samples axes_signal.Children(child_index).YData = audio_file_signal(:,index_of_channel); index_of_channel = index_of_channel-1; end end drawnow % Update spectrogram axes axes_spectrogram.Children(end).CData(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2))& = db(mean(audio_spectrogram(indices_of_frequencies(1):indices_of_frequencies(2),indeces_of_time(1):indeces_of_time(2),:),3)); axes_spectrogram.CLim = screen_color_limits; drawnow % Update the audio player media_player_audio = audioplayer(audio_file_signal,audio_file_sample_rate); PlayLineOnAxis(axes_signal,media_player_audio,media_button_play); media_button_play.ClickedCallback = {@PlayButtonClickedCallBackFunction,media_player_audio,axes_signal}; end end end
% Clicked callback function for the select button function SelectCLickedCallBackFunction(~,~) % update button status media_button_select.State = on; media_button_zoom.State = off; media_button_pan.State = off; zoom off pan off end
% Clicked callback function for the zoom button function ZoomClickedCallbackFunction(~,~) % update buttons media_button_select.State = off; media_button_zoom.State = on; media_button_pan.State = off; % enable zoom zoom_object = zoom(object_figure); zoom_object.Enable = on; % zoom x axes only setAxesZoomConstraint(zoom_object,axes_signal,x); %update pan pan off end
% pan clicked call back function function panClickedCallBackFunction(~,~) % update buttons media_button_select.State = off; media_button_zoom.State = off; media_button_pan.State = on; zoom off pan_object = pan(object_figure); pan_object.Enable = on; % set pan for x axis only setAxesPanConstraint(pan_object,axes_signal,x); end
% Clicked callback function for the background button function BackGroundClickedCallBackFunction(~,~) % change mouse depending on what is happening if strcmp(main_button_background.State,on) main_button_background.TooltipString = Background; elseif strcmp(main_button_background.State,off) main_button_background.TooltipString = Foreground; end end
% Close call back function function figureRequestCloseFunction(~,~) % stop playing life if isplaying(media_player_audio) stop(media_player_audio) end % close dialog box user_answer = questdlg(Close window?,& Close window,Yes,No,Yes); switch user_answer case Yes delete(object_figure) case No return end end
end
% Read imatlab icon function image_function_data = iconread(icon_name)