MATLAB: Convolution of a matrix convolutionimage processing I've tried to code some of the problem and it is working for a 3*3 filter Best Answer Check conv2() to avoid for loop. Following correct the mistake in your codeclc;clear all;close all;im=double(imread('cameraman.tif'));w=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;];k = floor(size(w,1)/2);[M,N]=size(im);g = zeros(M, N);for i=k+1:M-k for j=k+1:N-k g(i,j)=sum(im(i-k:i+k,j-k:j+k).*w, 'all'); endendfigure,imshow(im,[]);figure,imshow(g,[]);A simple version with conv2im=im2double(imread('cameraman.tif'));w=ones(5);g = conv2(im, w, 'same');figure,imshow(im,[]);figure,imshow(g,[]); Related SolutionsMATLAB: Why output image is showing blank Because round(k) is a floating point array, it expects the values to be between 0 and 1 inclusive. To get around this use [] in imshow:imshow(round(k), []);or cast to uint8:imshow(uint8(round(k))); MATLAB: Implement average filter without using built-in functions In all likelihood, you have not converted your image to floating pointimg = im2double( imread("Q3_Input", "tif") ); Related QuestionHow to transform double to unit8How to apply the arithmetic mean filter to a medical image to improve it? I have wrote this code but it did not work, there is an error which I could not figure it out.
Best Answer