How to remove text from an image?

9 views (last 30 days)
I am working on medical images. In most of the image contain patient details. While segmenting those images these texts are also segmented along with the region of interest. I want to remove those text from images before segmentation.

Accepted Answer

Image Analyst
Image Analyst on 1 Mar 2015
Presumably those details are not covering up the image data you're interested in. So then you can just assign those pixels to 0 or some other intensity:
grayImage(row1:row2:col1:col2) = 0;
Of course you need to know what rows and columns contain your annotation.
  4 Comments
Image Analyst
Image Analyst on 17 Jun 2017
Get rid of any blobs with a centroid within 10% of the edge of the image. This code works for both of your images:
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
% baseFileName = 'pat00002_1-1-1-1-2-10-1.mp4.cover.png';
baseFileName = '1-1-1-1-2-8-1 2.mp4.cover.png';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% Get 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(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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;
% Binarize the image by thresholding.
mask = grayImage > 17;
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;
% Find the areas
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'Area', 'Centroid');
allAreas = sort([props.Area], 'descend')
allCentroids = [props.Centroid];
centroidsX = allCentroids(1:2:end);
centroidsY = allCentroids(2:2:end);
% Make a margin of 10% of the image size.
marginx = 0.1 * columns;
marginy = 0.1 * rows;
keepers = (centroidsX > marginx & centroidsX < (columns - marginx)) & ...
(centroidsY > marginy & centroidsY < (rows - marginy))
indexes = find(keepers);
% Get a mask with only the keepers in it
newMask = ismember(labeledImage, indexes);
% Display the mask image.
subplot(2, 2, 3);
imshow(newMask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;
% Mask the gray scale image
maskedGrayImage = grayImage; % Initialize.
maskedGrayImage(~newMask) = 0;
% Display the masked grayscale image.
subplot(2, 2, 4);
imshow(maskedGrayImage);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Binary Image Mask', 'fontSize', fontSize);
title('Masked Gray Scale Image', 'fontSize', fontSize);
drawnow;
Imran
Imran on 10 Jan 2019
I have tried this code to my image named (Original) but it still leaves some unneceassary text. Output image named (Masked). Please give me code to remove it.

Sign in to comment.

More Answers (2)

divya d
divya d on 4 Dec 2018
Am an reserach scholar. i tried the above code to remove the unnecessary patient details from echo images. i attached the result. verify it and suggest some other code. plz repy.

Imran
Imran on 10 Jan 2019
I have tried your this code to my image named (Original) but it still leaves some unneceassary text. Output image named (Masked). Please give me code to remove it.
  3 Comments
Imran
Imran on 10 Jan 2019
Actually I have shared you gray sclaed output images of Matlab. I have attached the actual RGB image. Can i remove more text as compared to image named Masked using Matlab???
Image Analyst
Image Analyst on 10 Jan 2019
Actually, my answer stays unchanged. This is not the original image. You want the original image, not one with annotations imprinted on it. See if you can get the original images. They might be in dicom format which is used a lot in the medical field.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!