Exercises

  1. This chapter completes our introduction to basic I/O programming and data structures in OpenCV. The following exercises build on this knowledge and create useful utilities for later use.

    1. Create a program that (1) reads frames from a video, (2) turns the result to grayscale, and (3) performs Canny edge detection on the image. Display all three stages of processing in three different windows, with each window appropriately named for its function.

    2. Display all three stages of processing in one image.

      Hint: Create another image of the same height but three times the width as the video frame. Copy the images into this, either by using pointers or (more cleverly) by creating three new image headers that point to the beginning of and to one-third and two-thirds of the way into the imageData. Then use cvCopy().

    3. Write appropriate text labels describing the processing in each of the three slots.

  2. Create a program that reads in and displays an image. When the user's mouse clicks on the image, read in the corresponding pixel (blue, green, red) values and write those values as text to the screen at the mouse location.

    1. For the program of exercise 1b, display the mouse coordinates of the individual image when clicking anywhere within the three-image display.

  3. Create a program that reads in and displays an image.

    1. Allow the user to select a rectangular region in the image by drawing a rectangle with the mouse button held down, and highlight the region when the mouse button is released. Be careful to save an image copy in memory so that your drawing into the image does not destroy the original values there. The next mouse click should start the process all over again from the original image.

    2. In a separate window, use the drawing functions to draw a graph in blue, green, and red for how many pixels of each value were found in the selected box. This is the color histogram of that color region. The x-axis should be eight bins that represent pixel values falling within the ranges 0–31, 32–63,…, 223–255. The y-axis should be counts of the number of pixels that were found in that bin range. Do this for each color channel, BGR.

  4. Make an application that reads and displays a video and is controlled by sliders. One slider will control the position within the video from start to end in 10 increments; another binary slider should control pause/unpause. Label both sliders appropriately.

  5. Create your own simple paint program.

    1. Write a program that creates an image, sets it to 0, and then displays it. Allow the user to draw lines, circles, ellipses, and polygons on the image using the left mouse button. Create an eraser function when the right mouse button is held down.

    2. Allow "logical drawing" by allowing the user to set a slider setting to AND, OR, and XOR. That is, if the setting is AND then the drawing will appear only when it crosses pixels greater than 0 (and so on for the other logical functions).

  6. Write a program that creates an image, sets it to 0, and then displays it. When the user clicks on a location, he or she can type in a label there. Allow Backspace to edit and provide for an abort key. Hitting Enter should fix the label at the spot it was typed.

  7. Perspective transform.

    1. Write a program that reads in an image and uses the numbers 1–9 on the keypad to control a perspective transformation matrix (refer to our discussion of the cvWarpPerspective() in the Dense Perspective Transform section of Chapter 6). Tapping any number should increment the corresponding cell in the perspective transform matrix; tapping with the Shift key depressed should decrement the number associated with that cell (stopping at 0). Each time a number is changed, display the results in two images: the raw image and the transformed image.

    2. Add functionality to zoom in or out?

    3. Add functionality to rotate the image?

  8. Face fun. Go to the /samples/c/ directory and build the facedetect.c code. Draw a skull image (or find one on the Web) and store it to disk. Modify the facedetect program to load in the image of the skull.

    1. When a face rectangle is detected, draw the skull in that rectangle.

      Hint: cvConvertImage() can convert the size of the image, or you could look up the cvResize function. One may then set the ROI to the rectangle and use cvCopy() to copy the properly resized image there.

    2. Add a slider with 10 settings corresponding to 0.0 to 1.0. Use this slider to alpha blend the skull over the face rectangle using the cvAddWeighted function.

  9. Image stabilization. Go to the /samples/c/ directory and build the lkdemo code (the motion tracking or optical flow code). Create and display a video image in a much larger window image. Move the camera slightly but use the optical flow vectors to display the image in the same place within the larger window. This is a rudimentary image stabilization technique.

Get Learning OpenCV now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.