for loop, assign variable

9 次查看(过去 30 天)
Ahsen Feyza Dogan
Ahsen Feyza Dogan 2019-7-12
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
  1 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2019-7-12
Your newdata are a selected point in the image, while c is the intensity (grayscale) of your pixels. This looks like a bug to me. I guess that you either want the length between th image pixels and your selected pixel, that would be norm(newdata-[j,i]), or the grayscale difference between the pixels and the pixel closest to newdata, that would be c(round(newdata(2)),round(newdata(1)))-c(i,j).

请先登录,再进行评论。

回答(3 个)

KSSV
KSSV 2019-7-12
编辑:KSSV 2019-7-12
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);
  3 个评论
KSSV
KSSV 2019-7-12
YOur c is a matrix......a point subtracted from matrix..will give you a matrix. How it will be of only size 528?
KSSV
KSSV 2019-7-12
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);

请先登录,再进行评论。


Alex Mcaulley
Alex Mcaulley 2019-7-12
编辑:Alex Mcaulley 2019-7-12
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)
  2 个评论
KSSV
KSSV 2019-7-12
crows = repmat((1:rows)',1,columns);
ccolumns = repmat(1:columns,rows,1);
The above is not good. Use meshgrid
Alex Mcaulley
Alex Mcaulley 2019-7-12
编辑:Alex Mcaulley 2019-7-12
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

请先登录,再进行评论。


Ahsen Feyza Dogan
Ahsen Feyza Dogan 2019-7-12
Since it is distance, there is no x and y coordinate. There shoud be 278784 elemnt from 528*528
  1 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2019-7-12
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

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by