Skip to content

Tutorial: Find the yellow square!

Paula Rudy edited this page Nov 17, 2015 · 4 revisions

Here's an easy example usecase to help get you started!

yellowtutorial1

In this tutorial we will try to find the yellow square in the image and display it's position. The setup is pretty simple, just a USB web camera connected to the computer looking down at some colorful objects. The yellow plastic square is the thing that we're interested in locating in the image.


Enable the image source

yellowtutorial2

The first step is to acquire an image. For this tutorial, we used a webcam (to use a webcam, click on the "Add Webcam" button and select the camera number), but if you want to follow along, you can download the image displayed above and add it using the "Add Image" button. Then you can select the image preview button and the real-time display of the camera stream (or the static image you provided, if you're following along from home) will be shown in the preview area.


##Resize the image

yellowtutorial3

In this case the camera resolution is too high for our purposes, and in fact the entire image cannot even be viewed in the preview window. The "Resize" operation is clicked from the Operation Palette to add it to the end of the pipeline. To help locate the Resize operation, type "Resize" into the search box at the top of the palette. The steps are:

  1. Type "Resize" into the search box on the palette
  2. Click the Resize operation from the palette. It will appear in the pipeline.
  3. Enter the x and y resize scale factor into the resize operation in the pipeline. In this case 0.25 was chosen for both.
  4. Drag from the Webcam image output mat socket to the Resize image source mat socket. A connection will be shown indicating that the camera output is being sent to the resize input.
  5. Click on the destination preview button on the "Resize" operation in the pipeline. The smaller image will be displayed alongside the larger original image. You might need to scroll horizontally to see both as shown.
  6. Lastly, click the Webcam source preview button (since there is no reason to look at both the large image and the smaller image at the same time!).

##Find only the yellow parts of the image

yellowtutorial4

The next step is to remove everything from the image that doesn't match the yellow color of the piece of plastic that is the object being detected. To do that a HSV Threshold operation is chosen to set upper and lower limits of HSV values to indicate which pixels should be included in the resultant binary image. Notice that the target area is white while everything that wasn't within the threshold values are shown in black. Again, as before:

  1. Type HSV into the search box to find the HSV Threshold operation.
  2. Click on the operation in the palette and it will appear at the end of the pipeline.
  3. Connect the dst (output) socket on the resize operation to the input of the HSV Threshold.
  4. Enable the preview of the HSV Threshold operation so the result of the operation is displayed in the preview window.
  5. Adjust the Hue, Saturation, and Value parameters only the target object is shown in the preview window.

##Get rid of the noise and extraneous hits

yellowtutorial5

This looks pretty good so far, but sometimes there is noise from other things that couldn't quite be filtered out. To illustrate one possible technique to reduce those occasional pixels that were detected, an Erosion operation is chosen. Erosion will remove small groups of pixels that are not part of the area of interest.


##Mask to just the yellow area from the original image

yellowtutorial6

Here a new image is generated by taking the original image and masking it with the the results of the erosion. This leaves just the yellow card as seen in the original image with nothing else shown. And it makes it easy to visualize exactly what was being found through the series of filters.


##Find the yellow area (blob)

yellowtutorial7

The last step is actually detecting the yellow card using a Blob Detector. This operation looks for a grouping of pixels that have some minimum area. In this case, the only non-black pixels are from the yellow card after the filtering is done. You can see that a circle is drawn around the detected portion of the image.