Help with "for" loop

Associate
Joined
28 Jul 2003
Posts
1,987
Location
The Moon
Hello,

I am wanting to create a for loop in Matlab which will allow me to cycle an image through various sizes. The code to do this is imresize(image, scaling factor)

I wrote my loop as follows but it does not work;

for n = 0.1:0.1:1
h = imresize(image1, n)
c = normxcorr2(image2,h)
end

I am wanting to scale image1 through various sizes, correlating it with a larger image each time in order to find the best match. However this is not happening, I know I have written the loop incorrectly but I cannot see where exactly.
 
thats because you dont save the best case into c, you save the value from the last iteration in the loop.

are you sure that h > image2? that is a requirement as well.
 
Just out of curiosity, how are the results output? Considering how quickly that for loop would be executed if it is resizing a displayed image you wouldn't even see the 10 smaller versions as it would seem to instantly move to the final iteration.

Also, C is unreferenced, so has no effect? Unless there is a visible effect from the normxcorr2 function...?
 
It should have been c = normxcorr2(h,image2). I think I need to do something like this:

for i = 0.1:0.1:1
h(i) = imresize(image1,i);
c(i) = normxcorr2(h,image2);
end
 
for i = 0.1:0.1:1
h(i) = imresize(image1,i);
c(i) = normxcorr2(h(i),image2);
end

but you needn't save each h(i), and you only need to keep the 'best' c.

have you sorted it or need more help?
 
gimme some test data and expected results. :) I'll try and have a go tomorrow after work.

what error message, if any, do you get?

this:
Code:
c(i)= ...

wont work either since the rhs needs to return a 2-d matrix (I think), and that cant go into a single index, c(i).
However,
Code:
c(i,:,:) = ....
should be ok if that is the case.
 
Last edited:
When I use the original loop I posted, I got this error, Subscript indices must either be real positive integers or logicals.
 
you dont have any subscript indices in your original post.

in
Code:
for i = 0.1:0.1:1
h(i) = imresize(image1,i);
c(i) = normxcorr2(h,image2);
end

I missed that you cant use i in c(i) since i = 0.1 0.2 0.3 etc. which would give you that error message.
 
you dont have any subscript indices in your original post.

in
Code:
for i = 0.1:0.1:1
h(i) = imresize(image1,i);
c(i) = normxcorr2(h,image2);
end

I missed that you cant use i in c(i) since i = 0.1 0.2 0.3 etc. which would give you that error message.

So how do I get around it?
 
you need to give more of your code. depends on the type of image you use.

sounds like you might be using colour images.

what is the size of normxcorr2(h,image2)?

x=normxcorr2(h,image2);
size(x);
 
I cant help any more until you give more info, like I have asked for previously, like images you are using, and more of your code.
 
Last edited:
Back
Top Bottom