How to loop on a data cube of an image

Hi, I'm trying to calculate gradient at each pixel in a data cube of an image an i'm getting this error "index out of bounds because size(X)=[280,307,191]."
Any help would be much appreciated Thank you

2 件のコメント

Walter Roberson
Walter Roberson 2013 年 4 月 7 日
We are going to need to see some of your code.
Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
編集済み: Walter Roberson 2013 年 4 月 8 日
Okay,here's my code below:
close all
Bands =191; %no of bands
pixels =280*307; %no of pixels
SIZE = [280,307 ,191]; %WxHxBands
lam = 0.063;
B=12;
X0 = multibandread('dc.tif', SIZE, 'int16',1252*2, 'bsq', 'ieee-le');
%adding noise
std_n=40; var_noise=std_n^2; % Gaussian noise standard deviation
reduced_pw = 1.5*var_noise; % power to reduce in first phase
std_n=sqrt(var(X0(:)));
Xn = randn(size(X0))*std_n; %noise
X = X0 + Xn; %Noisy Image
%looping on datacube
x = (X(1,:,1));
y = (X(:,1,1));
bands = X(1,1,:);
for i = 1:1: x
for j = 1:1: y
for k = 1 :1:bands
current_grad(i,j,k) = gradient(i,j,k);
end
end
end

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

 採用された回答

Image Analyst
Image Analyst 2013 年 4 月 7 日

0 投票

Somewhere in your code where you're indexing x, y, and z, the value(s) of at least one of those indexes is greater than 280, 307, and 191, respectively.

2 件のコメント

Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
Okay and how to fix this? Thank you
Image Analyst
Image Analyst 2013 年 4 月 8 日
編集済み: Image Analyst 2013 年 4 月 8 日
You will be using the debugger. Go here if you don't know how yet: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 8 日

0 投票

You establish x, y, and bands according to the content of the noisy image, but then your triple nested for loop has you looping using those values as indices, such as in
for i = 1:1: x
Question: in your line
current_grad(i,j,k) = gradient(i,j,k);
is "gradient" the gradient function? If so then why are you applying the function to the indices i, j, k, rather than to the content of the image? And does it make sense to be applying the gradient function only to a point at a time?

8 件のコメント

Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
i need to calculate the gradient at each pixel of the image in all bands and put it in a matrix which is the same size of the image X so i thought looping on all 3 dimensions of the image would do so? What should i do to get it right?
Image Analyst
Image Analyst 2013 年 4 月 8 日
Then why are you not using imgradient() or imgradientxy() on each spectral band?
Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
i tried using it but it's not defined in Matlab, all i could find it "gradient" function Is it related to specific version of Matlab?
Walter Roberson
Walter Roberson 2013 年 4 月 8 日
imgradient() is part of the Image Processing Toolkit. That toolkit is included with Student Version licenses but is not installed by default.
Image Analyst
Image Analyst 2013 年 4 月 8 日
You must have a really old version. Does your license include upgrades? If so, do it.
Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
I think yes,would you help me do it pls cause i'm a bit new to matlab Thank you
Walter Roberson
Walter Roberson 2013 年 4 月 8 日
Which MATLAB version are you using? And are you using the Student license?
Image Analyst
Image Analyst 2013 年 4 月 8 日
I cannot help you do the upgrade. You'll have to contact the Mathworks to do that.

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

Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
編集済み: Walter Roberson 2013 年 4 月 8 日

0 投票

I've tried this:
x= squeeze(X(1,:,1));
y =squeeze(X(:,1,1));
b = X(1,1,:);
for i=1:x
for j =1 : y
for k = 1 :191
G = gradient(1:i,1:j,1:k);
end
end
end
but it keeps telling me 'Index exceeds matrix dimensions'

2 件のコメント

Walter Roberson
Walter Roberson 2013 年 4 月 8 日
You establish x, and y according to the content of the noisy image, but then your triple nested for loop has you looping using those values as indices.
Also, you are overwriting G in each iteration of the triple-nested loop.
Rawan hamdi
Rawan hamdi 2013 年 4 月 8 日
Okay , could you help me fix the code?

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

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by