How to read numbers from an image in matlab using ocr (optical character recognition)?

This video shows how one can use OCR from Computer Vision toolbox of Matlab to easily read numbers or digits from an image.

It further shows how we can superimpose a rectangle over the boundaries of the text part identified by the OCR in the image.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: or email at:

Complete source code and other details:

I = imread('number.png');
ocrOutput = ocr(I, 'TextLayout', 'Line', 'characterSet', '0123456789');
outputNum = str2double(ocrOutput.Text)
ocrImage = insertObjectAnnotation(I, 'rectangle', ocrOutput.WordBoundingBoxes, ...

The images used in demo are posted below:


    • Yes, OCR command requires ‘Computer Vision Toolbox’. Same can be validated using the below command in MATLAB:

      >> [~,pList] = matlab.codetools.requiredFilesAndProducts(‘ocr.m’);
      >> pList.Name

      The only workaround would be to use other tools (such as Python, Tesseract, etc.) which provide free libraries for OCR. And then call the respective commands from your MATLAB script and get the results back in the MATLAB environment.

Leave a Reply