《学习OpenCV》课后习题解答(第三章)(仅供参考)
代码在VS2008下通过,要在附加依赖项中添加:opencv_core220d.lib opencv_highgui220d.lib opencv_imgproc220d.lib。
也可以在代码里面添加以下内容:
- #pragma comment(lib,"opencv_core220d.lib")
- #pragma comment(lib,"opencv_highgui220d.lib")
- #pragma comment(lib,"opencv_imgproc220d.lib")
具体可见这篇文章:配置环境
Chapter 3:
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.
a. 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 ).
b. Display this image using methods described in Chapter 2.
Solution:
Test:
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).
Solution:
Test:
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).
Solution:
Test: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(). Th at 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 fi nal innermost square is set to value 200; all borders should be 10 pixels wide. Display the image.
Solution:
Test:
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.、
Solution:
Test:7、Create a mask using cvCmp(). Load a real image. Use cvSplit() to split the image into red, green, and blue images.
a. Find and display the green image.
b. Clone this green plane image twice (call these clone1 and clone2).
c. Find the green plane’s minimum and maximum value.
d. Set clone1’s values to thresh = (unsigned char)((maximum - minimum)/2.0).
e. 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.
f. Finally, use cvSubS(green_image,thresh/2, green_image, clone2) and display the results.
Solution:
Test:8、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.
Solution:
Test: