How to implement convolution instead of the built-in imfilter

10 ビュー (過去 30 日間)
Valeska Pearson
Valeska Pearson 2013 年 7 月 10 日
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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by