Tutorial: Color Image Processing

Tutorial: Color Image Processing

​

Tutorial: Color Image Segmentation

I. Introduction

In this tutorial, you are learn how to segment a colored object using a web-cam. We will use inRange() algorithm to segment the moving colored object and draw contour or boundary boxes around the tracked object.

II. Tutorial

OpenCV: inRange()

First, read the OpenCV documentation read here

Parameters

  • src first input array.

  • lowerb inclusive lower boundary array or a scalar.

  • upperb inclusive upper boundary array or a scalar.

  • dst output array of the same size as src and CV_8U type.

Example: Color Segmentation

One way of choosing the appropriate InRange conditions is analyzing the pixel statistic of a small sub-window within the targeted colored area.

Download the tutorial source code file

Choose the target window to analyze with mouse click and drag.

See Appendix for the MouseEvent code

Analyze for the standard deviation and mean of the targeted color within the window.

Add slidebars to change the InRange values of each R, G, B or H, S, V and segment each colored ball.

Apply appropriate morphology (i.e. dilation/erosion/fill holes) to the output binary images to cluster the detected objects into meaningful blobs.

Find all contours and select the contour with the largest area

Draw the contour and a box over the target object

Now, segment other color balls

III. Exercise

Drawing the trajectory of a colored object with Webcam

Modify your tutorial program to keep drawing the contour on a white or black background image.

  • Use your webcam for the source image data.

  • Use any colored object as the target.

  • Draw rectangles or circles for the output display.

Appendix

Sample code: On Mouse Event

Last updated

Was this helpful?