Answers to exercises after learning opencv (Chapter 3) (for reference only)

Source: Internet
Author: User
Tags scalar

If the code passes through vs2008, add opencv_core220d.lib opencv_highgui220d.lib opencv_imgproc220d.lib to the additional dependency.

You can also add the following content in the Code:

  1. # Pragma comment (Lib, "opencv_core220d.lib ")
  2. # Pragma comment (Lib, "opencv_highgui220d.lib ")
  3. # Pragma comment (Lib, "opencv_imgproc220d.lib ")
#pragma comment(lib,"opencv_core220d.lib")  #pragma comment(lib,"opencv_highgui220d.lib")  #pragma comment(lib,"opencv_imgproc220d.lib") 

For details, see this article: configure the environment

Chapter 3:

2. This exercise will accustom you to the idea of your functions taking matrix types. create a two-dimen1_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:

#include "opencv\cv.h"#include "opencv\highgui.h"int main(){CvMat* mat = cvCreateMat(100,100,CV_8UC3);cvZero(mat);cvCircle(mat,cvPoint(50,50),30,CV_RGB(255,0,0),2,8);cvNamedWindow("xiti_ex3_2",CV_WINDOW_AUTOSIZE);cvShowImage("xiti_ex3_2",mat);cvWaitKey(0);//cvReleaseImage(&mat);cvDestroyWindow("xiti_ex3_2");}

Test:

3. Create a two-dimen1_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:

#include "opencv\cv.h"#include "opencv\highgui.h"int main(){CvMat* mat = cvCreateMat(100,100,CV_8UC3);cvZero(mat);int i,j;for(i = 20;i < 40; ++i){for(j = 5;j < 20; ++j){uchar* p = cvPtr2D(mat,i,j);p[1] = 255;}}cvNamedWindow("drawRectangle",CV_WINDOW_AUTOSIZE);cvShowImage("drawRectangle",mat);cvWaitKey(0);cvDestroyWindow("drawRectangle");}

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:

#include "opencv\cv.h"#include "opencv\highgui.h"int main(){IplImage* img = cvCreateImage(cvSize(100,100),8,3);cvZero(img);for(int y = 5;y<= 20;++y){uchar* ptr = (uchar*)(img ->imageData + y * img ->widthStep);for(int x = 20;x <= 40;++x){ptr[3 * x + 1] = 255;}}cvNamedWindow("drawRectangle",CV_WINDOW_AUTOSIZE);cvShowImage("drawRectangle",img);cvWaitKey(0);cvReleaseImage(&img);cvDestroyWindow("drawRectangle");}

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 shold be 0, the next inner border shold be 20,
The next inner border shoshould be 40, and so on until the fi nal innermost square is set to value 200; all borders shoshould be 10 pixels wide. display the image.

Solution:

# Include "opencv \ cv. H "# include" opencv \ highgui. H "int main () {iplimage * IMG = cvcreateimage (cvsize (210,210), ipl_depth_8u, 1); cvzero (IMG); int x = 0; int y = 0; int scalar = 0; int add = 0; while (add <200) {cvsetimageroi (IMG, cvrect (X, Y, 210-add, 10); cvset (IMG, cvscalar (scalar); // value x + = 10; y + = 10; scalar + = 20; add + = 20; cvresetimageroi (IMG );} cvnamedwindow ("example", cv_window_autosize); cvshowimage ("example", IMG); cvwaitkey (0); cvreleaseimage (& IMG); cvdestroywindow ("example ");}

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
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 shoshould have two inverted rectangles
Within the larger image .,

Solution:

#include "opencv\cv.h"#include "opencv\highgui.h"int main(){IplImage* img = cvLoadImage("HUST.jpg");IplImage* img1;IplImage* img2;img1 = cvCreateImageHeader(cvSize(20,30),img ->depth,img ->nChannels);img1 ->origin = img ->origin;img1 ->widthStep = img ->widthStep;img2 = cvCreateImageHeader(cvSize(20,30),img ->depth,img ->nChannels);img2 ->origin = img ->origin;img2 ->widthStep = img ->widthStep;img1 ->imageData = img ->imageData + 10 * img ->widthStep +5 *  img ->nChannels;img2 ->imageData = img ->imageData + 60 * img ->widthStep +50 *  img ->nChannels;cvNot(img1,img1);cvNot(img2,img2);cvNamedWindow("Example",CV_WINDOW_AUTOSIZE);cvShowImage("Example",img);cvWaitKey(0);cvReleaseImage(&img1);cvReleaseImage(&img2);cvReleaseImage(&img);cvDestroyWindow("Example");}

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:

