How can I extract the white pixels of an image?
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2017-3-8
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 个评论
Image Analyst
2017-3-9
Pick whatever you want. That value seemed to give a white spot about the size of what you wanted. Higher thresholds give smaller spots.
更多回答(2 个)
John Chilleri
2017-3-8
编辑:John Chilleri
2017-3-8
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 个评论
John Chilleri
2017-3-8
编辑:John Chilleri
2017-3-8
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
2017-3-8
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
2019-8-27
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!