Remove small blobs and keep letters

I have a binary image after thresholding to filter out letters from the original image, as the attached. But many unwante small blobs are also retained. I tried bwareaopen, but it was hard to find a proper criterium of area to differentiate these small blobs from the letters. The goal is to fitlter out the text and remove them (both printed and handwritten text) from the image. Thanks.

9 comentarios

Walter Roberson
Walter Roberson el 11 de En. de 2023
I wonder if measuring their eccentricities would help? Or their density?
Zoe
Zoe el 11 de En. de 2023
By using regionprops with eccentricities?
Matt J
Matt J el 11 de En. de 2023
Editada: Matt J el 11 de En. de 2023
regionprops or better yet, bwpropfilt. And maybe with Solidity in addition to Eccentricity. But it would probably be better to go back to the pre-binarized image to see if additional characterisitics can be exploited.
Walter Roberson
Walter Roberson el 11 de En. de 2023
"Solidity" is a better property than where I said "density" (by which I meant something fairly similar but did not know quite how to implement it at the time.)
Image Analyst
Image Analyst el 11 de En. de 2023
Perhaps the letters are on a colored sticker or something. We don't know because you forgot to attach your original image. All you attached was your segmented image. Obviously your segmentation algorithm is not complete but we don't know how to improve or finish it because you forgot to attach the original image. It might be easier for us to start with the original image than try to repair your imperfect segmentation algorithm starting from the bad image.
Also say what you really want. Is it the letters? Or the blobs? What do you want to keep for further analysis, and what do you want to discard/ignore?
DGM
DGM el 11 de En. de 2023
Are we talking about the printed text or the handwritten text?
If you're trying to process many varied images, it would help to have examples of more than one.
Zoe
Zoe el 12 de En. de 2023
Hi @Image Analyst, I have attached the original image. I would like to filter out the printed and handwritten text and remove them from the image. Thanks.
Zoe
Zoe el 12 de En. de 2023
@DGM I want to remove both printed and handwritten text. Thanks.
Zoe
Zoe el 12 de En. de 2023
@Walter Roberson Thanks, I will try that property.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 12 de En. de 2023
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'zoe.JPG';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%--------------------------------------------------------------------------------------------------
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
% Next create the circle in the image.
centerX = columns/2;
centerY = rows/2;
radius = 520;
mask = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
% Display image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis on;
title('Binary image of a circle', 'FontSize', fontSize, 'Interpreter', 'None');
% Mask image by multiplying each channel by the mask.
maskedRgbImage = rgbImage .* cast(mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.
% Display image.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
impixelinfo;
axis on;
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get a mask for the dark blue letters
[blueMask,dummy] = createMask(maskedRgbImage);
% Enlarge it a bit to help with regionfilling
se = strel('disk', 4, 0);
blueMask = imdilate(blueMask, se);
% Display image.
subplot(2, 2, 4);
imshow(blueMask, []);
impixelinfo;
axis on;
title('Blue Letter Mask', 'FontSize', fontSize, 'Interpreter', 'None');
% Need to fill them in
[r, g, b] = imsplit(maskedRgbImage);
r = regionfill(r, blueMask);
g = regionfill(g, blueMask);
b = regionfill(b, blueMask);
% Create RGB image again
maskedRgbImage = cat(3, r, g, b);
% Crop image
props = regionprops(mask, 'BoundingBox');
maskedRgbImage = imcrop(maskedRgbImage, props.BoundingBox);
% Display image.
figure
imshow(maskedRgbImage, []);
impixelinfo;
axis on;
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
%=========================================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 12-Jan-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.581;
channel1Max = 0.710;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.237;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.596;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

2 comentarios

Zoe
Zoe el 13 de En. de 2023
@Image Analyst Thank you so much. Is there any way to automatically detection the threshold for the printed text in side the round region of interest? Also, if the smaller round region (the one that the printed text overlaid) is not in the middle of image, how to determine its centroid point automatically?
If the circle is not centered in the image you can find the circle by finding non-zero pixels and getting the centroid.
mask = bwareafilt(grayImage > 0, 1);
props = regionprops(mask, 'Centroid');
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
You'd have to work a bit to find the color segmentation for the letters. Of course it's best to avoid them in the first place. Try really hard to get samples where those letters do not cover the image. For example, can you take off the lid and replace it with a clear lid? Or have them print stuff on the edge of the thing?

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

Zoe
el 11 de En. de 2023

Comentada:

el 13 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by