hwo to extract numeric value from an image
5 ビュー (過去 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*
0 件のコメント
採用された回答
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
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
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 件)
参考
カテゴリ
Help Center および File Exchange で Feature Detection and Extraction についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!