Vectorize getting the intensity values from greyscale image
조회 수: 1 (최근 30일)
이전 댓글 표시
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
KALYAN ACHARJYA
2019년 6월 26일
편집: KALYAN ACHARJYA
2019년 6월 26일
Please clarify y1, y2 ?? or share the complete code
채택된 답변
Guillaume
2019년 6월 26일
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
Guillaume
2019년 6월 27일
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?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!