Remove small blobs and keep letters

4 views (last 30 days)
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 Comments
Zoe
Zoe on 12 Jan 2023
@DGM I want to remove both printed and handwritten text. Thanks.
Zoe
Zoe on 12 Jan 2023
@Walter Roberson Thanks, I will try that property.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Jan 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 Comments
Zoe
Zoe on 13 Jan 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?
Image Analyst
Image Analyst on 13 Jan 2023
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?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!