Clear Filters
Clear Filters

How to erase black letters in an image

2 views (last 30 days)
I have an image like this:
Is it possible to erase these letters conserving the background?

Answers (2)

Akira Agata
Akira Agata on 8 Feb 2024
How about the following?
I = imread('');
Igray = rgb2gray(I);
BW = Igray < 10;
se = strel('disk', 10);
BW = imdilate(BW, se);
I2 = inpaintCoherent(I, BW);
Joan Carreras Díaz
Joan Carreras Díaz on 8 Feb 2024
Is there no possibility to not blur the image?
DGM on 27 May 2024
I would assert that the image is not blurred. Zero image content outside the text region is altered in any way. All the original information which was covered by the text is gone permanently. That portion of the original image can't be blurred, because it does not exist.
Regardless of whether you use regionfill(), inpaintCoherent(), or inpaintExemplar(), the masked region must be estimated entirely from information outside the mask. Speaking loosely, what you get is a more or less smooth interpolation across the mask boundaries. Any small interior details that were obscured by the text won't be replicated unless there is adequate evidence of them outside the mask, and said evidence exists in a form well suited to the chosen inpainting algorithm.

Sign in to comment.

Image Analyst
Image Analyst on 27 May 2024
Try something like this
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
markerSize = 20;
folder = [];
baseFileName = 'skier.jpeg';
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);
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
[redImage, greenImage, blueImage] = imsplit(rgbImage);
threshold = 2; % Adjust as necessary.
mask = (redImage <= threshold) & (greenImage <= threshold) & (blueImage <= threshold);
% Dilate it one pixel layer
mask = imdilate(mask, true(11));
subplot(2, 2, 2);
axis('on', 'image');
title('Black Pixels', 'FontSize', fontSize, 'Interpreter', 'None');
redImage = regionfill(redImage, mask);
greenImage = regionfill(greenImage, mask);
blueImage = regionfill(blueImage, mask);
% Reconstruct the RGB image.
rgbImage2 = cat(3, redImage, greenImage, blueImage);
% Display the image
subplot(2, 2, 3);
axis('on', 'image');
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
DGM on 29 May 2024
Edited: DGM on 29 May 2024
Considering OP's objection to "blurring", regionfill() will probably have the strongest tendency to create unnaturally smooth transitions across the mask boundaries. In this case, the mask threshold could stand to be higher (e.g. Akira used 10), or the strel radius wider, since it's fairly clear that the halo of JPG artifacts around the text are not being adequately removed from the image. Consequently, the inpainted region is skewed heavily to match the darker artifact colors at the mask boundary.
To be fair, I strongly appreciate regionfill()'s relative simplicity and speed compared to the other IPT inpainting tools. It's a very significant difference in terms of execution time, and for a simple nontechnical exercise like this, it's probably totally adequate -- if we trust to disregard OP's unelaborated objection.
Image Analyst
Image Analyst on 29 May 2024
Yeah, inpaintCoherent is probably better and more sophisticated. I didn't know about it (or had forgotten about it) - it was introduced in 2019. The original poster forgot to enter their release, or chose not to include it for some reason, when they posted.
Since the original image is JPEG, you can't be super concerned about image quality. JPEG images have so many compression artifacts that the region filling may not look much different, particularly in relatively smooth areas like most of this image. On the other hand, I've used the "bandaid" tool in Photoshop quite a bit and while it can be quite good and smart about what nearby regions it tries to use to figure out the color and texture to use, but sometimes it puts in a region that looks like it was sampled from an totally inappropriate nearby region and it looks really weird and maybe a simply smearing inwards (like regionfill) might have been better.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!