0% found this document useful (0 votes)
16 views4 pages

NEW

The document describes a MATLAB GUI for image processing that allows users to load images, add various types of noise, and apply different filters. It includes functionalities for selecting noise types (Salt & Pepper, Gaussian, Speckle) and filter types (Median, Gaussian, Average, Alpha-Trimmed Mean, Midpoint, Contraharmonic Mean, Fourier Transform). The GUI displays the original and processed images side by side for comparison.

Uploaded by

Hamza Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

NEW

The document describes a MATLAB GUI for image processing that allows users to load images, add various types of noise, and apply different filters. It includes functionalities for selecting noise types (Salt & Pepper, Gaussian, Speckle) and filter types (Median, Gaussian, Average, Alpha-Trimmed Mean, Midpoint, Contraharmonic Mean, Fourier Transform). The GUI displays the original and processed images side by side for comparison.

Uploaded by

Hamza Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

pkg load image

______________________________________________________________

function ImageProcessingGUI
% Create GUI window
hFig = figure('Name', 'Image Processing', 'NumberTitle', 'off', ...
'Position', [300, 200, 600, 500], 'Color', [0.94, 0.94, 0.96]);

% Input and output buttons


uicontrol('Style', 'pushbutton', 'Position', [50, 450, 100, 30], 'String',
'Load Image', ...
'Callback', @loadImage);

uicontrol('Style', 'text', 'Position', [50, 390, 100, 20], 'String', 'Noise


Type:', ...
'HorizontalAlignment', 'right', 'FontSize', 10);

% Choose noise type


noiseTypeMenu = uicontrol('Style', 'popupmenu', 'Position', [160, 390, 150,
25], ...
'String', {'Salt & Pepper', 'Gaussian', 'Speckle'}, 'FontSize', 10);

uicontrol('Style', 'text', 'Position', [50, 330, 100, 20], 'String', 'Filter


Type:', ...
'HorizontalAlignment', 'right', 'FontSize', 10);

% Choose filter type


filterTypeMenu = uicontrol('Style', 'popupmenu', 'Position', [160, 330, 150,
25], ...
'String', {'Median Filter', 'Gaussian Filter', 'Average Filter', ...
'Alpha-Trimmed Mean', 'Midpoint Filter', ...
'Contraharmonic Mean', 'Fourier Transform'}, 'FontSize', 10);

uicontrol('Style', 'pushbutton', 'Position', [350, 450, 100, 30], 'String',


'Add Noise', ...
'Callback', @addNoise);

uicontrol('Style', 'pushbutton', 'Position', [350, 390, 100, 30], 'String',


'Apply Filter', ...
'Callback', @applyFilter);

% Axes for displaying images


hAxesOriginal = axes('Units', 'pixels', 'Position', [50, 150, 200, 200]);
title(hAxesOriginal, 'Original Image');
axis(hAxesOriginal, 'off');

hAxesProcessed = axes('Units', 'pixels', 'Position', [350, 150, 200, 200]);


title(hAxesProcessed, 'Processed Image');
axis(hAxesProcessed, 'off');

% Variables for storing images


originalImage = [];
noisyImage = [];

% Load image function


function loadImage(~, ~)
[fileName, filePath] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files'},
'Select an Image');
if fileName == 0
return; % User canceled the selection
end
originalImage = imread(fullfile(filePath, fileName));
if size(originalImage, 3) == 3
originalImage = rgb2gray(originalImage); % Convert to grayscale if the
image is colored
end
imshow(originalImage, 'Parent', hAxesOriginal);
title(hAxesOriginal, 'Original Image');
end

% Add noise function


function addNoise(~, ~)
if isempty(originalImage)
msgbox('Please load an image first.', 'Error', 'error');
return;
end

% Read the selected noise type


noiseType = get(noiseTypeMenu, 'Value'); % Use get to access the Value
property

switch noiseType
case 1 % Salt & Pepper
noisyImage = imnoise(originalImage, 'salt & pepper', 0.05);
case 2 % Gaussian
noisyImage = imnoise(originalImage, 'gaussian', 0, 0.01);
case 3 % Speckle
noisyImage = imnoise(originalImage, 'speckle', 0.01);
end

imshow(noisyImage, 'Parent', hAxesProcessed);


title(hAxesProcessed, 'Noisy Image');
end

% Apply filter function


function applyFilter(~, ~)
if isempty(noisyImage)
msgbox('Please add noise to the image first.', 'Error', 'error');
return;
end

% Read the selected filter type


filterType = get(filterTypeMenu, 'Value'); % Use get to access the Value
property

switch filterType
case 1 % Median Filter
filteredImage = medfilt2(noisyImage, [3, 3]);
case 2 % Gaussian Filter
filteredImage = imgaussfilt(noisyImage, 2); % Standard deviation 2
case 3 % Average Filter
h = fspecial('average', [3 3]);
filteredImage = imfilter(noisyImage, h);
case 4 % Alpha-Trimmed Mean
alpha = 2; % Alpha value
windowSize = 3;
filteredImage = alphaTrimmedMean(noisyImage, windowSize, alpha);
case 5 % Midpoint Filter
filteredImage = midpointFilter(noisyImage, 3);
case 6 % Contraharmonic Mean
Q = 1.5; % Q constant
filteredImage = contraharmonicMean(noisyImage, 3, Q);
case 7 % Fourier Transform
filteredImage = abs(ifft2(fft2(double(noisyImage)))); % Simple
Fourier transform
end

imshow(filteredImage, 'Parent', hAxesProcessed);


title(hAxesProcessed, 'Filtered Image');
end

% Alpha-Trimmed Mean Filter Function


function output = alphaTrimmedMean(img, windowSize, alpha)
padded = padarray(img, [floor(windowSize/2), floor(windowSize/2)],
'symmetric');
output = zeros(size(img));
for i = 1:size(img, 1)
for j = 1:size(img, 2)
window = double(padded(i:i+windowSize-1, j:j+windowSize-1));
sortedWindow = sort(window(:));
trimmedWindow = sortedWindow(alpha+1:end-alpha);
output(i, j) = mean(trimmedWindow);
end
end
output = uint8(output);
end

% Midpoint Filter Function


function output = midpointFilter(img, windowSize)
padded = padarray(img, [floor(windowSize/2), floor(windowSize/2)],
'symmetric');
output = zeros(size(img));
for i = 1:size(img, 1)
for j = 1:size(img, 2)
window = double(padded(i:i+windowSize-1, j:j+windowSize-1));
output(i, j) = (min(window(:)) + max(window(:))) / 2;
end
end
output = uint8(output);
end

% Contraharmonic Mean Filter Function


function output = contraharmonicMean(img, windowSize, Q)
padded = padarray(img, [floor(windowSize/2), floor(windowSize/2)],
'symmetric');
output = zeros(size(img));
for i = 1:size(img, 1)
for j = 1:size(img, 2)
window = double(padded(i:i+windowSize-1, j:j+windowSize-1));
numerator = sum(window(:).^(Q+1));
denominator = sum(window(:).^Q);
output(i, j) = numerator / denominator;
end
end
output = uint8(output);
end
end
________________________________________________________

ImageProcessingGUI

You might also like