Problem loop for and image size

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem loop for and image size

Julien563
Hello ;)



I would like to know two things :

First of all, i have nr=1000, that's ok, but nc = 3000??? Look my pic : Size in blue : 1000*1000 so why the programm doesn't show me : nc=1000 and nr= 1000.

And my last problem : my loop for.

Look "pixel_x_metres" and "pixel_y_metres" only "pixel_y_metres" changes why? I did the same loop for, for twice... Where am i wrong?

My programm:

A=imread('galaxy.jpg');

% Les Constantes

G= 6.67e-11 %Attention! Revoir les unités
c=3.0e+8
M= 1,7e+38 %Masse de la masse déflectrice
z1= 2,75e+22 %Distance de la masse par rapport à la Terre
z2= 2,75e+22%Distance de la galaxie par rapport à la masse

% Liste des Variables
[nr,nc]= size([A])
pixel_x1=round(nr/2-1)%Coordonnées de la masse
pixel_y1=round(nc/2-1)
pixel_x_metres=1e+16
pixel_y_metres=1e+16
pixel_x1_metres=(pixel_x_metres.*z1)./(z1+z2)
pixel_y1_metres=(pixel_y_metres.*z1)./(z1+z2)
R= (sqrt((pixel_x1_metres).^2+(pixel_y1_metres).^2)) %Rayon de la masse déflectrice (exemple de trou noir = 1 pixel)
largeur = nr  %largeur de l'image en pixel
longueur = nc %longueur de l'image en pixel
L=(4.*G*M)./(c^2)

% Lentille gravitationnelle

for pixel_x_metres=(-ceil(nc./2).*pixel_x_metres:pixel_x_metres:floor(nc./2).*pixel_x_metres)

        for pixel_y_metres=(-ceil(nr./2).*pixel_y_metres:pixel_y_metres:floor(nr./2).*pixel_y_metres)
       
                pixel_x_metres=pixel_x_metres
                pixel_y_metres=pixel_y_metres
                Ralpha = (sqrt(((pixel_x_metres.*(z1+z2)./z1)-(pixel_x1_metres)).^2+(((pixel_y_metres.*(z1+z2)./z1)-pixel_y1_metres).^2)));
                if Ralpha<=R;
                        j=round(((pixel_x_metres)./(pixel_x1_metres.*z1./(z1+z2)))+(floor(nc./2)))
                        i=round(((largeur+1)-pixel_y_metres./(pixel_y1_metres.*z1./(z1+z2)))+(floor(nr./2)))
                        B(i,j)= 0; % pixel noir
                else;
                        nouveau_pixel_x_metres=(((pixel_x_metres)*(z1+z2)./z1)+z2.*((((pixel_x_metres)*(z1+z2)./z1)./z1)-(L/Ralpha).*(((pixel_x_metres)*(z1+z2)./z1)-(pixel_x1_metres))./Ralpha));
                        nouveau_pixel_y_metres=((pixel_y_metres)*(z1+z2)./z1)+z2.*((((pixel_y_metres)*(z1+z2)./z1)./z1)-(L/Ralpha).*(((pixel_y_metres)*(z1+z2)./z1)-(pixel_y1_metres))./Ralpha);
                        j_nouveau=round((nouveau_pixel_x_metres+(floor(nc./2)))./(pixel_y_metres))
                        i_nouveau=round(((largeur+1)-(nouveau_pixel_y_metres+(floor(nr./2))))./(pixel_y_metres))
                endif
        endfor
endfor

Thx in advance
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Andreas Weber-6
Am 22.10.2016 um 17:28 schrieb Julien563:

> I would like to know two things :
> First of all, i have nr=1000, that's ok, but nc = 3000??? Look my pic : Size
> in blue : 1000*1000 so why the programm doesn't show me : nc=1000 and nr=
> 1000.
>
> A=imread('galaxy.jpg');
> r,nc]= size([A])

You are using "size" with two output args. See
[nr,nc]=size(ones(3,4,5))# This returns nc = 4*5 = 20
Use [nr,nc,channels]= size(A)

-- Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Julien563
Ok thx, a lot, it's working! But for the first loop for why my variable doesn't change?
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Andreas Weber-6
Am 22.10.2016 um 17:58 schrieb Julien563:
> Ok thx, a lot, it's working! But for the first loop for why my variable
> doesn't change?

Why do you expect it should change as long as the inner loop is running?
As you can see from the increasing pixel_y_metres the inner for loop is
still running. This is the same as:

for a = 1:3
  for b= 10:12
    printf ("a = %i, b = %i\n", a, b);
  endfor
endfor

a = 1, b = 10 #your screenshot
a = 1, b = 11 #your screenshot
a = 1, b = 12 #your screenshot
a = 2, b = 10
a = 2, b = 11
a = 2, b = 12
a = 3, b = 10
a = 3, b = 11
a = 3, b = 12

-- Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Julien563
This post was updated on .
Oh yes! I see, thank you for your help
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Julien563
In reply to this post by Julien563
Last thing,

 I knew that i had a problem with these loops.

When i did :

Why -Inf? for my 2nde value?.

Thx in advance
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Andreas Weber-6
Am 22.10.2016 um 19:37 schrieb Julien563:
> When i did :  <http://octave.1599824.n4.nabble.com/file/n4680309/Probem.jpg>

There is no need to make screenshots, just copy and paste the code/and
or output from the command window and insert it verbatim.

> Why -Inf? for my 2nde value?.

