something strange in my own imfilter
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.
Accepted Answer
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);
1 Comment
abdulvahid uçar
on 14 Apr 2012
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!