Matlab - OCR function digits recognition - matlab

Recently I'm trying to detect digits from images in Matlab and I encountered a problem: a clear visible '1' on photo below (and many similar)
1
is not detected by 'ocr' function. Could you tell me how can I preprocess this image to allow this funcion recognize such a numbers?

you might find this example on the mathworks web-site helpful. The second example of looking for digits on a picture of calculator is particularly relevant.
Applying the first trick they use (telling OCR you are expecting a block of text) results in the code below; which I found partially worked with the particular example image you linked above.
% Load your image
I = imread('tkTMN.jpg');
% Perform OCR, looking for a block of text:
results = ocr(I, 'TextLayout', 'Block');
% Display the recognized words
if ~isempty(results.Words)
disp(results.Words);
else
error('no words found');
end
For me, on matlab 8.6.0.267246 (R2015b) this returned the word list:
'1'
'.'
'j'
So not perfect, but at least it found the '1'. The other pre-processing tricks suggested on the mathworks page might get you better results.

Related

How to recognize and print the segmented character

I'm applying some image processing techniques in MATLAB I was able to segment the license plate as show in the figure below:
Now if I apply the followig code in a for loop:
ocrResults = ocr(finalImage);
ocrResults.Text
I'm getting output like VV, u etc that means these characters are not recognized properly. So, how can I fix that? It's not mandatory to use the OCR class so any other solution will also work.
MATLAB's ocr function accepts additional inputs as Name/Value pairs. In your case, to limit the output to numeric values, simply add in the following parameters:
ocrResults = ocr( finalImage, 'CharacterSet', '0123456789' );
However, I'm not certain doing just this will get you the output you desire. It might be helpful to erode the image and add additional blackspace around each character. Take advantage of other possible input parameters which may be added, such as 'TextLayout'.

Execute Example Code in Matlab when Publishing a Function

Background
Matlab has a publish function which outputs a, say, .html file based on the syntax of the comments. Sections which contain example code are indicated by a '%' followed by three spaces. An example function can be seen below, and the .html file generated by the publish function can be seen here.
function randomImage = GenerateRandomImage(n)
%% TestFunction
% generates a random image
%
% randomImage = GenerateRandomImage(n) returns an nxn array of random pixels
%
%% Example
% randomImage = GenerateRandomImage(10);
% figure; imagesc(abs(randomImage));
randomImage = rand(n);
end
Matlab will not automatically evaluate the example code, unless I create a separate script with the example code explicitly uncommitted. An example script is seen below. This time, it automatically includes the outputs of that script, such as the image produced by the example script shown here here.
%% TestFunction
% generates a random image
%
% randomImage = GenerateRandomImage(n) returns an nxn array of random pixels
%
%% Example
randomImage = GenerateRandomImage(10);
figure; imagesc(abs(randomImage));
The Question
Is it possible for the publish function to automatically evaluate the code snippits in the comments of a function and include those outputs in the html file?
The matlab publisher is explicitly meant to be performed on scripts, not function files, since its purpose is to produce a report, where individual sections (marked with %%) become headings, and lines beginning with % immediately under the %% become the description text for that section. Anything that follows after that, both code and comment lines, will be presented by the publisher in a nice little box as the code that was run for that section, and then the results of that code will be shown below that box, but contained within their section.
Putting code in the description text for that section, expecting it to be run is counter-intuitive. In fact, the only reason you might want to put a code snippet in the description is if you wanted to mention some code without running it.
For this reason the publisher provides special syntax (indent by 2 spaces for monotyped text, 3 spaces to add syntax-highlighting to your code).
Publisher is a nice tool, but unfortunately it's fairly limited. The main limitation for me is that you cannot choose which code sections to show results for and which ones not to; it's all or nothing. Therefore I usually resort to a "latex + saved matlab figures + makefile" approach instead for flexible reports. But publisher is nice if you want something quick and dirty; e.g. it's good for keeping a logbook / diary of your work.
Anyway, to answer your question, no, you can't put code in the description and expect it to run. Nor are you expected to 'publish' functions; the fact that a function file responds at all to publishing in the first place is probably more of a side-effect of how publisher gets called more than anything.

