How do I remove the unwanted white part of the grayscale image?

I used the camera to collect a series of images from a line laser hitting a small rotating metal ball and processed them accordingly. Now I only want to keep the images that belong to the outline of the blob, I want to remove the other invalid white parts, how can I achieve this? The parts circled by the red pen are the ones I want to remove.

 Risposta accettata

I already gave you the answer to that in your duplicate question:
Basically use a mask.

6 Commenti

I don't quite understand, can you please explain a little how to use this function? Thank you very much!
Presumably you have some kind of jig where your camera is bolted down and your desired thing (the ball) is always in the same place in your image. In that case you can simply make up a mask and then multiply it, or use it as an index, to erase the parts of the image not close to the curvature of the ball.
grayImage(~mask) = 0; % Erase gray scale image outside of mask.
edgeImage(~mask) = false; % or you can do it after segmentation this way.
If you have any more questions, then attach your original gray scale image and code to read it and process it with the paperclip icon after you read this:
I tried to write the code to create the mask, but it didn't work, can you check it for me? Thanks a lot!
grayImage = imread('001.png');
%
mask = grayImage > 30;
props = regionprops(logical(mask), 'Area');
allAreas = sort([props.Area]);
%
mask = bwareaopen(mask, 25);
mask = imclearborder(mask);
%
maskedgrayImage = grayImage;
maskedGrayImage(~mask) = 0;
imshow(maskedGrayImage);
This will do it. Just make sure you have a template/mask that covers where the curvature of the ball is supposed to be.
% Read in edge detected image.
grayImage = imread('001.png');
if size(grayImage, 3) > 1
% Convert from RGB to gray scale.
grayImage = grayImage(:,:,1);
end
subplot(3, 1, 1);
imshow(grayImage);
title('Original Image', 'FontSize', 20)
% Read in mask template
mask = imread('sterne mask.png') > 0;
subplot(3, 1, 2);
imshow(mask);
title('Mask', 'FontSize', 20)
% Erase image outside mask
grayImage(~mask) = 0;
subplot(3, 1, 3);
imshow(grayImage);
title('Masked Image', 'FontSize', 20)
You can see by using the mask you can erase everything outside the region where you want to operate.
Thank you very, very much for your help!

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 7 Gen 2023
Spostato: Walter Roberson il 7 Gen 2023
dilate the image to join close groups. bwareafilt to keep the largest only. regionprops to find the edge trace. Use that to extract from the un-dilated image. (I was going to suggest bounding box instead of edge trace but bounding box can have problems with accidentally including additional sections from another branch that happen to curve through the same rectangle.)

Community Treasure Hunt

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

Start Hunting!

Translated by