Obtain specific pixel values without FOR loop..?

2 views (last 30 days)
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));
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.

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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by