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.
Find and open …/opencv/cxcore/include/cxtypes.h. Read through and find the many conversion helper functions.
Choose a negative floating-point number. Take its absolute value, round it, and then take its ceiling and floor.
Generate some random numbers.
Create a floating point CvPoint2D32f
and
convert it to an integer CvPoint
.
Convert a CvPoint
to a CvPoint2D32f
.
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.
Draw a circle in the matrix using void cvCircle(CvArr*
img, CvPoint center, intradius, CvScalar color, int thickness=1, int line_type=8,
int shift=0).
Display this image using methods described in Chapter 2.
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).
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).
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.
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.
Create a mask using cvCmp()
. Load a real image.
Use cvSplit()
to split the image into red, green, and
blue images.
Find and display the green image.
Clone this green plane image twice (call these clone1 and clone2).
Find the green plane's minimum and maximum value.
Set clone1's values to thresh = (unsigned char)((maximum - minimum)/2.0).
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.
Finally, use cvSubS(green_image,thresh/2, green_image,
clone2)
and display the results.
Create a structure of an integer, a CvPoint
and a
CvRect
; call it "my_struct".
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
.
Write and read an array of 10 my_struct
structures.