image processing using template matching approach - matlab

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!

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

Find the connected component in matlab

Given a BW image that contains some connected components.
Then, given a single pixel P in the image. How to find which component that contains the pixel P? It is guaranteed that the pixel P is always on the white area in one of the connected components.
Currently, I use CC = bwconncomp(BW) than I iterate each component using 'for' loop. In the each component, I iterate the index pixel. For each pixels, I check whether the value equal to the (index of) pixel P or not. If I find it, I record the number of connected component.
However, it seems it is not efficient for this simple task. Any suggestion for improvement? Thank you very much in advance.
MATLAB provides multiple functions that implement connected-component in different ways.
In your example, I would suggest bwlabel.
http://www.mathworks.com/help/images/ref/bwlabel.html
[L, num] = bwlabel(imgBW) This will perform a full-image connected-component labeling on a black-and-white image.
After calling this function, the label value that pixel P belongs to can be read off the result matrix L, as in label_to_find = L(row, col) index. Simple as that.
To extract a mask image for that label, use logical(L == label_to_find).
If you use different software packages such as OpenCV you will be able to get better performance (efficiency in terms of cutting unnecessary or redundant computation), but in MATLAB the emphasis is on convenience and prototyping speed.

fit function of Matlab is really slow

Why is the fitfunction from Matlab so slow? I'm trying to fit a gauss4 so I can get the means of the gaussians.
here's my plot,
I want to get the means from the blue data and red data.
I'm fitting a gaussian there but this function is really slow.
Is there an alternative?
fa = fit(fn', facm', 'gauss4');
acm = [fa.b1 fa.b2 fa.b3 fa.b4];
a_cm = sort(acm, 'ascend');
I would apply some of the options available with fit. These include smoothing by setting SmoothingParam (your data is quite noisy, the alternative of applying a time domain filter may also help*), and setting the values of your initial parameter estimates, with StartPoint. Your fits may also not be converging because you set your tolerances (TolFun, TolX) too low, although from inspection of your fits that does not appear to be the case, in fact the opposite is likely, you probably want to increase the MaxIter and/or MaxFunEvals.
To figure out how to get going you can also try the Spectr-O-Matic toolbox. It requires Matlab 7.12. It includes a script called GaussFit.m to fit gauss4 to data, but it also uses the fit routine and provides examples on how to set and get parameters.
Note that smoothing will of course broaden your peaks, but you can subtract the contribution after the fact. The effect on the mean should not be deleterious, on the contrary, since you are presumably removing noise this should be more accurate.
In general functions will be faster if you apply it to a shorter series. Hence, if speedup is really important you could downsample.
For example, if you have a vector that you want to downsample by a factor 2: (you may need to make sure it fits first)
n = 2;
x = sin(0.01:0.01:pi);
x_downsampled = x(1:n:end)+x(2:n:end);
You will now see that x_downsampled is much smaller (and should thus be easier to process), but will still have the same shape. In your case I think this is sufficient.
To see what you got try:
plot(x)
Now you can simply process x_downsampled and map your solution, for example
f = find(x_downsampled == max(x_downsampled));
location_of_maximum = f * n;
Needless to say this should be done in combination with the most efficient options that the fit function has to offer.

function parameters in matlab wander off after curve fitting

