What is wrong with my image filter code?
Mostrar comentarios más antiguos
Hello i have a code below,it works with n=3 ,but for kernel n=5 or above it doesnt work
error: ??? Attempted to access window(18); index out of bounds because numel(window)=17.
how can i fix it? Thanks.
% code
clear all
image=imread('cameraman.tif');
n=3
nPercent = 10;
[y x]=size(image);
nMaxHeight=round(y*nPercent/100.0);
nMaxWidth=round(x*nPercent/100.0);
for I=1:nMaxHeight,
for J=1:nMaxWidth,
cx=round(rand(1)*(x-1))+1;
cy=round(rand(1)*(y-1))+1;
aaa=round(rand(1)*255);
if aaa>128
image(cy,cx)=255;
else
image(cy,cx)=1;
end
end
end
for i=1:x
for j=1:y
if(i==1 || j==1 || i==x ||j==y)
image_out(j,i)=image(j,i);
else
for l= 1:n
for k=1:n
window(l+(k-1)*3)=image(j+l-2,i+k-2);
end
end
for l=1:(n*n-1)
for k=2:(n*n)
if (window(l)>window(k))
temp=window(k);
window(k)=window(l);
window(l)=temp;
end
end
end
image_out(j,i)=window(5);
end
end
end
figure
subplot(1,2,1);imshow(image)
subplot(1,2,2);imshow(image_out)
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 26 de Mzo. de 2015
Don't use "image" as a variable name since it's a built in function.
For the first double for loop, where you threshold the image, simply do:
binaryImage = yourImage > 128;
For the second loop, I'm not exactly sure what you're doing, but you can probably do your second loop without loops using a single call to imfilter(), or conv2(), or medfilt2(), or ordfilt2() .
8 comentarios
david
el 26 de Mzo. de 2015
Image Analyst
el 26 de Mzo. de 2015
What, huh? Why not? Is this homework? If you can't use medfilt2(), then you probably can't use sort() either so not sure why your "Answer" asked that.
david
el 27 de Mzo. de 2015
Image Analyst
el 27 de Mzo. de 2015
Not exactly equal. conv2() "flips" the kernel before multiplying and summing, because this is what the convolution formula requires, whereas imfilter() just leaves it alone - it's a straight multiply and sum. For a symmetric kernel there is no difference because a flipped kernel and the original would be the same. For an asymmetric kernel there is a difference.
Image Analyst
el 27 de Mzo. de 2015
Sorry, I don't have that book anymore. My company went paperless about 5 years ago and I had to throw out most of my old textbooks. I don't think I have that one. Most likely the filter kernels are slightly different. A box filter has a flat top and is rectangular/square. Averaging can be any filter with all positive weights in the kernel, no negative weights (which will cause edge detection). Same for smoothing. Linear means it can be done with convolution (multiply and sum) and doesn't do anything tricky or bizarre, like median filter or things like that.
david
el 27 de Mzo. de 2015
Image Analyst
el 27 de Mzo. de 2015
I have no idea what their definition of that is. Of course, all filters are arithmetic in that they use numbers.
Categorías
Más información sobre Image Filtering en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!