MATLAB: How to erode parts of an image

erosionimage analysisimage processingMATLAB

Does anyone know the best way to get rid of the black part on the left side of the image and the black border around the white. I need to keep the numbers and letters. I tried different types of imerode, but could never get rid of it. Any pointers would be great thank you!NeedtobeEroded.jpg

Best Answer

This works without changing the shape of the "good" characters, like erosion would do:
grayImage = imread('NeedtobeEroded.jpeg');
if ndims(grayImage) > 1
% It's RGB - convert to gray scale.

grayImage = grayImage(:,:,1);
end
subplot(2, 2, 1);
imshow(grayImage);
binaryImage = grayImage > 128; % Convert to binary.

% Get rid of largest blob.

mask = bwareafilt(~binaryImage, 1);
binaryImage(mask) = true;
subplot(2, 2, 2);
imshow(binaryImage);
% Get aspect ratios so we can find the tall, narrow vertical line at the right.
props = regionprops(~binaryImage, 'BoundingBox', 'PixelIdxList');
bb = vertcat(props.BoundingBox)
aspectRatios = bb(:, 4) ./ bb(:, 3) % Heights over widths
% Find aspect ratios more than, say, 50.
badRegions = aspectRatios > 50;
% Fill them in
for k = 1 : length(props)
if badRegions(k)
% It's bad. Fill this one in.
binaryImage(props(k).PixelIdxList) = true;
end
end
subplot(2, 2, 3);
imshow(binaryImage);
%------------------------------------------------------------------------------

% Set up figure properties:

% Enlarge figure to full screen.

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
Another way to do it rather than getting rid of blobs with bad aspect ratios is, if you know the bad line is always there and always at the right side, you can simply fill in the rightmost blob.
grayImage = imread('NeedtobeEroded.jpeg');
if ndims(grayImage) > 1
% It's RGB - convert to gray scale.
grayImage = grayImage(:,:,1);
end
subplot(4, 1, 1);
imshow(grayImage);
binaryImage = grayImage > 128; % Convert to binary.
% Get rid of largest blob.
largestBlob = bwareafilt(~binaryImage, 1);
binaryImage(largestBlob) = true;
subplot(4, 1, 2);
imshow(binaryImage);
% Get rid of rightmost blob.
[labeledImage, numBlobs] = bwlabel(~binaryImage);
rightMostBlob = ismember(labeledImage, numBlobs);
subplot(4, 1, 3);
imshow(rightMostBlob);
binaryImage(rightMostBlob) = true;
subplot(4, 1, 4);
imshow(binaryImage);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
0000 Screenshot.png