first a little background. I'm a psychology student so my background in coding isn't on par with you guys :-)
My problem is as follow and the most important observation is that curve fitting with 2 different programs gives completly different results for my parameters, altough my graphs stay the same. The main program we have used to fit my longitudinal data is kaleidagraph and this should be seen as kinda the 'golden standard', the program I'm trying to modify is matlab.
I was trying to be smart and wrote some code (a lot at least for me) and the goal of that code was the following:
1. Taking an individual longitudinal datafile
2. curve fitting this data on a non-parametric model using lsqcurvefit
3. obtaining figures and the points where f' and f'' are zero
This all worked well (woohoo :-)) but when I started comparing the function parameters both programs generate there is a huge difference. The kaleidagraph program stays close to it's original starting values. Matlab wanders off and sometimes gets larger by a factor 1000. The graphs stay however more or less the same in both situations and both fit the data well. However it would be lovely if I would know how to make the matlab curve fitting more 'conservative' and more located near it's original starting values.
validFitPersons = true(nbValidPersons,1);
for i=1:nbValidPersons
personalData = data{validPersons(i),3};
personalData = personalData(personalData(:,1)>=minAge,:);
% Fit a specific model for all valid persons
try
opts = optimoptions(#lsqcurvefit, 'Algorithm', 'levenberg-marquardt');
[personalParams,personalRes,personalResidual] = lsqcurvefit(heightModel,initialValues,personalData(:,1),personalData(:,2),[],[],opts);
catch
x=1;
end
Above is a the part of the code i've written to fit the datafiles into a specific model.
Below is an example of a non-parametric model i use with its function parameters.
elseif strcmpi(model,'jpa2')
% y = a.*(1-1/(1+(b_1(t+e))^c_1+(b_2(t+e))^c_2+(b_3(t+e))^c_3))
heightModel = #(params,ages) abs(params(1).*(1-1./(1+(params(2).* (ages+params(8) )).^params(5) +(params(3).* (ages+params(8) )).^params(6) +(params(4) .*(ages+params(8) )).^params(7) )));
modelStrings = {'a','b1','b2','b3','c1','c2','c3','e'};
% Define initial values
if strcmpi('male',gender)
initialValues = [176.76 0.339 0.1199 0.0764 0.42287 2.818 18.52 0.4363];
else
initialValues = [161.92 0.4173 0.1354 0.090 0.540 2.87 14.281 0.3701];
end
I've tried to mimick the curve fitting process in kaleidagraph as good as possible. There I've found they use the levenberg-marquardt algorithm which I've selected. However results still vary and I don't have any more clues about how I can change this.
Some extra adjustments:
The idea for this code was the following:
I'm trying to compare different fitting models (they are designed for this purpose). So what I do is I have 5 models with different parameters and different starting values ( the second part of my code) and next I have the general curve fitting file. Since there are different models it would be interesting if I could put restrictions into how far my starting values could wander off.
Anyone any idea how this could be done?
Anybody willing to help a psychology student?
Cheers
This is a common issue when dealing with non-linear models.
If I were, you, I would try to check if you can remove some parameters from the model in order to simplify it.
If you really want to keep your solution not too far from the initial point, you can use upper bounds and lower bounds for each variable:
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
defines a set of lower and upper bounds on the design variables in x so that the solution is always in the range lb ≤ x ≤ ub.
Cheers
You state:
I'm trying to compare different fitting models (they are designed for
this purpose). So what I do is I have 5 models with different
parameters and different starting values ( the second part of my code)
and next I have the general curve fitting file.
You will presumably compare the statistics from fits with different models, to see whether reductions in the fitting error are unlikely to be due to chance. You may want to rely on that comparison to pick the model that not only fits your data suitably but is also simplest (which is often referred to as the principle of parsimony).
The problem is really with the model you have shown resulting in correlated parameters and therefore overfitting, as mentioned by #David. Again, this should be resolved when you compare different models and find that some do just as well (statistically speaking) even though they involve fewer parameters.
edit
To drive the point home regarding the problem with the choice of model, here are (1) results of a trial fit using simulated data (2) the correlation matrix of the parameters in graphical form:
Note that absolute values of the correlation close to 1 indicate strongly correlated parameters, which is highly undesirable. Note also that the trend in the data is practically linear over a long portion of the dataset, which implies that 2 parameters might suffice over that stretch, so using 8 parameters to describe it seems like overkill.

Getting the solver type and step size (for fixed step solvers)

we are trying to integrate a simulation model into Simulink as a block. We have a custom continuous block which loads an m file that contains the functions Derivatives, Outputs etc.
My question is: is there a way to find out which solver is used currently and with which parameters? Our model won't be able to support variable time solvers and I would like to give a warning. Similarly, the model requires the fixed step time for initialization.
Thanks in advance.
You can get the current solver name using
get_param('modelName', 'SolverName');
Some of the other common solver parameters are
AbsTol
FixedStep
InitialStep
ZcThreshold
ExtrapolationOrder
MaxStep
MinStep
RelTol
SolverMode
You can find other parameters you may wish to query by opening the .mdl file in your favorite text editor and digging through it.
If I'm understanding your use case correctly, you are trying to determine the type of solver (and other solver params) for the top-level simulink system containing your block.
I think the following should give you what you want:
get_param(bdroot, 'SolverType'); % //Returns 'Variable-step' or 'Fixed-step'
get_param(bdroot, 'FixedStep'); % //Returns the fixed step size
Notice that for purposes of generality/reusability, this uses bdroot to identify the top-level system (rather than explicitly specifying the name of this system).
If you want to find out more about other model parameters that you can get/set, I would check out this doc.
Additionally, I'm interested to know why it is that your model doesn't support a variable-step solver?