How can I extract the white pixels of an image?
19 views (last 30 days)
Show older comments
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
Accepted Answer
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
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.
More Answers (2)
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
John Chilleri
on 8 Mar 2017
Edited: 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
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.
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!