something strange in my own imfilter

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.

 Accepted Answer

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

Thanks for your comments.
picture(1+n:r0 +n ,1+n:c0+n) = source(1:r0,1:c0);
figure,
subplot(2,2,1),imshow(picture);title('source');
subplot(2,2,2),imshow(sourcepicture);title('picture');
after inserting these lines into the code, I must get almost same picture, but I don't
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.

Sign in to comment.

More Answers (1)

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);

1 Comment

Yes, I did what you said exactly. I was displaying pictures by using imshow(picture) and it was working good. When I replaced it with imshow(picture,[]), it worked. Actually, I compared the gray scale values in source and picture. They were the same. Thanks for your efforts. I spend your time with a silly mistake .I really appreciate. I don't know how to thank to you

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image 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!