Obtain specific pixel values without FOR loop..?

6 views (last 30 days)
Philip
Philip on 8 May 2011
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!

Answers (1)

Walter Roberson
Walter Roberson on 8 May 2011
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.

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!