Obtain specific pixel values without FOR loop..?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a binary image 'image_map' which contains either zeros that represent the background, and ones that represent the object... or vice versa. What I would like to do now is work out whether it is the zeros or ones that represent the background.
Since the background is likely to be more uniform (less detail) I thought to use the standard deviation of all grayscale pixel values that are represented by a 1 (I am using 'image_map' just to provide the correct coordinates). I will then repeat this process for the zeros, and the answer with the lowest standard deviation can be selected as the background... However, the way I have programmed this does not seem to be efficient.
Is there a way to achieve the following without using a FOR loop? The processing time is extremely vast if I try to do this as below:
[pix_y pix_x] = find(image_map == 1);
for i=1:length(pix_y)
object1(i) = gray_image(pix_y(i),pix_x(i));
end
Also, if anyone can think of any better way of doing this (I'm sure there will be some), then I would be very happy to hear your suggestions.
Thanks!
댓글 수: 0
답변 (1개)
Walter Roberson
2011년 5월 8일
Replace the code with:
object1 = gray_image(image_map == 1);
One of your problems was that you were not preallocating object1. What you had would have been faster if you had put
object1 = zeros(size(pix_y));
before the "for" loop.
댓글 수: 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!