Color Segmentation Sample

Microsoft Robotics

Glossary Item Box

Microsoft Robotics Developer StudioSend feedback on this topic

Color Segmentation Sample

The Color Segment service analyzes images from a webcam, or simulated webcam, and based on color definitions declared in the service state, identifies color regions in each image.

This sample is provided in the C# language. You can find the project files for this sample at the following location under the Microsoft Robotics Developer Studio installation folder:

Sample location




Typically this sample would require a webcam. It can be run using a simulated webcam in the simulation environment.


You will also need Microsoft Internet Explorer or another conventional web browser.

Running the ColorSegment service

To start the ColorSegment service, start a node by using following command from the DSS Command Prompt:

dsshost /p:50000 /m:"samples\config\Colorsegment.manifest.xml"

This will start the ColorSegment service and the default WebCam service sample. To interact with the running ColorSegment service navigate to http://localhost:50000/colorsegment, this will present the service's user interface.

User Interface


The ColorSegment web-base user interface has the following sections.

  • Images - the source camera image on the left, the analyzed image on the right.
  • Live camera controls
    • The Refresh Interval text box allows you to set the rate at which your browser reads images from the camera, this interval is specified in milliseconds.
    • The Start button start live updates of the images
    • The Stop button pauses live updates of the images
    • The Refresh button gets the current image from the camera and the ColorSegment service.
  • Settings
    • The Threshold setting is a measure of how closely a color has to match, the larger the number, the less precise the match has to be.
    • The Minimum Blob Size setting is the smallest set of connected pixels that will be reported to a subscribing service. This is measured in pixels.
    • The Change button commits changes to the settings to the service. Note: changes to settings do not take effect until this button is pressed.
  • Color Definitions - Each color definition has the following parts.
    • Name - This is a name given to the color when it was first added. All definitions with the same name are considered together, and when displayed in the segmented image, pixels are colored with the color of the first color definition with the common name.
    • Y Cb Cr - This is the color as defined in Y Cb Cr colorspace. All color matching is done internally in this color space. The color definition also shows the range of colors that are considered in each dimension.
    • R G B - This is the color as defined in the R G B colorspace.
    • Delete - Pressing this button deletes the color definition
    • Expand Y - This expands the range of colors that are considered in the Y dimension of Y Cb Cr colorspace. This causes the ColorSegment service to consider lighter and darker colors to be similar.

Defining Colors

To define a color, perform the following sequence of actions.

  • Identify a region in the source image that has the color that you want to define.
  • Click on one corner of that region in the source image.
  • Click on another corner of the region. This will cause a red rectangle to be displayed over the image showing the area that will be considered when defining the color.
  • If you are satisfied with the region that has been selected:
    • Click on the name of an existing color definition, or
    • Type the name of a new color definition in the Name text box of the Add Entry row at the bottom of the list of definitions.
  • Click on the Add button to add the new definition.

This process sends the coordinates of the rectangle and the name entered, to the ColorSegment service. The service then performs a simple statistical analysis of the selected area in the current source image to determine the most representative color, and the range of colors found in that area.

Using the ColorSegment Service from another service

To use the ColorSegment service from another service you need to add the ColorSegment as a partner to your service.

First add a reference to the ColorSegment service proxy to your project, then at the top of the service add a using statement.

using cs = Microsoft.Robotics.Services.Sample.ColorSegment.Proxy;

Then in your service class you need to declare the partner.

[Partner("ColorSegment", Contract = cs.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UsePartnerListEntry)]
cs.ColorSegmentOperations _colorSegmentPort = new cs.ColorSegmentOperations();
cs.ColorSegmentOperations _colorSegmentNotify = new cs.ColorSegmentOperations();

In the Start method of your class, Subscribe to the ColorSegment service, in this example we only subscribe to the cs.UpdateColorAreas message. Also activate a receiver for the notifications from the service

protected overide void Start()

    _colorSegmentPort.Subscribe(_colorSegmentNotify, typeof(cs.UpdateColorAreas));

        Arbiter.Receive<cd.UpdateColorAreas>(true, _colorSegmentNotify, OnUpdateColorAreas)

Then implement the handler method that will be called when the ColorSegment service processes an image frame and identifies a color

void OnUpdateColorAreas(cs.UpdateColorAreas update)
    // process color area information




© 2012 Microsoft Corporation. All Rights Reserved.