Displaying Matlab Function Summary in Lookup Query

I'm creating my own function in Matlab and I want to be able to display it like so below, when I type 'lookfor'.
>> lookfor mean
mean - Average or mean value.
msfun_metronomean - METRONOMANIMATION S-function for making metronomean animation.
mameannorm - normalizes microarray data by dividing by global mean.
distfcm - Distance measure in fuzzy c-mean clustering.
fcm - Data set clustering using fuzzy c-means clustering.
initfcm - Generate initial fuzzy partition matrix for fuzzy c-means clustering.
stepfcm - One step in fuzzy c-mean clustering.
I've heard it can be done by typing a line directly below the declaration of the function as can be seen below:
function [outputArgs] = TestFunction(inputArgs)
%TESTFUNCTION Summary of this function goes here
However I've tried this and it still doesn't appear in the list of lookups?
Any help would be greatly appreciated.
Thanks in advance!
It is not clear what you have done. For a function like
function [outputArgs] = TestFunction(inputArgs)
%TESTFUNCTION Summary of this function goes here
I would not expect lookfor mean to find it. The documentation of lookfor is pretty clear
lookfor topic searches for the string topic in the first comment line
(the H1 line) of the help text in all MATLABĀ® program files found on
the search path. For all files in which a match occurs, lookfor
displays the H1 line.
lookfor topic -all searches the entire first comment block of a MATLAB
program file looking for topic.
If you want lookfor mean to find your function you need to have the H1 line include the word "mean"
function [outputArgs] = TestFunction(inputArgs)
%TESTFUNCTION Summary of this function goes here (mean)
Then lookfor mean works fine.
I'm not sure exactly what I changed but it now works now. I'm sure it was something to do with the file path that the project was stored in. Thanks for your help though guys.

How to deal with Epipole inside Image (Stereo image) and Persistance of Outlier

I am using Matlab to create a stereo image. I have followed the example shown here:
http://www.mathworks.com/help/vision/examples/stereo-image-rectification.html
The "if" loop is where I hit problems:
if status ~= 0 || isEpipoleInImage(fMatrix, size(I1)) ...
|| isEpipoleInImage(fMatrix', size(I2))
error(['Either not enough matching points were found or '...
'the epipoles are inside the images. You may need to '...
'inspect and improve the quality of detected features ',...
'and/or improve the quality of your images.']);
I've commented out the status part so I know that there is an epipole within an image. I'm not sure what sort of problem this creates. Is it a big issue, or should I just comment this part out? Also, things look good for the most part but an outlier persists after the RANSAC implementation - any help with fixing that would be very much appreciated!
for getting rid of outliers, there are 2 things you can do.
1) Change the threshold of the feature detector
2) Modify some parts of RANSAC, such as minimum number of inliers for termination and minimum acceptable error in homography

MATLAB m-file help formatting

