Exercises

In the following exercises, you may need to refer to the CxCore manual that ships with OpenCV or to the OpenCV Wiki on the Web for details of the functions outlined in this chapter.

  1. Find and open …/opencv/cxcore/include/cxtypes.h. Read through and find the many conversion helper functions.

  2. This exercise will accustom you to the idea of many functions taking matrix types. Create a two-dimensional matrix with three channels of type byte with data size 100-by-100. Set all the values to 0.

  3. Create a two-dimensional matrix with three channels of type byte with data size 100-by-100, and set all the values to 0. Use the pointer element access function cvPtr2D to point to the middle ("green") channel. Draw a green rectangle between (20, 5) and (40, 20).

  4. Create a three-channel RGB image of size 100-by-100. Clear it. Use pointer arithmetic to draw a green square between (20, 5) and (40, 20).

  5. Practice using region of interest (ROI). Create a 210-by-210 single-channel byte image and zero it. Within the image, build a pyramid of increasing values using ROI and cvSet(). That is: the outer border should be 0, the next inner border should be 20, the next inner border should be 40, and so on until the final innermost square is set to value 200; all borders should be 10 pixels wide. Display the image.

  6. Use multiple image headers for one image. Load an image that is at least 100-by-100. Create two additional image headers and set their origin, depth, number of channels, and widthstep to be the same as the loaded image. In the new image headers, set the width at 20 and the height at 30. Finally, set their imageData pointers to point to the pixel at (5, 10) and (50, 60), respectively. Pass these new image subheaders to cvNot(). Display the loaded image, which should have two inverted rectangles within the larger image.

  7. Create a mask using cvCmp(). Load a real image. Use cvSplit() to split the image into red, green, and blue images.

    1. Find and display the green image.

    2. Clone this green plane image twice (call these clone1 and clone2).

    3. Find the green plane's minimum and maximum value.

    4. Set clone1's values to thresh = (unsigned char)((maximum - minimum)/2.0).

    5. Set clone2 to 0 and use cvCmp(green_image, clone1, clone2, CV_CMP_GE). Now clone2 will have a mask of where the value exceeds thresh in the green image.

    6. Finally, use cvSubS(green_image,thresh/2, green_image, clone2) and display the results.

  8. Create a structure of an integer, a CvPoint and a CvRect; call it "my_struct".

    1. Write two functions: void write_my_struct(CvFileStorage * fs, const char * name, my_struct *ms) and void read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms). Use them to write and read my_struct.

    2. Write and read an array of 10 my_struct structures.