How to recognize and print the segmented character - matlab

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'.

Related

It appears imageLabeler cannot handle a transformed datastore?

The Matlab app imageLabeler is supposed to support the following format:
imageLabeler(imgStore)
I have an imgStore, defined as follows:
imds = imageDatastore(cellArrayOfImageFilenames);
imgStore = transform(imds, #(x)demosaic(x,'rggb'));
I have to do this, because my images are stored as bayer encoded images, and this is the only way I've figured out to get the imgStore to return these images as 3 channel RGB images. However, when I try and initalize imageLabeler, I get this error:
>> imageLabeler(imgStore)
Error using imageLabelerInternal
Expected input name to be one of these types:
char
Instead its type was matlab.io.datastore.TransformedDatastore.
Error in vision.internal.imageLabeler.imageLabelerInternal
Error in imageLabeler (line 58)
vision.internal.imageLabeler.imageLabelerInternal(varargin{:});
TLDR:
How do I get imageLabeler to handle my bayer encoded images?
The way to fix this, is with the imageDatastore 'ReadFcn' parameter. The documentation for imageDatastore explicity tells you to NOT do this, as it slows down Neural Network stuff. Here's the Matlab doc text:
Using ReadFcn to transform or pre-process 2-D images is not
recommended. For file formats recognized by imformats, specifying
ReadFcn slows down the performance of imageDatastore. For more
efficient ways to transform and pre-process images, see Preprocess
Images for Deep Learning (Deep Learning Toolbox).
So, all that said, here's the workaround:
imgStore = imageDatastore(cellArrayOfImageFilenames ...
, 'ReadFcn', #(x)demosaic(imread(x),'rggb')));

MATLAB display a link to workspace elements

I'm trying to improve the readability of my outputs. In order to avoid displaying a lot of data, it would be nice to display links that point to specific elements in the workspace (i.e. a variable, a table, a figure, etc.).
Here is a picture to illustrate the idea:
Maybe we can use the disp function, as I know it allows the generation of hyperlinks to a webpage or a file stored in the computer.
Is this possible in MATLAB?
OK, so this is what I came up with. The first thing is to use the openvar function and you specify the variable you want wrapped around in single quotations. This will open up the variable in the Variable Editor (the image that is pictured in your snapshot).
Now, you can also use disp to allow clickable links to run MATLAB commands. Using these two ideas, you would combine the disp linking and openvar to allow a clickable link to execute the openvar function to display your desired variable.
As such, you would basically do this assuming our variable is stored in A:
A = magic(5);
disp('Click on me to show the matrix A')
The disp statement will show you a clickable link, and the desired function to be executed only runs if you click on the link. You can achieve this desired effect by specifying the matlab: keyword inside the URL in the href key, then after it, you write out whatever MATLAB function or statements you want to use. In our case, we use only need to run one function, and that's openvar. Make sure you specify single quotes around the variable you want inside the argument to openvar. The reason why is because the argument to disp is a string, and if you want single quotations to be recognized, you must use a pair of single quotes. As such, in the disp string, there are pairs of single quotes around the variable you want.
Here's the what I get in MATLAB. The steps are reproduced and shown in an animated GIF:

image processing using template matching approach

i am working on a project related to template matching image processing , i have done the matching algorithm but the problem i am facing that , template matcher always yeilds the best co-relation matched in source image of template image but i want to notify or respond only when the desired output comes neither on false output. i want to serially communicate MATLAB code with arduino board UNO R3 which will generate the pulse birectionaly when the output comes ? so what should i supposed to do ?
this is the code :
cam=videoinput('winvideo',2,'YUY2_320x240');
start(cam);
preview(cam);
set(cam,'ReturnedColorSpace','RGB');
get=input('get frame ???');
frame=getsnapshot(cam);
imwrite(frame,'got.jpg');
I=imread('D:\Template matcher\got.jpg');
H_Eq=vision.HistogramEqualizer;
Temlate_matcher=vision.TemplateMatcher;
Temlate_matcher.Metric='Maximum absolute difference';
Temlate_matcher.OutputValue='Metric matrix';
marker_inserter=vision.MarkerInserter('Size',30,'Fill',false,'FillColor','White','Opacity',0.75);
I=rgb2gray(I);
I=step(H_Eq,I);
Template1=imread('D:\Template matcher\ge.jpg');
Template1=rgb2gray(Template1);
H_Eq=vision.HistogramEqualizer;
Template1=step(H_Eq,Template1);
Location1=step(Temlate_matcher,I,Template1);
marker_inserter.Shape='Square'
output_image=step(marker_inserter,I,Location1);
figure();imshow(output_image);
As we have discussed in our comments, template matching using the Computer Vision toolbox will produce the best template match in the frame. However, this does not necessarily mean that the object you are searching for is located within the location of where the template best matched.
As such, what I would recommend you do is take a look at what the metric gives you for that template. In your case, you're using the maximum absolute difference. If this metric is less than some threshold, then that could mean that the template was found in the frame you are examining. If it's greater, then there is a good possibility that it wasn't. This threshold you'll have to play around with as it totally dependent on what the template looks like and the contents of the frame you're trying to look at. If it is less than some threshold, then you can send your signal to the Arduino board. Before we do this, you must change your Template Matcher setup so it looks like this:
Temlate_matcher=vision.TemplateMatcher('OutputValue', ...
'Best match location', 'BestMatchNeighborhoodOutputPort', true);
This will allow us to get what we eventually want. As such, you can't use Metric Matrix as the OutputValue field anymore so get rid of this line in your code. Now that this is set up, you can override the behaviour of the template matching by replacing this code:
Location1=step(Temlate_matcher,I,Template1);
With this:
[Location1, NVALS, NVALID] = step(Temlate_matcher,I,Template1);
NVALID returns true if the match is fully contained within the frame and false otherwise. For your case, NVALID should always be true as the metric you have chosen is guaranteed to do matching as long as the template is contained within the frame. NVALS is the matrix of metric values that best matched the template image. In essence, the centre of this matrix gives you the best metric value that the matcher produced, and so this is the value that you want. You can cheat and just find what the minimum value is by:
val = min(NVALS(:));
As such, after this point you can check to see if val is less than some threshold. This I don't know what it would be. I guess something to play around with would be that if the best match was off by... say 5, then there may be something interesting to take a look at in the frame. As such, set thresh = 5.
Therefore, if val is less than thresh, then go ahead and signal your Arduino board.
Good luck!

imfreehand data type issue

I'm using imfreehand in my program, and when I came to erosion, I got the following error (I didn't paste the whole error):
Error using imerode
Expected input number 1, IM, to be one of these types:
numeric, logical
Instead its type was imfreehand.
It seems then that the region I extract will be of type imfreehand? Is there a way to convert to the above data types? Or, there is some way to deal with such issue?
Thanks.
You can take a look at the examples in this post. My interpretation is that you create a mask using the handle associated with the generated freehand object,
hFH = imfreehand();
binaryImage = hFH.createMask();
then use the mask for additional purposes.

S-function documentation that "S-function level-1 supports vector inputs and outputs. DOES NOT support multiple input and output ports"

I read in S-function documentation that "S-function level-1 supports vector inputs and outputs. DOES NOT support multiple input and output ports".
Does the second sentence mean the input and output dimension must be the same?
I have been using S-function level-1 to do the following:
[a1, b1] = choose_cells(c, d);
where a1 and b1 are outputs, c and d are inputs. All the variables are having a single value, except d is an array with 6 values.
Referring to the image attached, we all know that in S-function block, the input dimension must be SAME as output dimension, else we will get error, in this case, the input dimension is 7 while the output dimension is 2, so I have to include the "Terminator" blocks in the diagram for it to work perfectly, otherwise, I will get an error.
My problem is, when the system gets bigger, the array d could contain hundreds of variables, using this method, it means I would have to add hundreds of "Terminator" blocks in order to get this work, this definitely does not sound practical.
Could you please suggest me a wise way to implement this?
http://imgur.com/ib6BTTp
http://imageshack.us/content_round.php?page=done&id=4tHclZ2klaGtl66S36zY2KfO5co
Updated: actually I have been trying to convert my level-1 S-function to level-2 but I got stuck at calling another sub function at function Output(block) trying to look for other threads but to no avail, do you mind to provide related links?
My output depends on a lot processing with the inputs, this is the reason I need to call the sub-function in order to calculate and then return output values, all the examples that I can see are calculating their outputs directly in "function Output(block)", in my case I thought it is not possible.
I then tried to use Interpreted Matlab Function block but failed due to the output dimension is NOT the same as input dimension, also it does not support the return of more than ONE output................
Level-1 s-function supports single input and single output port. These ports must be vectors. But there is no restriction on the length. Input and output can have different lengths. You can use selector block to select only relevant data. You do not need to use Bus in the output.
There is also no restriction on calling other sub-functions from Output. If your sub-function is not in the same file it must be in the path or in the current directory.
If your MATLAB code is compatible with MATLAB Function block, I recommend using that block. It is simpler to setup and use.