Asked by Ahsen Feyza Dogan
on 12 Jul 2019

Hi, I am trying to assign distance between each pixel and picked point on mri to variable ed in for loop. However all elements are same. I dont understand the reason. Thank you...

clc;

clear all;

a=imread('tumor.jpeg');

b=imresize(a,3);

c=im2bw(b);

imshow(c);

d=zeros(size(c));

%150,210

[ptx,pty] = ginput(1);

newdata=[ptx, pty];

n=25;

%[count,x] = imhist(c);

[rows, columns]=size(c);

for i=1:rows

for j=1:columns

%calc and store sorted euclidean distances with corresponding indices

ed(i)=sqrt(sum((newdata-c(i,j)).^2));

euc=ed';

end

end

Answer by KSSV
on 12 Jul 2019

Edited by KSSV
on 12 Jul 2019

You should initiliaze ed before the loop.

for i=1:rows

for j=1:columns

%calc and store sorted euclidean distances with corresponding indices

ed(i,j)=sqrt(sum((newdata-c(i,j)).^2));

end

end

Loop is not required here.

ed=sqrt((newdata(1)-c).^2+(newdata(2)-c).^2);

KSSV
on 12 Jul 2019

Ahsen Feyza Dogan commented:

Thank you but it does not work true. I want to calculate distance between picked point and all pixels. Image size is 528*528 so distance array should contain 528 element. In my code all of them is same but it shoud not be.

KSSV
on 12 Jul 2019

KSSV
on 12 Jul 2019

Try:

a=imread('image.jpeg');

b=imresize(a,3);

c=im2bw(b);

imshow(c);

d=zeros(size(c));

%150,210

[ptx,pty] = ginput(1);

newdata=[ptx, pty];

n=25;

%[count,x] = imhist(c);

[nx,ny] = size(c) ;

[X,Y] = meshgrid(1:nx,1:ny) ;

ed=sqrt((newdata(1)-X).^2+(newdata(2)-Y).^2);

Sign in to comment.

Answer by Alex Mcaulley
on 12 Jul 2019

Edited by Alex Mcaulley
on 12 Jul 2019

As Bjorn Gustavsson said your code make no sense. To calculate distances you have to use the coordinates of each pixel, not mixing coordinates and intensity values. One option without loop:

a = imread('tumor.jpeg');

b = imresize(a,3);

c = im2bw(b);

imshow(c);

d = zeros(size(c));

%150,210

[ptx,pty] = ginput(1);

newdata = [ptx, pty];

n = 25;

%[count,x] = imhist(c);

[rows, columns] = size(c);

crows = repmat((1:rows)',1,columns);

ccolumns = repmat(1:columns,rows,1);

euc = sqrt((newdata(1) - crows).^2 + (newdata(2) - ccolumns).^2)

KSSV
on 12 Jul 2019

crows = repmat((1:rows)',1,columns);

ccolumns = repmat(1:columns,rows,1);

The above is not good. Use meshgrid

Alex Mcaulley
on 12 Jul 2019

Why is not good? The code works and the execution time is similar than using meshgrid (In fact, my code results in a slightly better performance in my computer).

%5 different executions

nx = 512;

ny = 512;

timeit(@() meshgrid(1:nx,1:ny))

timeit(@() repmat((1:nx)',1,ny)) + timeit(@() repmat(1:ny,nx,1))

%1st

ans =

0.0013

ans =

0.0011

%2nd

ans =

0.0015

ans =

0.0011

%3rd

ans =

0.0014

ans =

0.0012

%4th

ans =

0.0014

ans =

0.0012

%5th

ans =

0.0013

ans =

0.0013

Sign in to comment.

Answer by Ahsen Feyza Dogan
on 12 Jul 2019

Since it is distance, there is no x and y coordinate. There shoud be 278784 elemnt from 528*528

Bjorn Gustavsson
on 12 Jul 2019

Yeah, about that. You have one selected point, lets call that one r_slected and an image, lets call that one Img. Now, from each image pixel you want the distance to r_selected. You get that with KSSV's second suggestion, or Alex's, lets repeat it here:

[ny,nx] = size(Img);

[X,Y] = meshgrid(1:nx,1:ny);

l2slected_point = sqrt((r_selected(1)-X).^2+(r_selected(2)-Y).^2);

That will make l2selected_point a double array of size 528 x 528 (or whatever size your image has), which is your 278784 distances - but you will have them in an order that is sensibly arranged to be of the same size as your image, which will be crucial for further use.

Try this code and before doing anything more display the result:

imagesc(l2selected_point),colorbar

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Bjorn Gustavsson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471403-for-loop-assign-variable#comment_723760

Sign in to comment.