# Include "opencv \ cv. H "# include" opencv \ highgui. H "# include <iostream> using namespace STD; int main () {iplimage * IMG = cvloadimage (" hust.jpg "); // display the original image cvnamedwindow (" img_pre ", cv_window_autosize); cvshowimage ("img_pre", IMG); // create three-channel image iplimage * imgred = cvcreateimage (cvsize (IMG-> width, IMG-> height ), IMG-> depth, 1); iplimage * imggreen = cvcreateimage (cvsize (IMG-> width, IMG-> height), IMG-> depth, 1 ); iplimage * imgblue = cvcreateimage (cvsize (IMG-> width, IMG-> height), IMG-> depth, 1 ); // extract the R, G, and B-channel cvsplit (IMG, imggreen, imgblue, imgred, null); // display the green image cvnamedwindow ("img_green", cv_window_autosize ); cvshowimage ("img_green", imggreen); // clone the green image twice iplimage * cursor = cvcloneimage (imggreen); iplimage * imggreenclone2 = cvcloneimage (imggreen); double maxnum, minnum; cvminmaxloc (imggreen, & minnum, & maxnum); cout <"minnum in imggreen:" <minnum <Endl; cout <"maxnum in imggreen: "<maxnum <Endl; double scalar = (maxnum-minnum)/2; cvset (imggreenclone1, cvscalar (scalar); cvzero (imggreenclone2); cvcmp (imggreen, imggreenclone1, dimensions, dimensions); cvsubs (imggreen, cvscalar (scalar/2), imggreen, dimensions); cvnamedwindow ("imggreen_after", cv_window_autosize); cvshowimage ("imggreen_after", imggreen ); cvwaitkey (0); cvreleaseimage (& IMG); cvreleaseimage (& imggreen); cvdestroywindow ("img_pre"); cvdestroywindow ("img_green"); cvreleaseimage (& imggreenclone2 ); cvdestroywindow ("imggreen_after ");}

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:

# Include "opencv \ cv. H "# include" opencv \ highgui. H "# include <iostream> using namespace STD; typedef struct {int I; cvpoint pt; cvrect rect;} my_struct; void write_my_struct (cvfilestorage * FS, const char * Name, my_struct * ms) {assert (FS! = 0); cvwriteint (FS, "I", MS-> I); cvstartwritestruct (FS, "PT", cv_node_seq); cvwriteint (FS, 0, MS-> PT. x); cvwriteint (FS, 0, MS-> PT. y); cvendwritestruct (FS); cvstartwritestruct (FS, "rect", cv_node_seq); cvwriteint (FS, 0, MS-> rect. x); cvwriteint (FS, 0, MS-> rect. y); cvwriteint (FS, 0, MS-> rect. width); cvwriteint (FS, 0, MS-> rect. height); cvendwritestruct (FS);} void read_my_struct (cvfilestorage * FS, cvfilenode * ms_node, my_struc T * ms) {assert (FS! = 0); MS-> I = cvreadintbyname (FS, 0, "I", 0); cvseq * s = cvgetfilenodebyname (FS, 0, "PT")-> data. seq; MS-> PT. X = cvreadint (cvfilenode *) cvgetseqelem (S, 0); MS-> PT. y = cvreadint (cvfilenode *) cvgetseqelem (s, 1); cvseq * S1 = cvgetfilenodebyname (FS, 0, "rect")-> data. seq; MS-> rect. X = cvreadint (cvfilenode *) cvgetseqelem (S1, 0); MS-> rect. y = cvreadint (cvfilenode *) cvgetseqelem (S1, 1); MS-> rect. width = cvreadint (cvfilenode *) cvgetseqelem (S1, 2); MS-> rect. height = cvreadint (cvfilenode *) cvgetseqelem (S1, 3); // output the read result cout <"my_struct:" <Endl; cout <"I: "<MS-> I <Endl; cout <" PT :( "<MS-> PT. x <"," <MS-> PT. Y <")" <Endl; cout <"rect :(" <MS-> rect. x <"," <MS-> rect. Y <"," <MS-> rect. width <"," <MS-> rect. height <Endl;} int main () {// write data to the XML file cvfilestorage * FS = cvopenfilestorage ("cfg. XML ", 0, cv_storage_write); my_struct MS; Ms. I = 100; ms.pt. X = 10; ms.pt. y = 20; Ms. rect. X = 30; Ms. rect. y = 40; Ms. rect. width = 50; Ms. rect. height = 60; write_my_struct (FS, null, & MS); cvreleasefilestorage (& FS); // read data from the XML file cvfilestorage * fs1 = cvopenfilestorage ("cfg. XML ", 0, cv_storage_read); my_struct ms1; read_my_struct (fs1, null, & ms1); cvreleasefilestorage (& fs1 );}

Test:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.