hwo to extract numeric value from an image

6 views (last 30 days)
_I have an image in which I recognise the numeric part using OCR FUNCTION numeric value is lablled with yellow line rectangle I want to extract that numeric value and save in .txt file_
results = ocr(O, 'TextLayout', 'Block');
results.Text
GrayImage = uint8(255 * O);
regularExpr = '\d';
bboxes = locateText(results, regularExpr, 'UseRegexp', true);
digits = regexp(results.Text, regularExpr, 'match');
Idigits = insertObjectAnnotation(GrayImage, 'rectangle', bboxes, digits);
figure;
imshow(Idigits);title('digit image');
*i have used this code to make yellow rectangle box on numbers*
_when i am using_
results.Text
_output is coming_ *JANE IJYRE. 343 but i want only 343*
*'O' is my input binary image*

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jun 2018
str2double(regexp(results.Text, '\d+', 'match'))
this will produce a numeric vector of all of the substrings that it thinks are groups of digits. For example if it had though the text was 'JANE 1JYRE. 343' then the vector would contain [1 343]
  2 Comments
Image Analyst
Image Analyst on 11 Jun 2018
Very cool (+1 vote). Can it be made general enough to extract integers and floating point numbers? sscanf() can't since you need to know precisely what's in the string. If
str = 'JANE 1JYRE. 343 67.89'
numbers = str2double(regexp(str, '\d+', 'match'))
Can it make
numbers =
1 343 67.89
instead of
numbers =
1 343 67 89
Walter Roberson
Walter Roberson on 11 Jun 2018
You can use '[+-]?\d+(\.\d*)?' as the pattern to match numbers with an optional sign immediately followed by digits, optionally followed by period and then a series of digits.
This pattern will not, however, handle the case of space between sign and number (which increases the chance that the symbol is an operator), and will not handle the case of period with no leading 0, and will not handle floating point. Handling floating point and the possibility of no leading digit are possible, but the pattern gets a bit tricky to read and write. Handling optional spaces around optional signs gets a bit tricky, and starts getting into questions about whether the separated sign is intended to be a sign or is intended to be an operator. str2double() itself only permits adjacent sign, not separated sign.

Sign in to comment.

More Answers (1)

praveen rai
praveen rai on 12 Jun 2018
suppose i have a string str='Sec 4.12 Visual quantization 123' now i want only '123' so how shd i xtract dis part only

Tags

Community Treasure Hunt

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

Start Hunting!