How to use OCR for charater regocnition on picture of keyboard

3 views (last 30 days)
Hi out there!
I'm doing a project in which i need to detect specific caracters on an image of a keyboard. I have been trying many different ways of pre-processing the image and i seem to have found a relatively clear image for the OCR function to detect the characters, although i can't seem to make it work :( If anyone has some tips or tricks it would be really helpfull :=) Thanks in advance!
Here's my code
clear all
close all
clc
%Pre-edditing of photo to make easy Character regocnition
I = imread('keyboard 4.jpg');
I = imresize(I, 3);
I = im2gray(I);
Icor = imtophat(I,strel('disk',8));
BW1 = imbinarize(Icor);
Marker = imerode(Icor,strel('line',4,0));
Iclean = imreconstruct(Marker,Icor);
BW2 = imbinarize(Iclean);
imshow(BW2)
%Implemenation of character regocnition
results = ocr(BW2,'TextLayout','Word', 'CharacterSet','A':'Z');
%Array of the needed letters
A = {'H','E','L','O','W','R','D'};
Boxsize = 0;
%Locating the letters, inspect the heigh column vector, find the max value
%of that vector, and extract the corrosponding matrix row vector
for i = 1:7
Letters = locateText(results,A(i),IgnoreCase=true)
Size = size(Letters)
for u=1+Boxsize:Size(1)+Boxsize(1)
box(u, :) = Letters(u-Boxsize(1), :)
end
Boxsize = size(box)
end
Iocr = insertShape(I,"FilledRectangle",box);

Answers (1)

Image Analyst
Image Analyst on 5 Dec 2022
ocr does not work well unless the letters are more than 20 pixels high. How high are your pixels?
help ocr
OCR Recognize text using Optical Character Recognition. txt = OCR(I) returns an ocrText object containing the recognized text, location of recognized characters, confidence of recognized characters, recognized words, location of recognized words and confidence of recognized words as properties. Confidence values range between 0 and 1 and should be interpreted as probabilities. txt = OCR(I, roi) recognizes text in I within one or more rectangular regions defined by an M-by-4 matrix, roi. Each row of roi is a four-element vector, [x y width height], that specifies the upper-left corner and size of a rectangular region of interest in pixels. Each rectangle must be fully contained within I. [...] = OCR(...,Name=Value) specifies additional name-value pair arguments described below: 'Language' Specify the language to recognize as a string or a cell array of strings. The language can be specified using the name of a language such as 'english', 'seven-segment' and 'japanese'. A list of supported languages is shown in the documentation. Custom trained languages are also supported. Default: 'english' 'TextLayout' Specify the layout of the text within I as a string to perform text segmentation. Valid string values are: 'auto' - Use 'TextLayout' as 'block' if 'Language' is 'seven-segment', otherwise use 'page'. 'page' - Treats the text in the image as a page containing blocks of text. 'block' - Treats the text in the image as a single block of text. 'line' - Treats the text in the image as single line of text. 'word' - Treats the text in the image as a single word of text. 'character' - Treats the text in the image as a single character. Default: 'auto' 'CharacterSet' Specify the character set as a string of characters. The classification process is constrained to select the best matches from this smaller set of characters. By default, all characters in the Language are used. Default: '' Class Support ------------- The input image I can be logical, uint8, int16, uint16, single, or double, and it must be real and nonsparse. Notes ------- The 'seven-segment' language uses '0 1 2 3 4 5 6 7 8 9 . : -' as character set. Example 1 - Recognize text within an image ------------------------------------------ businessCard = imread('businessCard.png'); ocrResults = OCR(businessCard) recognizedText = ocrResults.Text; figure imshow(businessCard) text(600, 150, recognizedText, ... BackgroundColor = [1 1 1]) Example 2 - Recognize text in regions of interest (ROI) -------------------------------------------------------- I = imread('7segmentDisplay.jpg'); % Define one or more rectangular regions of interest within I. roi = [1149,88,1106,375]; % You may also use DRAWRECTANGLE to select a region using a mouse: % figure; imshow(I); rect = drawrectangle; roi = round(rect.Position); ocrResults = OCR(I, roi, Language = 'seven-segment'); % Insert recognized text into original image. Iocr = insertObjectAnnotation(I,'rectangle', ... ocrResults.WordBoundingBoxes,ocrResults.Words); figure imshow(Iocr) Example 3 - Display word bounding boxes and recognition confidences ------------------------------------------------------------------- businessCard = imread('businessCard.png'); ocrResults = OCR(businessCard) Iocr = insertObjectAnnotation(businessCard, 'rectangle', ... ocrResults.WordBoundingBoxes, ... ocrResults.WordConfidences); figure imshow(Iocr) Example 4 - Find and highlight text in the image ------------------------------------------------ businessCard = imread('businessCard.png'); ocrResults = OCR(businessCard); bboxes = locateText(ocrResults, 'MathWorks', IgnoreCase = true); Iocr = insertShape(businessCard, 'FilledRectangle', bboxes); figure imshow(Iocr) See also ocrTrainer, ocrText, ocrText>locateText, insertShape, insertText Documentation for ocr doc ocr

Community Treasure Hunt

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

Start Hunting!

Translated by