Function inside for loop not working

5 vues (au cours des 30 derniers jours)
Peter Hinderberger
Peter Hinderberger le 5 Nov 2020
Commenté : Steve Eddins le 6 Nov 2020
Hi,
I am trying to iterate the diagonal filling of a previously defined, 0 valued hypercube object with a for loop of the following form:
EDIT - REMAINING CODE:
% Load and adjust test image, convert to .png (probably unnecessary)
addpath('D:\Users\Peter\Desktop\Masterarbeit\Messdaten\Bilder_Einfach');
img = imread('BspBild_1.jpg');
imwrite(img, 'BspBild.png');
img = double(rgb2gray(imread('BspBild.png')));
img = rescale(img);
sz = size(img);
% Create empty hypercube
zerocube = zeros(detformat_y, 2*detformat_x, n_lambda);
hcube = hypercube(zerocube, wavelengths);
% Add image to the first band
hcube = assignData(hcube, 1:411, detformat_x/2+1:640+detformat_x/2, 1, img);
% Show image as first hypercube band (for testing reasons)
figure(2)
imshow(hcube.DataCube(:, :, 1))
title('Cube-Ausschnitt')
% loop to "shear" image in the wavelength direction EDIT - END
for k = 1:n_lambda % n_lambda is the number of bands
temp = img(:, k); % img is the monochromatic image that should fill a diagonal line
hcube = assignData(hcube, :, k, k, temp);
end
the function assignData is defined in the Image Processing Toolbox Hyperspectral Imaging Library and works fine outside the for loop.
Whenever I try to execute the code it gives me "Unrecognized function or variable 'assignData'." in the line with this function.
Since directly indexing inside the hcube.DataCube object is not possible this function seems to be the only option to change hyperspectral data.
  1 commentaire
Geoff Hayes
Geoff Hayes le 5 Nov 2020
Peter - can you show more of your code? It isn't clear why calling this function within the loop would fail whereas calling it outside the loop would succeed.

Connectez-vous pour commenter.

Réponse acceptée

Steve Eddins
Steve Eddins le 5 Nov 2020
One possible explanation for the observed behavior is that the variable hcube does not actually contain a hypercube object when the call to assignData happens inside your for-loop. Since we don't see the code before the for-loop, I can't really guess at why that might be. I recommend that you use the debugger and single-step through your code, making sure that the variables contain what you think they should contain at each step.
Also, does your real code include the single quotes around the colon character in the call to assignData? I don't this code will work as it is written here, without the single quotes.
  2 commentaires
Peter Hinderberger
Peter Hinderberger le 6 Nov 2020
Modifié(e) : Peter Hinderberger le 6 Nov 2020
The line to test if there is image data in the hcube.DataCube object works fine, and i can also view it in the hyperspectral viewer.
You're right - there should be quotes around the character data according to the help center. It didn't show this error again, just takes eternally long. But why do i need those quotes here? They are not needed when i index into it via "x:y" as in a normal array, but they are if i only use ":" ?
This for loop takes far too long, but i don't know how to do it faster. Has somebody an idea?
I have to do this for many images to create the object to work with, but waiting for hours will probably be a problem.
In any case thanks a lot, i somehow knew that it has to be such a small thing, but i wouldn't have expected to need those thingies when indexing :)
Steve Eddins
Steve Eddins le 6 Nov 2020
The colon character, by itself, can only be used as an array index. In this code:
hcube = assignData(hcube, :, k, k, temp);
assignData is a function, not an array. You can't pass the colon character by itself as an argument to a function.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Hyperspectral Image Processing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by