something strange in my own imfilter
4 views (last 30 days)
Show older comments
I tried to implement my own imfilter with 2D 5x5 Gaussian filter on gray scaled image, but I could not get the result. I am getting a fully white colored picture. Here is the myfilter.
function result = myfilter(sourcepicture)
G = fspecial('gaussian',[n n],4);
[r1 c1] = size(G);
n = (r1+1)/2 - 1;
[r0 c0] = size(sourcepicture);
picture = zeros(r0 + r1 +1,c0 + c1 +1);
picture(1+n:r0 +n ,1+n:c0+n) = sourcepicture(1:r0,1:c0);
result = [];
for i = 1:r0-n-1
for j = 1:c0-n-1
sample = double(picture(i:i+r1- 1,j:j+c1-1));
dummy = G.*sample;
result(i,j) = sum(sum(dummy));
end
end
Actually,
picture(1+n:r0 +n ,1+n:c0+n) = sourcepicture(1:r0,1:c0);
does not insert sourcepicture into picture, but I don't understand why.
0 Comments
Accepted Answer
Image Analyst
on 13 Apr 2012
I'm not sure what you're saying. It certainly does insert it. It's not centered but that's just because you messed up with the settings, but it did insert sourcepicture into picture as the Variable editor will reveal to you.
By the way, you could use padarray() instead, and you could also do
result(i,j) = sum(dummy(:));
instead of you want.
2 Comments
Image Analyst
on 14 Apr 2012
But it DOES insert it, and the pictures ARE almost the same, except for the zero padding because picture is bigger than source picture. sourcepicture is exactly in there inside picture. See my demo code I just posted here.
Maybe it's because you're just not displaying it correctly. Remember, these are double arrays you created so you should use imshow(picture, []) to display it otherwise it may show up as all white or all black.
More Answers (1)
Image Analyst
on 14 Apr 2012
It does insert it. Here's proof:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
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
sourcepicture = imread(fullFileName);
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(sourcepicture, []);
axis on;
title('"Source" picture', '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 by ImageAnalyst','numbertitle','off')
% Get the dimensions of the image.
[r0 c0] = size(sourcepicture);
picture = zeros(400, 'uint8');
n = 30; % Offset.
picture(1+n:r0 +n ,1+n:c0+n) = sourcepicture(1:r0,1:c0);
% Display it.
subplot(1, 2, 2);
imshow(picture, []);
axis on;
title('"Picture" picture', 'FontSize', fontSize);
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!