Vectorize getting the intensity values from greyscale image

1 vue (au cours des 30 derniers jours)
Aon
Aon le 26 Juin 2019
Commenté : Aon le 27 Juin 2019
Hi, im trying to vectorize this piece of code to increase calculation speed, y1, y2, y3 is 1288x1 arrays with different y-indexpoints in a image matrix:
for xNr = 1:1288
intensity1(xNr) = image(y1(xNr), xNr); % Find intensity.
intensity2(xNr) = image(y2(xNr), xNr); % Find intensity.
intensity3(xNr) = image(y3(xNr), xNr); % Find intensity.
end
Then I get 3 arrays with dimension 1288x1 with intensity values.
And when I try to vectorize:
xNr = (1:1288);
intensity1 = image(y1, xNr); % Find intensity.
intensity2 = image(y2, xNr); % Find intensity.
intensity3 = image(y3, xNr); % Find intensity.
I get 3 arrays with dimension 1288x1288 which is wrong.
Can this be vectorized so I get the same result as in the for-loop?
Thanks in advance
  2 commentaires
KALYAN ACHARJYA
KALYAN ACHARJYA le 26 Juin 2019
Modifié(e) : KALYAN ACHARJYA le 26 Juin 2019
Please clarify y1, y2 ?? or share the complete code
Aon
Aon le 26 Juin 2019
y1, y2, y3 is arrays which stores 1288 y-values of the image matrix.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 26 Juin 2019
In the little snippet of code you gave us, you don't show how intensity1 is initialised. If it is not initialised properly, then your loop will indeed be very slow as you're growing the array as you construct it.
intensity1 = zeros(1, 1288); %preallocate intensity1 instead of growing it in the loop
for xNr = 1:1288
intensity1(xNr) = image(y1(xNr), xNr); % Find intensity.
end
The loop can of course be eliminated. You have to convert your 2D indexing into linear indexing with sub2ind:
intensity1 = image(sub2ind(size(image), y1, 1:1288));
Note that you should avoid numbered variables or variables named in any form of sequence. The sequential naming is a clear indication that these variables belong together in a single variable. In this case your y1, y2, y3 should be single 2D variable. Assuming they're row vectors:
y = [y1; y2; y3]; %don't use numbered variables
intensity = image(sub2ind(size(image), y, repmat(1:1288, 3, 1)));
Finally, note that using image as a variable name is not a good idea as it prevents you from using the image function with the same name.
  3 commentaires
Guillaume
Guillaume le 27 Juin 2019
And how long does it take? It should be very quick:
>> im = rand(1288); %actual size and content of matrix doesn't matter
>> yValuesCenterHorizontalLine = randi(1288, 1288, 1); %actual values don't matter
>> xNr = 1:1288;
>> timeit(@() im(sub2ind(size(im), xNr(:), yValuesCenterHorizontalLine)))
ans =
2.3783e-05
>> timeit(@() im(sub2ind(size(im), xNr(:), yValuesCenterHorizontalLine)))
ans =
1.1473e-05
>> timeit(@() im(sub2ind(size(im), xNr(:), yValuesCenterHorizontalLine)))
ans =
2.0707e-05
>> timeit(@() im(sub2ind(size(im), xNr(:), yValuesCenterHorizontalLine)))
ans =
1.0618e-05
>> timeit(@() im(sub2ind(size(im), xNr(:), yValuesCenterHorizontalLine)))
ans =
1.0548e-05
As you can see it takes between 10 and 20 microseconds on my computer. Isn't that fast enough for you. Are you sure that it's the slow part of your code?
Aon
Aon le 27 Juin 2019
For me it is approx 40 microsec. No I have much more , but what I know you should have as few for-loops as you can because it is a bottleneck in program speed. But I will take a close look at the rest of the program to see what is most time consuming.
Is the Profiler the right tool to use?
Many thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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