I could not find what formatting available to write help for your own MATLAB function. Very little information is available in official documentation.
Do you know about any other formatting that can be visible with Help Browser (not with help function)? As it is for built-in functions. How to format headings (like Syntax, Description, Examples)? Are bullets, tables possible? Or may be it should be a separate file?
I've tried text markup as used for PUBLISH and HTML, didn't work.
I found only one interesting thing. If your function contains mixed case, like testHelpFunction, its name will be highlighted:
No highlighting if it's just testhelpfunction.
Any other thoughts?
UPDATE
Here is extensive documentation I found on creating your own help files:
Providing Your Own Help and Demos
(Dead link replaced with web archive link)
(Dead link removed)
Updated again:
Add Help for Your Program
Display Custom Documentation
Try this other section in the official documentation. It's more thorough. MATLAB > User's Guide > Desktop Tools and Development Environment > Customizing Help and Demos > Providing Your Own Help and Demos. This describes both simple helptext and generating separate HTML help files.
Here's the helptext formatting I've picked up on and found useful.
function foo(x,y,z)
%FOO One-line description goes here
%
% foo(x,y,z)
%
% Multi-line paragraphs of descriptive text go here. It's fine for them to
% span lines. It's treated as preformatted text; help() and doc() will not
% re-wrap lines. In the editor, you can highlight paragraphs, right-click,
% and choose "Wrap selected comments" to re-flow the text.
%
% More detailed help is in the extended help.
% It's broken out like this so you can keep the main "help foo" text on
% a single screen, and then break out obscure parts to separate sections.
%
% Examples:
% foo(1,2,3)
%
% See also:
% BAR
% SOMECLASS/SOMEMETHOD
disp(x+y+z);
function extended_help
%EXTENDED_HELP Some additional technical details and examples
%
% Here is where you would put additional examples, technical discussions,
% documentation on obscure features and options, and so on.
error('This is a placeholder function just for helptext');
The first line after the function signature is called the "H1 line". It needs to be just one line so it is properly picked up by contentsrpt(), which can autogenerate a Contents.m file from the helptext in your functions
The function name in the H1 line is all caps, regardless of actual capitalization of the function name in the signature
Case matters for the "See also". I'm not sure which all cases work; this one does for sure.
Function names after "See also:" are all caps. Method names are qualified; I think names of methods in the same class as the current method can be unqualified.
Everything between the H1 line and "Examples:" is just a conventional formatting that I find readable; help() doesn't treat it specially.
You can use a limited form of hyperlinks in help. In particular, you can use hyperlinks to invoke arbitrary Matlab commands, and point to other sections of helptext by having it invoke help(). You can use this to point to any function; "function>subfunction" is just the syntax for addressing subfunctions in help() calls. Unfortunately, since you need to put either "help" or "doc" in those hyperlinks, it only works in one or the other presentation form. It would be nicer if there were a direct helptext hyperlink form.
I think the most important aspect in help formatting is that there is a help at all, and that your formatting is consistent, so that you (and people working with you) don't waste time finding out how to look for the information. Note that for OOP, it is useful to have a superclass with a 'help'-method that calls doc(class(obj)), since you cannot easily access the help from an instantiation of your class
To help me be consistent (and to make sure I don't forget stuff), I have created an automatic function template on the file exchange.
Here's the minimal header
function testhelp
%TESTHELP is an example (this is the H1 line)
%
% SYNOPSIS: a=testhelp(b,c)
%
% INPUT b: some input parameter
% c: (opt) some optional input parameter. Default: []
%
% OUTPUT a: some output parameter
%
% REMARKS This is just an example, it won't run
%
% SEE ALSO testHelpFunction
%
% created with MATLAB ver.: 7.11.0.584 (R2010b) on Mac OS X Version: 10.6.4 Build: 10F569
%
% created by: Jonas
% DATE: 01-Oct-2010
%
I figure there is some (see example), but I've never found the appropriate documentation. I often have such blocks:
% ...
%
% See also:
% this_other_function()
%
% <author>
And the See also part is formatted as a title, but if you replace See also by something else, it doesn't work. If anyone finds the list of such supported titles, please link to it here!
EDIT:
I've recently come to learn about matlab's built-in publishing system. It seems MATLAB comments support some form of markup not so far from Markdown's syntax (as used on SO itself), with support for LaTeX equations and all.
There's a post by "Loren on the art of MATLAB" with a short introduction on publishing and markup. For a full reference, see Making Up MATLAB Comments for Publishing on the Mathworks website.
When your code is ready, you can export the result to HTML/PDF/XML, etc. using the publish() function. I use the following snippet to export my files:
% Other formats are supported, refer to documentation.
options.format = 'html';
% I don't evaluate the code, especially for functions that require arguments.
% However, if providing a demo, turning this on is a fantastic way to embed
% figures in the resulting document.
options.evalCode = false;
% You can run this in a loop over files in the currrent directory if desired.
publish('script.m', options);