Getting Started with Webcam and IPCam in Matlab
May 27, 2021
In this tutorial, we will look at how to install the WEBCAM and IPCAM hardware package for MATLAB, which is necessary to access the hardware. We will also look at how to access the laptop’s in-built WEBCAM and external USB webcam with Matlab code, to take snapshots, preview, and record video clips.
We will understand more about the android application to convert mobile phone’s camera as an IPCAM using Matlab.
A Web Camera (WEBCAM) is connected to a PC and can be used to make videos available to other users. The connection is normally through an USB. IP Cameras (IPCAM) involve the combination of high-end web functionality, PC and network interface to one network-ready product.
To follow this tutorial along - the reader will need the following:
- Matlab installed on your PC.
- Webcam and Ipcam support packages installed.
- Webcam Android application installed on your mobile phone.
Table of contents
- Need for camera interface
- Installation of hardware support package
- Matlab code for accessing laptop’s inbuilt webcam and an external USB webcam
- How to access external USB camera
- Matlab code for capturing a live video clip
- Making mobile phone as an IP camera
- How to access the live video stream from your IP to your Matlab
Need for camera interface
There are several projects which require either images or videos as primary input.
Few use cases where we require image or video as input are:
- Face identification and recognition.
- Face recognition-based security system and attendance system.
- Facial expression recognition.
- Object identification and classification.
- Fire detection on site.
- Vehicle license plate recognition and vehicle logging system.
- and many more…
Hardware support package installation
- Log in to your MathWork account at the top right corner of the Matlab window.
Signing into MathWork account
- Click on the
Add-Onsdrop-down arrow and select the
Get hardware support package.
Add-Ons to download hardware support package
- The hardware support package opens up and on the search tab, type
MATLAB support package for USB webcam.
Search for MATLAB Support Package for USB webcam
- Then, click on the
Installation of package
For our case, the push button for
Install changes to
Manage since the package is already installed on our PC.
Since we are accessing the IP camera, we need the IP support package.
- Using the same process above, we will locate the
MATLAB support package for IP cameraand then install it.
Installing support package for IP Camera
Matlab code for accessing laptop’s inbuilt webcam and an external USB webcam
To see the list of all attached cameras to your PC, execute the code below on the console:
First, create a camera object with a
webcam. Here, we use
cam1 as the object:
cam1 = webcam %integrated webcam
We then execute using
preview(). This is to see the live stream of the video your camera object that is being captured.
You can take a snapshot using
snapshot() and save the frame.
This is done by executing the snapshot function and view the captured image using the
The captured image is then stored in the variable
img1 as shown:
img1 = snapshot(cam1); imshow(img1)
Captured image using Internal web camera
Clear the camera object
cam1 after the live stream and the snapshot, to free up the memory.
How to access external USB camera?
The command remains the same as that of accessing the internal webcam.
The difference is that you need to change the hardware name when defining the webcam.
cam2 = webcam('USB2.0 PC camera'); preview(cam2); img2 = snapshot(cam2); imshow(img2)
Captured image using External web camera
Matlab code for capturing a live video clip
To capture a live video clip, we first need to define the
cam = webcam('USB2.0 PC camera');
Then, we specify the duration
t to capture our video and define the frames per second
t = 20; % the time is in second fps = 30;
The number of frames
nof will be the product of the time
t and the frames per second
We implement the above as follows:
nof = t* fps
We then declare the video writer object
videowriter() with your file name. This means that your output will be saved in the current directory folder with the file name of
vidwriter = videowriter('myvideo.mp4') open(vidwriter)
Now, we define a
for loop to loop through the frames. This defines the number of frames you want to capture.
Here, we loop from
nof as shown:
for index = 1: nof img = snapshot(cam); %this is to acquire the frame
We then write (save) the video by executing the
writeVideo(vidWriter, img); pause(t/nof) end
By executing the
snapshot(cam), each snapshot will be stored in
img, and at the same-time writing to
pause(t/nof) command is a pause given proportionally to create the
Once this is done, we close the
vidWrite and clear the camera object
close(vidWriter) clear cam
Making a mobile phone an IP camera
To make your mobile phone an IP camera, you need a third-party Android application by which you can convert your mobile phone as an IP camera.
To find and use the Android application, follow the steps below:
- Go to the
Google Play Storeon your mobile phone.
- Search for IPCAM.
Note that, you will find many such applications. IP Webcam by Pavel (developer) is the most popular and preferred for converting mobile phones to IPCAMs.
Screenshot of the app to be downloaded
Once it is installed, follow the steps below to view the live stream video:
- Scroll down and select the start server option.
Starting the server
- Once the server is selected, the live stream video is visible on your phone.
Note that, at the lower part of the screen, you can see the IPv4 addresses.
IP addresses of live webcamera
IPv4 address will be used to access the camera from our Matlab. So, we should make note of it.
This IP address can also be opened in your PC browser to see the live streaming from the mobile IP camera.
This can be done by simply copying your IP address by long press of your screen and searching it in your browser.
Note that, the Phone and the PC must connected to the same WiFi network.
How to access the live video stream from your IP to your Matlab?
We first create a camera object
cam3. When creating the camera object, we need the IP address of the IP camera.
cam3 = ipcam('http://192.168.1.4:8080/video'); %execute the preview command to get the video on your Matlab preview(cam3)
For more information about the topic, check here
Matlab is a better software to use for live video streaming. This is because it has support packages that make this process easier.
It also easier to use and understand, since the codes are not bulky.
IPCAM and WEBCAM are very important. They can be utilized for every sector to improve and enhance the security. They can also be used to capture images for further analysis and projects related to data science.
Peer Review Contributions by: Srishilesh P S
About the authorQueenter Bruce
Queenter Bruce is a computer science student at the Technical University of Mombasa. She does Artificial Intelligence and Machine Learning. Her hobbies include reading novels, playing football, and traveling.