Respuesta aceptada

Ameer Hamza
Ameer Hamza el 1 de Abr. de 2020
Editada: Ameer Hamza el 1 de Abr. de 2020

0 votos

Check conv2() to avoid for loop. Following correct the mistake in your code
clc;
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');
end
end
figure,imshow(im,[]);
figure,imshow(g,[]);
A simple version with conv2
im=im2double(imread('cameraman.tif'));
w=ones(5);
g = conv2(im, w, 'same');
figure,imshow(im,[]);
figure,imshow(g,[]);

4 comentarios

The Eagle
The Eagle el 1 de Abr. de 2020
I was working on it and I didn't want to use inbuilt functions. However I tried your code without conv2 and I got an error using sum.
Error using sum
Trailing string input must be 'double', 'native',
'default', 'omitnan' or 'includenan'.
Error in Untitled (line 11)
g(i,j)=sum(im(i-k:i+k,j-k:j+k).*w, 'all');
This was the error. Can you tell me how to resolve it? Actually I'm new to MATLAB :/
Ameer Hamza
Ameer Hamza el 1 de Abr. de 2020
I guess you are using some older version of MATLAB. Replace this line with
g(i,j)=sum(sum(im(i-k:i+k,j-k:j+k).*w));
The Eagle
The Eagle el 1 de Abr. de 2020
Yes I'm using R2016a thank you for helping. :)
Ameer Hamza
Ameer Hamza el 1 de Abr. de 2020
Glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 1 de Abr. de 2020

Editada:

el 2 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by