How can I extract the white pixels of an image?

I want to get some help to extract the white part of an image.
For example, in this image, I want to extract the area shown in the red retangular.
I want the area to be substracted is at least 20 *20pixel width and length, like shown in the example above. It means that any small white areas, such as a single white point, is ignored.
Can I get some help?
Thanks a lot!

 Accepted Answer

Simply threshold to create a binary image of where "white" is. Then call beareaopen() to remove blobs smaller than a certain size. Then call regionprops() to get the bounding box of the blob you want to extract. Then call imcrop() to cut it out and display it separately. See attached code, below this image it creates:

2 Comments

Thanks for your help! Your code works well!
I have a question. Why you choose 200 as the threshold for each channel? Is 200 representing white?
Thanks!
Pick whatever you want. That value seemed to give a white spot about the size of what you wanted. Higher thresholds give smaller spots.

Sign in to comment.

More Answers (2)

Hello,
Assuming your image is of size m x n x 3, where the stored values range up to 255, you could use the following simple approach:
% Assume img is your m x n x 3 image
img = double(img);
[m, n, ~] = size(img);
% Step one, constrain to lower left, as we don't want to lose her shoes or the upper left:
m1 = round(2*m/3);
n1 = round(1*n/3);
submat = img(m1:m,1:n1,:); % fit this accordingly, I just guestimated
% Determine location of white pixels using threshold and change:
for i = 1:size(submat,1)
for j = 1:size(submat,2)
if (sum(submat(i,j,:)) > 3*240)
% White pixel - do what you want to original image
img(m1-1+i,j,:) = [0 0 0]; % make it black, for example
end
end
end
img = uint8(img);
Using the above code on a screen shot of your image with a threshold of 240, I get this:
Note: the image is cut off because I snapped a quick screen shot that wasn't carefully cropped.
There are probably much better methods, but this is a simple approach.
Hope this helps!

3 Comments

Thanks for your help! I want my code to work for any pictures, so there is no need to set the constrain at step one.
I tried to change the code to
for i = 1:size(img,1)
for j = 1:size(img,2)
if (norm(img(i,j,:)) > 240)
% White pixel - do what you want to original image
img(i,j,:) = 2;
end
end
end
However, I got the error: Undefined function 'norm' for input arguments of type 'uint8'.
In addition, I want to extract the white areas from the image. I want the area to be substracted is at least 20 *20pixel width and length, like shown in the example above.
Thanks!
Finally finished editing my code, sorry for the delay, tell me how things work now!
I find that a threshold of 3*210 seems to get a majority of the reflection (if not too much):
In order to extract an area, you could make the submat = img, and set equal to black if less than threshold, else set to white. Then you have a white image with black splotches that you can then select (if they're big enough) to extract.

Sign in to comment.

img = double(img);
[m, n, ~] = size(img);
% Step one, constrain to lower left, as we don't want to lose her shoes or the upper left:
m1 = round(2*m/3);
n1 = round(1*n/3);
submat = img(m1:m,1:n1,:); % fit this accordingly, I just guestimated
% Determine location of white pixels using threshold and change:
for i = 1:size(submat,1)
for j = 1:size(submat,2)
if (sum(submat(i,j,:)) > 3*240)
% White pixel - do what you want to original image
img(m1-1+i,j,:) = [0 0 0]; % make it black, for example
end
end
end
img = uint8(img);

Community Treasure Hunt

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

Start Hunting!