How to implement convolution instead of the built-in imfilter

3 次查看(过去 30 天)
m=13; n=13;
sigma=15;
[h1 h2]=meshgrid(-(m-1)/2:(m-1)/2, -(n-1)/2:(n-1)/2);
hg= exp(-(h1.^2+h2.^2)/(2*sigma^2)); %Gaussian function
h=hg ./sum(hg(:));
Now I want to use these Gaussian kernels to implement linear filtering with on image
OriginalRGB = imread('LeerNaam.png'); % read image
filter=h; s = size(OriginalRGB);
r = zeros(s);
for i = 2:s(1)-1
for j = 2:s(2)-1
temp = OriginalRGB(i-1:i+1,j-1:j+1)) .* filter;
r(i,j) = sum(temp(:));
end
end
  4 个评论
Image Analyst
Image Analyst 2013-7-10
Don't do that - it's not linear filtering, that's masking. See my demo below.
Ferdie
Ferdie 2013-7-11
Thanks... The reason for not implementing imfilter is because I need to know what imfilter does. It is for a project and I can't just use built-in functions.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2013-7-10
Try this demo:
clc; % Clear the command window.
clearvars; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
close all;
% 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 a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 1, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Construct Gaussian Kernel
m=13;
n=13;
sigma=15;
[h1 h2]=meshgrid(-(m-1)/2:(m-1)/2, -(n-1)/2:(n-1)/2);
hg= exp(-(h1.^2+h2.^2)/(2*sigma^2)); %Gaussian function
h=hg ./sum(hg(:))
% Could be done easier with fspecial though!
% Convolve the three separate color channels.
redBlurred = conv2(redChannel, h);
greenBlurred = conv2(greenChannel, h);
blueBlurred = conv2(blueChannel, h);
% Recombine separate color channels into a single, true color RGB image.
rgbImage2 = cat(3, uint8(redBlurred), uint8(greenBlurred), uint8(blueBlurred));
% Display the blurred color image.
subplot(2, 1, 2);
imshow(rgbImage2);
title('Blurred Color Image', 'FontSize', fontSize);
  1 个评论
Ferdie
Ferdie 2013-7-11
Thank you very much! But I also need to do the convolution without using the conv2 built in function. I tried for loops, but I have problem when doing .* multiplication. Matlab gives errors. How can I code conv2?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by