Vectorize getting the intensity values from greyscale image
4 views (last 30 days)
Show older comments
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 Comments
KALYAN ACHARJYA
on 26 Jun 2019
Edited: KALYAN ACHARJYA
on 26 Jun 2019
Please clarify y1, y2 ?? or share the complete code
Accepted Answer
Guillaume
on 26 Jun 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 Comments
Guillaume
on 27 Jun 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?
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!