Color-Based Segmentation with Live Image Acquisition
Acquire an image and perform image analysis to find small objects, count them, and differentiate them by color.
Hello, and welcome. In this video, we will use Image Acquisition Toolbox™ to capture images into MATLAB®. And we will then perform image analysis and other operations using Image Processing Toolbox™. As an example, I have set up my camera to capture images of candies of different colors. We will determine the total number of candies in the image and count the number of candies of a certain color.
In this example, I will perform various image processing algorithms, such as thresholding, morphological operations, and color segmentation on the acquired images. The first step is to connect my camera to MATLAB. To see if MATLAB recognizes that my camera is available, I use the imaqhwinfo command to get a list of the hardware adapters and specific devices available on my computer. The camera that I currently have connected is accessible through the winvideo adapter. So I use the imaqhwinfo command to obtain its device ID.
With this information, I can use the video input command to create an instance of an image acquisition object that can acquire video from my camera. Using this video input object, I can bring up a preview window for my captured video. I then use the getsnapshot command to capture one image frame from my acquired video. I can display this image frame using the imshow command inside MATLAB. Let me dock this image frame here.
Let us first identify all objects in the image using thresholding. So I separate the R, G, and B compoments of my image and compute a threshold value for each of them separately using the graythresh command. I then threshold each of those components separately using the im2bw command and then combine them together to have a binary image with clearly identified objects.
To remove any artifacts in the image, I use morphological operations such as imfill to remove any holes and imclearborder to remove any extraneous objects lying on the border. It is now very easy to distinctly count the number of objects in my image. I use the bwlabel command to uniquely label each object and count the total number of objects in the image.
To robustly select candies of a certain color, we need to account for variation in color values across an object caused by problems such as non-uniform lighting conditions and camera noise. So I need to construct an image that replaces the actual RGB values with the median color of each candy.
To do this, I use logical indexing to get only those pixels that I want and then calculate their median. For instance, I can logically index into the images label matrix and extract only those pixels with a label of 1. Let me show an example here.
I can then calculate the median of the extracted pixels that belong to this segmented region. So in this for loop, I have thus computed the median values for all three color planes for each label. And at the end of the loop, I recombine the color components to know the medium color value for each candy.
Using the impixelinfo command from the Image Processing Toolbox, I can see the median R, G and B values that I just computed for each candy. I now want to select the desired color and count the number of candies for that particular color. Since I already had the median color of each labeled region, I need to simply find the regions that have a median color close to the selected color.
For color-based selection, it is better to work with a LAB Color Space, because it separates out luminescence information from color information. The Euclidean distance between color values in this space is a more effective measure for color similarity than in the RGB color space. So I've used the makecform and the applycform comments to convert my image and the selected color from RGB to LAB space.
From this, I can keep the A and B components as I don't really need any luminescence information here. To find regions with the same color, I first calculate the Euclidean distance between the color values using the hypot command with the A and B components. I then use an appropriate threshold hold value to choose only those regions that lie close to the selected color. I then can use the bwlabel command to obtain account of the chosen regions.
So this demo illustrated how Image Processing Toolbox and Image Acquisition Toolbox can be combined to perform image analysis operations for a live feed of images. Taking this example a step further, you could add a GUI with guide or even build a standalone executable with MATLAB Compiler™. For more image acquisition and processing demos, please visit the product pages on our web site. Thank you.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本Japanese (日本語)
- 한국Korean (한국어)