Why OCR cannot recognize image with black background color?

6 views (last 30 days)
I'm new to Matlab and this is my first time on doing a project using Matlab and image processing. I'm currently in the midst of finishing my fyp project related to OCR which allows partially blind user to snap a picture and extract text in the image. My code is running properly, however it cannot recognize text from image with black background color. Why is it happening?
  2 Comments
Joseph Cheng
Joseph Cheng on 9 Jul 2015
you really need to expand on your question and attach any code if you want an answer. what you have right now sums up as "My code doesn't work, why doesn't my code work?" There is not much to go on for anyone to help you besides asking you how are you distinguishing text vs background?
Intan Nurulhashikin Mohd Yunus
Thanks for ur respond sir. Basically this is my code. Supposedly this program will load image from any folder and extract the text from the image. However, as I've test this coding I've found out that this program cannot recognize text from an image with black background color. Could u have any ideas why this happen? Thank you.
warning off %#ok<WNOFF> % Clear all clc, close all
% Get file from folder [filename, pathname] = uigetfile('*','LOAD AN IMAGE'); %fullimageFileName=fullfile(folder,baseFileName);
% Read image Image=imread(fullfile(pathname,filename));
% Show image figure,imshow(Image) title('ORIGINAL IMAGE');pause(3)
% NOISE REMOVAL - Pre Processing % 1- Gaussian Smoothing Image2 = imgaussfilt(Image,2); figure,imshow(Image2) title('GAUSSIAN APPLIED');pause(3)
%Adjust image intensity 88 PreP1 = imadjust(Image2,[.3 0.5],[]); figure, imshow(PreP1) title('IMAGE INTENSITY ADJUSTED');pause(3)
% 3- Convert to Grayscale if size(PreP1,3)==3 % check if its RGB image convert to grayscale PreP1=rgb2gray(PreP1); %figure, imshow(PreP1) %title('Grayscale Image') end
% 4- Convert to Black and White (thresholding) threshold = graythresh(PreP1); PreP1 =~im2bw(PreP1,threshold); PreP2 = PreP1; %figure, imshow(PreP2) %title('Binary Image')
% Remove all object containing fewer than 30 pixels PreP3 = bwareaopen(PreP2,30); Image3 = PreP3; figure, imshow(Image3) title('Filtered Image');pause(3)
%Storage matrix word from image word=[ ]; re=Image3;
%Opens .txt file to write the extracted text fid = fopen('Text Extracted.txt', 'wt');
% Load templates load templates global templates
% Compute the number of letters in template file num_letras=size(templates,2);
while 1 %separate letter in lines [fl, re]=lines_crop(re); % fl = first line in text; re = remaining imgn2=fl;
figure,imshow(fl);pause(3) %
% Label and count connected components
[L, Ne] = bwlabel(imgn2);
for n=1:Ne
%to get the row and column coordinates of the object labeled 'n'
[r,c] = find(L==n);
% Extract Letter
n1=imgn2(min(r):max(r),min(c):max(c));
% Resize letter (same size of template)
img_r=imresize(n1,[42 24]); %imresize(A,scale) returns img_r that is scale times the size of A.
imshow(img_r);pause(0.5)
%-------------------------------------------------------------------
% Call fcn to convert image to text
letter=read_letter(img_r,num_letras);
% Letter concatenation
word=[word letter];
end
%fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)
word=[ ];
%*When the sentences finish, breaks the loop
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
end
%Text that has been extracted will be displayed in the 'text.txt' file winopen('Text Extracted.txt')
%read data from .txt file data = textread('Text Extracted.txt', '%s', 'whitespace','');
%text2speech synthesizer convert the data from .txt file to voice TextToSpeech(data);
fclose(fid); clear

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!