Step-by-step Tutorial on Face Detection with MATLAB – Part 1

This is a step-by-step, easy to use, beginner level, and detailed tutorial for face detection with MATLAB. Let’s start!!

List available devices

In order to list the available webcams that can be detected and used by MATLAB do as follows:


The result should be something similar to this:

ans =
'FJ Camera'


Webcam is not detected

If MATLAB cannot detect your webcam, you need to install the package for USB cameras provided by mathworks.
Enter the following command in MATLAB command-line and press enter:


This will bring up a window that you need to select an action, Install from Internet is a good option. Then you have to scroll down and find USB camera and select it. After that MATLAB will ask you to log in with your mathworks account. If you do not have it, you can create one. Finally, the process will begin and you have to wait a little bit.

In order to see that everything is fine, check to see if MATLAB can find the camera:


The result should be something similar to this:

ans =
'FJ Camera'


Preview webcam

Firstly, get a list of existing (detectable) webcams. And use the one that you want to make a webcam object. In the following example I have chosen the first item in the list.


When the object is created, you can check its properties by just typing the name of the object:

>> cam
cam =
Name: 'FJ Camera'
Resolution: '640x480'
AvailableResolutions: {'640x480'  '352x288'  '320x240'  '176x144'  '160x120'  '1280x1024'}
Gain: 0
Contrast: 32
BacklightCompensation: 1
Saturation: 64
Brightness: 0
Gamma: 100
Sharpness: 0
WhiteBalance: 4600
Hue: 0

To get a preview, you can do as follows:


To close the preview windows:




Capture a single Image

To capture a single image from the webcam while the preview window is open, you can use the following commands:

img = snapshot(cam);

The result would be captured as a matrix in MATLAB called img. To show the image in a new window you can use:


if you have the image processing toolbox, otherwise use:


remember if you close a preview using closePreview command, you should also clear the object from the memory:


Simple face detection

In order to do a simple face detection, you need to take a snapshot and check to see if there is a face in there.first create a face detection object. (you have to have the computer vision toolbox).

faceDetector = vision.CascadeObjectDetector;

and then easily pass the taken snapshot to the face detector. If it finds a face in the input image, the return would be 4 coordinates of the corners a box around the detected face.

bboxes = step(faceDetector, img);


Source Code – A simple face detection by MATLAB

Because most of webcam cameras are low quality and noisy, you can use the following loop, in order to avoid manual repetition.

C = webcamlist;
NotYet = false;
faceDetector = vision.CascadeObjectDetector;
while ~NotYet
I = snapshot(cam);
disp('took a snapshot. checking to find a face ....')
bboxes = step(faceDetector, I);
if ~isempty(bboxes)
NotYet = true;
disp('face found!');
disp('no face detected :(, repeating...');
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Face');
figure, imshow(IFaces), title('Detected faces');



Posted in Linux, Machine Learning, MATLAB, OpenCV, programming, Robotics, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Efficient coding in MATLAB – part 1

Question: Given a matrix, test to see if any of its elements are greater than 3.

A = rand(3,7,5) * 5;

Non-efficient code:

b = ~isempty(find(A>3));

More efficient code:

b = ~isempty(find(A(:)>3));

Most efficient code:

b = any(A(:)>3);

check the documents for any function.
short description: Determine if any array elements are nonzero


Question: Pick odd elements from the matrix below:

A = [1 -3 2;5 4 7;-8 1 3];

Non-efficient code:

for ii=1:size(A,1)
for jj=1:size(A,2)
if mod(A(ii,jj),2)~=0

More efficient code:

[r,c,v] = find(B==1);

Most efficient code:


check the documentation for logical function.
short description: Convert numeric values to logicals



Posted in Linux, MATLAB, Optimization, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

What is a DOI?

A DOI is a unique permanent number used to identify digital content which stands for Digital Object Identifier. For instance, a DOI can be generated for a journal paper you have published. DOIs provide a reliable link to your work online, making it easy to be cited by others. DOIs can be generated for both published and unpublished works. Publishers generate DOIs for papers they publish. A DOI also can be generated by other websites as well, for instance ResearchGate. If you add a contribution to your account in ResearchGate, the website generates a DOI for the file you have uploaded. When you have a DOI for an existing publication you can just search the DOI number and it takes you the corresponding page.



Posted in Linux, Software, Thoughts, Ubuntu | Tagged , , , , , , , , , , | Leave a comment