We can't say without having the values for nc and pixel_y_metres. I
guess one of them is already Inf:

octave:1> floor (Inf./2)*5
ans = Inf
octave:2> floor (1000./2)*Inf
ans = Inf

-- Andy

PS: your code with 2 nested for loops will be very slow. Try to
vectorize it.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

Julien563
This post was updated on .
First of all, thx Andy for your answer:

I tried to do a simple test to search the infinite value.

In my 2nde loop i have 2 infinite : upper bound and lower bound:

        for pixel_y_metres=(-ceil(nr./2).*pixel_y_metres:pixel_y_metres:floor(nr./2).*pixel_y_metres)

1) -ceil(nr./2).*pixel_y_metres = -Inf !=  -500.*e+16

-ceil(nr./2) = -500
pixel_y_metres = 0 (i don't know why? Because i define pixel_y_metres=1e+16, before....)
pixel_x_metres = -2.06e+18 (i don't know why? Because i define pixel_x_metres=1e+16, before....)


2) floor(nr./2).*pixel_y_metres = +Inf !=  +500.*e+16

 floor(nr./2)=500
 pixel_y_metres=0


In my 1st loop i have to wrong values of my bounds :

for pixel_x_metres=(-ceil(nc./2).*pixel_x_metres:pixel_x_metres:floor(nc./2).*pixel_x_metres)

1) -ceil(nc./2).*pixel_x_metres = 1.03e+21 != -500.*e+16

2) floor(nc./2).*pixel_x_metres) = -1.03e+21 != +500*e+16


For more explications i copy and paste one more time my programm there:

A=imread('galaxy.jpg');

% Les Constantes

G= 6.67e-11 %Attention! Revoir les unités
c=3.0e+8
M= 1,7e+38 %Masse de la masse déflectrice
z1= 2,75e+22 %Distance de la masse par rapport à la Terre
z2= 2,75e+22%Distance de la galaxie par rapport à la masse

% Liste des Variables
[nr,nc,channels]= size([A])
pixel_x1=round(nr/2)%Coordonnées de la masse
pixel_y1=round(nc/2)
pixel_x_metres=1e+16
pixel_y_metres=1e+16
pixel_x1_metres=(pixel_x_metres.*z1)./(z1+z2)
pixel_y1_metres=(pixel_y_metres.*z1)./(z1+z2)
R= (sqrt((pixel_x1_metres).^2+(pixel_y1_metres).^2)) %Rayon de la masse déflectrice (exemple de trou noir = 1 pixel)
largeur = nr  %largeur de l'image en pixel
longueur = nc %longueur de l'image en pixel
L=(4.*G*M)./(c^2)

% Lentille gravitationnelle
B=[]

for pixel_x_metres=(-ceil(nc./2).*pixel_x_metres:pixel_x_metres:floor(nc./2).*pixel_x_metres)

        for pixel_y_metres=(-ceil(nr./2).*pixel_y_metres:pixel_y_metres:floor(nr./2).*pixel_y_metres)
       
                Ralpha = (sqrt(((pixel_x_metres.*(z1+z2)./z1)-(pixel_x1_metres)).^2+(((pixel_y_metres.*(z1+z2)./z1)-pixel_y1_metres).^2)));
                if Ralpha<=R;
                        j=round(((pixel_x_metres)./(pixel_x1_metres.*z1./(z1+z2)))+(floor(nc./2)))
                        i=round(((largeur+1)-pixel_y_metres./(pixel_y1_metres.*z1./(z1+z2)))+(floor(nr./2)))
                        B(i,j)= 0; % pixel noir
                else;
                        nouveau_pixel_x_metres=(((pixel_x_metres)*(z1+z2)./z1)+z2.*((((pixel_x_metres)*(z1+z2)./z1)./z1)-(L/Ralpha).*(((pixel_x_metres)*(z1+z2)./z1)-(pixel_x1_metres))./Ralpha));
                        nouveau_pixel_y_metres=((pixel_y_metres)*(z1+z2)./z1)+z2.*((((pixel_y_metres)*(z1+z2)./z1)./z1)-(L/Ralpha).*(((pixel_y_metres)*(z1+z2)./z1)-(pixel_y1_metres))./Ralpha);
                        j_nouveau=round((nouveau_pixel_x_metres+(floor(nc./2)))./(pixel_y_metres))
                        i_nouveau=round(((largeur+1)-(nouveau_pixel_y_metres+(floor(nr./2))))./(pixel_y_metres))
                B(i_nouveau,j_nouveau)=A(i,j);
                endif
        endfor
endfor

Reply | Threaded
Open this post in threaded view
|

Re: Problem loop for and image size

nrjank
In reply to this post by Julien563

On Oct 22, 2016 1:13 PM, "Julien563" <[hidden email]> wrote:
>
> Oh yes! I see, so how can i change both together? In same time? Whith loop
> "while"?

If you want to change both variables at the same time, then you do not need two loops. Just ibe loop, where both variables are made to change as the loop increments. This could be done with a mathematical expression or an index:

for ind = 1:3
  a = ind;
  b = 9 + ind;
  printf ("a = %i, b = %i\n", a, b);
endfor

a = 1, b = 10
a = 2, b = 11
a = 3, b = 12

Or

a = [1,2,3];
b = [10,11,12];

for ind = 1:3
  printf ("a = %i, b = %i\n", a(ind), b(ind));
endfor

a = 1, b = 10
a = 2, b = 11
a = 3, b = 12


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave