How to erase black letters in an image
6 views (last 30 days)
Show older comments
I have an image like this:
Is it possible to erase these letters conserving the background?
0 Comments
Answers (2)
Akira Agata
on 8 Feb 2024
How about the following?
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1611496/image.jpeg');
Igray = rgb2gray(I);
BW = Igray < 10;
se = strel('disk', 10);
BW = imdilate(BW, se);
I2 = inpaintCoherent(I, BW);
imshow(I2)
2 Comments
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.
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;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
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);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
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';
drawnow;
%--------------------------------------------------------------------------------------------
% SPLIT INTO INDIVIDUAL COLOR CHANNELS SO WE CAN FIND BLACK.
[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);
imshow(mask)
impixelinfo;
axis('on', 'image');
title('Black Pixels', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------
% DO INPAINTING WITH REGIONFILL.
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);
imshow(rgbImage2)
impixelinfo;
axis('on', 'image');
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
2 Comments
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
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.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!