필터 지우기
필터 지우기

How can I extract the white pixels of an image?

조회 수: 17 (최근 30일)
Chen Zhu
Chen Zhu 2017년 3월 8일
답변: chandu priya 2019년 8월 27일
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!

채택된 답변

Image Analyst
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
Chen Zhu
Chen Zhu 2017년 3월 9일
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 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
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
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
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
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);

Community Treasure Hunt

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

Start Hunting!

Translated by