What is wrong with my image filter code?

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)

 採用された回答

Jan
Jan 2015 年 3 月 26 日
編集済み: Jan 2015 年 3 月 26 日

0 投票

If you remove the evil clear all you could use the debugger to step through your code line by line to find out, what's going on.
You create window with n+(n-1)*3 elements, but try to access the values until n*n.
By the way: What about using sort for an efficient sorting?

その他の回答 (2 件)

david
david 2015 年 3 月 26 日

0 投票

how can i use sort?
Image Analyst
Image Analyst 2015 年 3 月 26 日

0 投票

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 件のコメント

david
david 2015 年 3 月 26 日
thanks,yeah i know i can use medfilt2 but i'm not alloweded for this... i check your solutions but it didn't work.
Image Analyst
Image Analyst 2015 年 3 月 26 日
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
david 2015 年 3 月 27 日
i don't know you should probably ask my teacher ! i think this kind of "sort" is allowed ,but not sure.actually i think my teacher don't want us use only imfilter or medfilter2... anyway,can somebody tell me what's diffrence between conv2 and imfilter? for my another code for average filter i use conv2 and imfilter have average filter built-in,so they are equal?
Image Analyst
Image Analyst 2015 年 3 月 27 日
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.
david
david 2015 年 3 月 27 日
編集済み: david 2015 年 3 月 27 日
Thanks.i have an another question,as i see you are an image specialist and i think you read image processing "gonzalez" book,in chapter 3 we have "box filter" ,"averaging filter"and "smoothing linear filter" ,are these 3 filters named for same averaging filter and they are equal? later in chapter 5 we have arithmetic filter and said that is similar to averaging filter,so arithmetic filter is actually the same? or have diffrences? do you have any matlab code for arithmetic filter? i didn't find anything. thank you.
Image Analyst
Image Analyst 2015 年 3 月 27 日
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
david 2015 年 3 月 27 日
what about arithmetic filter?did you see anything like that?
Image Analyst
Image Analyst 2015 年 3 月 27 日
I have no idea what their definition of that is. Of course, all filters are arithmetic in that they use numbers.

サインインしてコメントする。

タグ

質問済み:

2015 年 3 月 26 日

コメント済み:

2015 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by