Anonymous functions in MATLAB/OCTAVE – A Simple Example

In the previous post, I showed you lambda function in python and how to combine it with built-in filter function. As you may know, lambda is also known as anonymous function. In this post, we take a look at using anonymous functions in MATLAB/OCTAVE.

It is really easy, in MATLAB/OCTAVE you need to define the anonymous function using a handle sign (i.e. @): it would be something like this:

f = @(x)(x^3 + x^2 + x + 1)


It can be seen that after the @, the parameters are listed between parenthesis. Then you mention the function embraced with another couple of parenthesis.
After you defined the anonymous function, now it can be used in many ways. For instance you can evaluate it, like this:

ans = 25.375

You can also, calculate integrals, for instance:


ans = 2.0833


Let’s do all that in one line of code:

quad(@(x)(x^3 + x^2 + x + 1),0,1)
ans = 2.0833

Now consider a case that you have more than a variable in your function. It is simple, NO?

f=@(x,y)(x^2 + y^3  + x*y)
ans = 5.1250

And the last and the most interesting tip here. In MATLAB/OCTAVE we can have array of anonymous functions, for instance:

f={@(x) (x+2);
@(y) (3*y + y^0.5);
@(z) (sin(z)+cos(z));


ans = 2.200
ans = 1.4477
ans = -0.67526
ans = 0.087168

Now, go crazy!!!

Posted in Linux, MATLAB, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Using ‘filter’ in python – a lambda function tip

When using filter in python, one of the most helpful functions is lambda. Firstly, because you do not want to name and separately save any simple function. Secondly, lambda is a fast to code function, if you know how to. I give you some example, let’s first see how to use lambda function. The following piece of code shows a normal function, including a return term:

def f(x):
return x+ x**2 + x**3

Now if we want to use lambda function, the code would be as:

g = lambda x: x+ x**2 + x**3

As you can see, after lambda you should mention the parameters of the function and then “:” as usual in python. And then you mention the function without any return term.

filter is a  built-in function in python.
According to python documentation, filter constructs a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Now we want to combine filter with lambda to get more efficient and simpler code. Check the following example out:

# define an array
Ar = [3,67,23,55,90]
# now use lambda to find elements divisible by 3
print filter(lambda x: x % 3 ==0, Ar)

The result would be as follows:



Posted in Linux, Optimization, programming, Python, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Modify the bibliography style in Latex – Part.1

When using IEEEtran bibliography style, authors’ name are something like following:

In case you need to have full first name for each author, you need to perform the following steps:


1- open IEEEtran.bst and find the following line:

FUNCTION {}{ "{f.~}{vv~}{ll}{, jj}" }

This line shows the format has been used by default for presenting the first, middle, and last names.

2- Replace the line with the following line:

FUNCTION {}{ "{ff~}{vv~}{ll}{, jj}" }

3- save the file and compile the latex project again, you should get results similar to following:




Note that, you need to have the full names in your bib file (bibliography database), otherwise the software cannot guess by itself. ;)

Posted in Linux, Thoughts, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Make PowerPoint presentation using MATLAB – toPPT

Short description:

toPPT is a powerful tool for generating PowerPoint presentations programmatically defined in MATLAB. It will use different scripts to perform exports of figures, tables and texts. For this purpose it will use scripts written by Jens Richter

main article + example

toPPT accepts the following predefined TeX parameters:

   'alpha','upsilon','sim','beta','phi','leq','gamma','chi', ...
   'infty','delta','psi','clubsuit','epsilon','omega','diamondsuit', ...
   'zeta','Gamma','heartsuit','eta','Delta','spadesuit','theta', ...
   'Theta','leftrightarrow','vartheta','Lambda','leftarrow','iota', ...
   'Xi','uparrow','kappa','Pi','rightarrow','lambda','Sigma', ...
   'downarrow','mu','Upsilon','circ','nu','Phi','pm','xi','Psi','geq', ...
   'pi','Omega','propto','rho','forall','partial','sigma','exists', ...
   'bullet','varsigma','ni','div','tau','cong','neq','equiv','approx', ...
   'aleph','Im','Re','wp','otimes','oplus','oslash','cap','cup', ...
   'supseteq','supset','subseteq','subset','int','in','o','rfloor', ...
   'lceil','nabla','lfloor','cdot','ldots','perp','neg','prime', ...
   'wedge','times','0','rceil','surd','mid','vee','varpi','copyright', ...
Posted in Linux, MATLAB, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , | Leave a comment