WOLFRAM
Products
Wolfram|One
Mathematica
Wolfram Notebook Assistant + LLM Kit
Wolfram|Alpha Notebook Edition
System Modeler
All Products
Consulting & Solutions
Wolfram Consulting
Industry Solutions
Solutions for Education
Learning & Support
Wolfram U Courses
Wolfram Language Resources
Wolfram Community
Support FAQs
Contact Support
Company
About Wolfram
Careers
Events
Educational Programs
All Sites and Resources
Wolfram|Alpha
Wolfram Cloud
Your Account
Your Account
User Portal
Search
Close
Enable JavaScript to interact with content and submit forms on Wolfram websites.
Learn how
Legacy Documentation
Digital Image Processing
(2000)
This is documentation for an obsolete product.
Current products and services
User's Guide
Image Segmentation
7.6 Example: Color Image Segmentation
In this example we use clustering to segment an example image according to color features. Subsequently we show examples of color-selective masking operations. This is accomplished by clustering the pixel values in the 3D RGB color space. The determination of the number of clusters comes from a visual examination of the example image. Iterative refinement of the clustering outcome is possible based on a global classification measure. Alternatively, the classification result may be refined in a postprocessing step, by removing outliers from each of the clusters and appropriately labeling them as belonging to none of the selected color classes.
In[1]:=
This reads the
beans
image and extracts a subregion for further processing.
In[2]:=
Here we cluster the color pixel values into three groupings according to their RGB values. The clusters define background, green bean, and red bean pixels. The initial seed values are estimates of typical color values in these three regions.
In[3]:=
Here we verify the resulting clustering by listing the cluster centers.
In[4]:=
Out[4]=
This shows the number of pixels in each of the color clusters.
In[5]:=
Out[5]=
This shows a scatter diagram of the cluster assignment of all the image pixels.
In[6]:=
Out[6]=
Given these classification results, we can now proceed with selective image operations. Here we find the positions of all the red pixels in the example image.
In[7]:=
Here we flood the region occupied by red beans with a uniform red color.
In[8]:=
This shows the result.
In[9]:=
Out[9]=
Here we mask all the nonred pixels, which includes the background and green bean pixels, with a uniform gray value. First we find the positions of all the pixels that belong to this category.
In[10]:=
This replaces all affected pixels with a uniform gray value.
In[11]:=
This shows the result.
In[12]:=
Out[12]=