applying special matris to median filter

5 views (last 30 days)
mustafa karatas
mustafa karatas on 16 Dec 2021
Answered: Image Analyst on 19 Dec 2021
hi everyone,
i would like to create a matrix then apply in median filter like 3x3 neighborhood.this matrix is folllowing;
medxfilter=[1,0,0,0,1;0,1,0,1,0;0,0,1,0,1;0,1,0,1,0;1,0,0,0,1]
how can apply a grayscale image which use this filter .

Answers (2)

Walter Roberson
Walter Roberson on 16 Dec 2021
filename = 'cameraman.tif';
img = imread(filename);
medxfilter = [1,0,0,0,1;0,1,0,1,0;0,0,1,0,1;0,1,0,1,0;1,0,0,0,1]
medxfilter = 5×5
1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1
fimg = conv2(img, medxfilter, 'same');
fimg8 = cast(fimg, 'like', img);
imshow(fimg8)
rs_fimg = fimg ./ sum(medxfilter(:));
rs_fimg8 = cast(rs_fimg, 'like', img);
imshow(rs_fimg8)
The first version is mostly white because you are adding up 10 different pixels, and that can give a maximum score of 255*10 . The second version scales the filtered image back by the sum of the weights, and gives a more realistic result.
  1 Comment
mustafa karatas
mustafa karatas on 19 Dec 2021
thanks your replying:) second version is good for me . on the other hand , how can i apply mean filter like 3x3 square neigboorhod following matrix.
mean4=[0,1,0;1,1,1;0,1,0];

Sign in to comment.


Image Analyst
Image Analyst on 19 Dec 2021
You can use nlfilter() to create any kind of mask shape you want. See attached demo.
% Demo to threshold an image with a locally adaptive Otsu threshold using nlfilter.
function test()
clc;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in gray scale demo image.
folder = pwd; % Change this if the image is not in the same folder as this m-file.
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo to median filter image with special mask', 'NumberTitle', 'Off')
% Let's compute and display the histogram, just for fun.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
%--------------------------------------------------------------------------
% Do a local median filter of the gray level image using the nlfilter function.
% We'll tell nlfilter to use our LocalMaskedMedian() function to do its operations.
fun = @(x) LocalMaskedMedian(x);
doubleImage = im2double(grayImage); % Cast to double.
% Here comes the actual filtering.
localThresh = nlfilter(doubleImage, [3, 3], fun);
%--------------------------------------------------------------------------
% All done!
% Display the image.
subplot(2, 2, 3);
imshow(localThresh, []);
title('Local Masked Median Filter', 'FontSize', fontSize);
% Function to take the Otsu threshold of the small patch of gray levels passed in by nlfilter().
function outputPixel = LocalMaskedMedian(grayImagePatch)
mask = logical([0,1,0;1,1,1;0,1,0]);
outputPixel = median(grayImagePatch(mask));
return; % from LocalMaskedMedian()

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!