Need help to seperate the text from the background

7 views (last 30 days)
I need help to find the intensity treashold that will help me seperate the background from the text in this greyscale image.
i need to have the text in white and the backouground in black.
  2 Comments
Rik
Rik on 2 Oct 2021
Since your image is lighter and darker than the text, this cannot be done with a single threshold. The noise looks it is intended to be in the same intensity range as the text.
What other techniques were you taught in your course?
Julien Roussel
Julien Roussel on 2 Oct 2021
Edited: Julien Roussel on 2 Oct 2021
the only method I learned so far is using a threshold but because the text and part of the backgroup are all dark im not able to accomplish what im asked..
what technique do you think i should be using?

Sign in to comment.

Accepted Answer

DGM
DGM on 3 Oct 2021
Thresholding might be part of it, but it'll take a lot of other work to make the masks into something useful.
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/756529/image3.png');
B = medfilt2(A,[3 3]);
% denoise
m = abs(double(A)-double(B)) > 10;
C = A;
C(m) = B(m);
% try to extract text
mtx = (C>70) & (C<180);
% try to get rid of BG transition bands
mtr = rangefilt(B,ones(5)) > 180;
mtr = bwareafilt(mtr,3);
mtx = mtx & ~mtr;
% clean up mask
%mtx = despeckle(mtx,20); % MIMT-only
mtx = ~bwareaopen(~bwareaopen(mtx,20),20);
%subplot(1,2,1)
imshow(A)
%subplot(1,2,2)
clf % just to force the web-display to show both images at a reasonable size
imshow(mtx)
It's hardly perfect, but I imagine it's better than what's expected.

More Answers (1)

Image Analyst
Image Analyst on 2 Oct 2021
Edited: Image Analyst on 3 Oct 2021
Thresholding will work with this image. Do something like
mask = grayImage > gl1 & grayImage < gl2;
You decide on the gray levels, like gl1 is 100 and gl2 is 200 or whatever. It looks like the noise is salt and pepper noise (demo attached) with values of 0 and 255 so you probably won't have any noise in the mask image.
% 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 = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'image3.png';
grayImage = imread(fileName);
% 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.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
grayImage(grayImage == 1) = 255;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram of Image Gray Levels', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Blob Area', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image.
lowThreshold = 130;
highThreshold = 200;
% Interactively threshold with Image Analyst's interactive thresholding utility.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find out areas in initial mask to find outliers.
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
subplot(2, 2, 4);
histogram(allAreas);
grid on;
title('Histogram of Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Blob Area', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of small blobs less than 20 pixels in area.
mask = bwareaopen(mask, 20);
% Get rid of the slanted line.
verticalProfile = sum(mask, 2);
rowsToErase = verticalProfile <= 3;
mask(rowsToErase, :) = false; % Erase the rows with the slanted line.
% Get rid of small blobs less than 20 pixels in area.
mask = bwareaopen(mask, 20);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find out areas in final mask.
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
subplot(2, 2, 4);
histogram(allAreas);
grid on;
title('Histogram of Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Blob Area', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
The letter shapes are pretty accurate except for those places where the diagonal line intersects them. You can get rid of those with some more work -- try it.

Categories

Find more on Images 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!