Image Acquisition Using MATLAB
MATLAB offers robust tools for image acquisition, allowing users to connect to imaging devices, capture images, and process them. This section provides an overview of image acquisition in MATLAB, examples of common problems, and their solutions.
Basics of Image Acquisition
Image acquisition involves capturing images from a connected device such as a webcam, digital camera, or other imaging hardware. MATLAB uses the Image Acquisition Toolbox
for this purpose.
Connecting to an Imaging Device
Use the imaqhwinfo
function to retrieve information about connected imaging devices:
% List connected imaging devices
info = imaqhwinfo;
disp(info);
% Display specific device information
deviceInfo = imaqhwinfo('winvideo');
disp(deviceInfo);
Acquiring an Image
Once a device is connected, use the following code to acquire an image:
% Connect to a webcam
vid = videoinput('winvideo', 1, 'RGB24_640x480');
% Start video preview
preview(vid);
% Capture a single frame
img = getsnapshot(vid);
% Display the image
imshow(img);
% Clean up
delete(vid);
clear vid;
Working with External Cameras Using MATLAB
This example demonstrates how to connect to an external camera using MATLAB’s Image Acquisition Toolbox and configure its properties such as resolution, frame rate, exposure time, brightness, contrast, gain, and region of interest (ROI).
Step-by-Step Guide
1. Connect to the Camera
First, use the imaqhwinfo
function to detect available cameras and connect to the desired one:
% Detect connected cameras
info = imaqhwinfo('winvideo');
disp(info);
% Connect to the first available camera
vid = videoinput('winvideo', 1);
% Display the default properties of the camera
disp(get(vid, 'ReturnedColorSpace'));
disp(get(vid));
2. Configure Camera Properties
Use the videoinput
and src
objects to adjust camera properties. Here’s how to configure the following properties:
(2.a) Resolution
% Set the camera resolution
vid = videoinput('winvideo', 1, 'RGB24_1280x720');
(2.b) Frame Rate
% Configure the frame rate (if supported)
src = getselectedsource(vid);
set(src, 'FrameRate', '30'); % Set frame rate to 30 fps
(2.c) Exposure Time
% Adjust the exposure time (in seconds)
set(src, 'Exposure', -4); % Lower values mean shorter exposure times
(2.d) Brightness, Contrast, and Gain
% Configure brightness, contrast, and gain
set(src, 'Brightness', 150); % Set brightness level
set(src, 'Contrast', 50); % Set contrast level
set(src, 'Gain', 5); % Set gain level
(2.e) Region of Interest (ROI)
Specify a subset of the image to capture:
% Define the region of interest (ROI) [x, y, width, height]
vid.ROIPosition = [100, 100, 400, 300];
3. Capture and Display an Image
% Start video acquisition
start(vid);
% Capture a frame
frame = getsnapshot(vid);
% Display the captured frame
imshow(frame);
4. Clean Up
Always release the video object after use:
% Stop video acquisition and delete the object
stop(vid);
delete(vid);
clear vid;
Expected Output
After running the above code, you should see a live preview or a captured image with the specified properties. Adjust the parameters to match your camera’s capabilities.
Common Problems and Solutions
Problem 1: Low Image Resolution
Solution: Adjust the resolution settings during acquisition:
% Set resolution to 1280x720
vid = videoinput('winvideo', 1, 'RGB24_1280x720');
% Capture an image
img = getsnapshot(vid);
imshow(img);
Problem 2: No Imaging Device Detected
Solution: Ensure the device is connected and drivers are installed. Use imaqhwinfo
to verify:
% Check hardware info
info = imaqhwinfo;
disp(info);
Problem 3: Processing Large Images
Solution: Resize the image or process it in smaller chunks:
% Resize the image
smallImg = imresize(img, 0.5);
imshow(smallImg);
Useful MATLAB Functions for Image Acquisition
Practice Questions
Test Yourself
1. Connect to a webcam and capture a live image. Convert it to grayscale and display both images side-by-side.
2. Perform edge detection on a captured image and save the resulting image to a file.
3. Capture an image with a resolution of 1920×1080, resize it to 640×480, and display the resized image.
4. Connect to a camera and configure the resolution to 1920×1080, set the frame rate to 60 fps, and capture a grayscale image.