Environment CentOS7
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16) $ pkg-config --modversion opencv 2.4.13
Refer to "OpenCV Chinese Reference Manual"
Source code:
/************************* * * main.cpp * * Read in the picture and display 2018.05 in reverse color * * compile:g++ main.cpp `pkg-config --cflags --libs opencv` */ #include<stdlib.h> #include<stdio.h> #include<math.h> #include<cv.h> #include<highgui.h> /* handle mouse events */ void mouseHandler(int event, int x, int y, int flags, void* param) /*flags: CV_EVENT_FLAG_CTRLKEY, CV_EVENT_FLAG_SHIFTKEY, CV_EVENT_FLAG_ALTKEY, CV_EVENT_FLAG_LBUTTON, CV_EVENT_FLAG_RBUTTON, CV_EVENT_FLAG_MBUTTON*/ { printf("%d,%d,%d,%d\n",event,x,y,flags); switch(event){ /* left mouse button pressed */ case CV_EVENT_LBUTTONDOWN: printf("Left button down\n"); break; /* Left mouse button up */ case CV_EVENT_LBUTTONUP: printf("Left button up\n"); break; /*left mouse button*/ case CV_EVENT_LBUTTONDBLCLK: printf("Left button DBLCLK\n"); break; /* Right mouse button pressed */ case CV_EVENT_RBUTTONDOWN: printf("Right button down\n"); break; /*Right click on the mouse*/ case CV_EVENT_RBUTTONUP: printf("Right button up\n"); break; /*right click*/ case CV_EVENT_RBUTTONDBLCLK: printf("Right button DBLCLK\n"); break; /* Middle mouse button pressed */ case CV_EVENT_MBUTTONDOWN: printf("Middle button down\n"); break; /* Middle mouse button up*/ case CV_EVENT_MBUTTONUP: printf("Middle button up\n"); break; /* middle mouse button */ case CV_EVENT_MBUTTONDBLCLK: printf("Middle button DBLCLK\n"); break; /*Mouse move*/ case CV_EVENT_MOUSEMOVE: printf("Mouse move\n"); } } /* Slider handler */ void trackbarHandler(int pos) { printf("Trackbar position: %d\n",pos); } int main(int argc,char**argv) { /*image variable*/ IplImage *img = 0; int height, width, step, channels; uchar *data; int i, j, k; if(argc<2){ printf("Usage: main <image-file-name>\n\7"); exit(0); } /*load image*/ img = cvLoadImage(argv[1]); if(!img){ printf("Could not load image file: %s\n",argv[1]); exit(0); } /*Get image parameters*/ height = img->height; width = img->width; step = img->widthStep; channels= img->nChannels; data = (uchar*)img->imageData; /*display image*/ //cvShowImage("mainWin0",img); printf("Processing a %dx%d image with %d channels\n",height,width,channels); /* Create a window and automatically control the size */ cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); /*Control window size*/ cvMoveWindow("mainWin", 100, 100); /* Process the image: do whatever you want with it */ for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++) /*Invert color*/ data[i*step+j*channels+k]=255-data[i*step+j*channels+k]; /*display image*/ cvShowImage("mainWin", img ); /*mouse events*/ int mouseParam = 0; cvSetMouseCallback("mainWin",mouseHandler,&mouseParam); /*Keyboard events*/ int key; while(1){ key = cvWaitKey(0); printf("key=%d\n",key); /*Esc == 27*/ if(key == 27)break; switch(key){ case 'h':printf("press h\n"); break; case 'a':printf("press a\n"); break; case 'b':printf("press b\n"); break; case 'c':printf("press c\n"); break; case 'd':printf("press d\n"); break; default: break; } } /*Resize the window (resize according to the window name: only the window with this name is resized)*/ cvResizeWindow("mainWin",100,100); /* handle slider events */ int trackbarVal = 25; int maxVal = 200; cvCreateTrackbar("bar1","mainWin",&trackbarVal, maxVal, trackbarHandler); //Get the current slider position int pos = cvGetTrackbarPos("bar1","mainWin"); printf("pos = %d\n",pos); //Set the slider position cvSetTrackbarPos("bar1","mainWin",25); /* release this image */ cvReleaseImage(&img ); /*close the window*/ cvDestroyWindow("mainWin"); return 0; }
Compile and run:
$g++ main.cpp `pkg-config --cflags --libs opencv` $ ./a.out wong.jpg Processing a 301x286 image with 3 channels Left button down Left button up Right button down Right button up Middle button down Middle button up key=97 press a key=98 press b key=99 press c key=27 pos = 25
Pictures used: