Object Recognition using MATLAB 

 May 26, 2020

By  Roshini

The task of identifying an object using MATLAB is a simple example of deep learning using MATLAB. We will be using a deep neural network named AlexNet to identify the object in any image. Let us first see what a deep neural network is and how it works. Then let's see how it is implemented in our code using MATLAB.

What is Deep Learning?

Deep learning is a type of machine learning in which a model learns to perform classification tasks directly from images, text, or sound. Deep learning is usually implemented using a neural network architecture. The term "deep" refers to the number of layers in the network—the more layers, the deeper the network. Traditional neural networks contain only 2 or 3 layers, while deep networks can have more than 3.

Inside a Deep Neural Network

A deep neural network combines multiple non-linear processing layers, using simple elements operating in parallel. It is very much similar to our human biological system. It consists of an input layer, several hidden layers, and an output layer. The layers are interconnected through nodes, or neurons, with each hidden layer using the output of the previous layer as its input.

Deep neural network layers

Source: Mathworks

How does a neural network work?

Let's say we have a set of images where, each image contains one of three different categories of objects like a cat, dog, parrot, etc. We want the deep learning network to automatically recognize which object is in each image. We separate it into training data and testing data. From the input layer, we pass onto feature extraction layers that identify the unique feature present in each category. For example, a beak is a unique feature for the parrot. Each layer in the network takes in data from the previous layer, transforms it, and passes it on. After extracting the features, we move onto the next few layers which are classification layers. After performing the classification of training data, we then move onto the classification of testing data. That's how the images are classified using the previously learned data from the testing set.

Deep learning workflow

Source: Mathworks


If you're new to deep learning, a quick and easy way to get started is to use an existing network, such as AlexNet. AlexNet is a neural network that has already been trained on a million images. Hence, it can directly be used for testing. AlexNet is most commonly used for image classification. It can classify images into 1000 different categories, including keyboards, computer mice, pencils, and other office equipment, as well as various breeds of dogs, cats, horses, and other animals.

Alex net neural network

Source : Mathworks


You can use AlexNet to classify objects in any image. In this example, we'll use it to identify the object in an image from a webcam installed on a desktop. In addition to MATLAB, we'll be using the following:


Let us now see the coding part and the results obtained. First, we start with clear statements for clearing the windows and to start new every time we run the code.

close all

Next, we connect to the camera by naming it as a webcam to access the webcam.

camera = webcam;

Then we load the AlexNet which is already pre-trained and store it in nnet.

nnet = alexnet;

Then we take a snapshot using the webcam and store it in a picture using the snapshot function.

picture = camera.snapshot;

Next, we have that picture in figure 1, and the image() function displays it.


Then, we resize the picture to [227,227] since the AlexNet has been trained with images of the size [227,227] and our image must be compared with them. It will be easier to compare if the images are of the same dimensions.

picture = imresize(picture,[227,227]);

Next, we use the classify command to find the category to which the image belongs and we store that in variable 'label'.

label = classify(nnet, picture);

Finally, we use the label as the title for the image and that is displayed in uppercase characters. We also clear camera so that webcam is released at the end of execution.

clear camera;


Now, we can run the program. When we run the program, the webcam will be on automatically and keep the object to be captured near the webcam. Then we would get the image displayed along with its identified label. Here there are 2 categories where the program has identified the sunglasses and the coffee mug correctly.

Object recognition sun glasses
Object recognition coffee mug


This task can be extended to various other wide-ranging applications in today's life. For example:

  • When an image of a pedestrian crosswalk or a speed breaker is identified by a neural network, a self-driving vehicle slows down.
  • When an ATM can identify the image of an illegal note, it will automatically reject it.
  • A smartphone app instantly identifies a language and gives a translation of the foreign language.

This image recognition is especially well-suited to identification applications such as face recognition, text translation, and advanced driver assistance systems, including, lane classification and traffic sign recognition.

Object recognition

Source : Mathworks

Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 


An Electronics and Communication engineering student. Passionate about learning and sharing with others.

  • Abhishek Agrawal says:

    Nice implementation of deep learning algorithm for object detection using AlexNet Neural network

  • A very interesting blog, clearly understood how to use MATLAB for deep learning.

  • A very good application of Alexnet indeed, however, I would it would be great if, for example, the image cant be detected, it shows error message

  • tanu jain says:

    deep learning algorithm for object detection using AlexNet Neural network
    as direct is good for knowledge based project In matlab

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179