MATLAB Answers

How can I extract the white pixels of an image?

85 views (last 30 days)
Chen Zhu
Chen Zhu on 8 Mar 2017
Answered: chandu priya on 27 Aug 2019
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!

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 8 Mar 2017
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

Chen Zhu
Chen Zhu on 9 Mar 2017
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!
Image Analyst
Image Analyst on 9 Mar 2017
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)

John Chilleri
John Chilleri on 8 Mar 2017
Edited: John Chilleri on 8 Mar 2017
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

Chen Zhu
Chen Zhu on 8 Mar 2017
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!
John Chilleri
John Chilleri on 8 Mar 2017
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):
John Chilleri
John Chilleri on 8 Mar 2017
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.


chandu priya
chandu priya on 27 Aug 2019
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);

  0 Comments

Sign in to comment.