hwo to extract numeric value from an image

4 次查看(过去 30 天)
_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*

采纳的回答

Walter Roberson
Walter Roberson 2018-6-11
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 个评论
Image Analyst
Image Analyst 2018-6-11
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 2018-6-11
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.

请先登录,再进行评论。

更多回答(1 个)

praveen rai
praveen rai 2018-6-12
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

类别

Help CenterFile Exchange 中查找有关 Feature Detection and Extraction 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by