Advertisement
Guest User

Untitled

a guest
May 28th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <highgui.h>
  2. #include <cv.h>
  3. #include <cxcore.h>
  4. #include <stdio.h>
  5.  
  6. [CUT]
  7. int mouseRect();
  8.  
  9. int main(){
  10.     printf("Menu - Select Apps \n \n");
  11.    
  12. [CUT]
  13.     printf("8 - Mouse Rect");
  14.     printf("\n Your Choose: ");
  15.  
  16.  
  17.     char c = getchar();
  18.  
  19.     switch (c) {
  20. [CUT]
  21.     case '8':
  22.         printf("\n Mouse Rect");
  23.         mouseRect();
  24.         break;
  25.     default:
  26.         break;
  27.     }
  28.     return 0;
  29. }
  30.  
  31. void my_mouse_callback(int event, int x, int y, int flags, void* param);
  32.  
  33. CvRect box;
  34. bool drawing_box = false;
  35.  
  36. void draw_box (IplImage *img, CvRect rect) {
  37.  
  38.     cvRectangle(img, cvPoint(box.x, box.y), cvPoint(box.x+box.width, box.y+box.height), CV_RGB(0, 0, 255), 1, 8, 0);
  39. }
  40.  
  41. // implementazione mouse callback
  42. void my_mouse_callback(int event, int x, int y, int flags, void *param) {
  43.    
  44.     IplImage *image = (IplImage *)param;
  45.  
  46.     switch (event) {
  47.     case CV_EVENT_MOUSEMOVE:
  48.         if (drawing_box) {
  49.             box.width = x - box.x;
  50.             box.height = y - box.y;
  51.         }
  52.         break;
  53.     case CV_EVENT_LBUTTONDOWN:
  54.         drawing_box = true;
  55.         box = cvRect(x, y, 0, 0);
  56.         break;
  57.     case CV_EVENT_LBUTTONUP:
  58.         drawing_box = false;
  59.         if(box.width < 0) {
  60.             box.x += box.width;
  61.             box.width *= -1;
  62.         }
  63.         if(box.height < 0) {
  64.             box.y += box.height;
  65.             box.height *= -1;
  66.         }
  67.         draw_box(image, box);
  68.         break;
  69.     }
  70. }
  71. int mouseRect(int argc, char** argv[]) {
  72.     const char *name = "Mouse Box";
  73.     box = cvRect(-1, -1, 0, 0);
  74.  
  75.     IplImage *image = cvLoadImage("/home/maurizio/openCV/aur.jpg", CV_LOAD_IMAGE_COLOR);
  76.     cvZero(image);      // azzera img
  77.     IplImage *temp = cvCloneImage(image);
  78.  
  79.     cvNamedWindow(name, CV_WINDOW_AUTOSIZE);
  80.  
  81.     // set up callback
  82.     cvSetMouseCallback(name, my_mouse_callback, (void *)image);
  83.  
  84.     // main loop
  85.     while (1){
  86.         cvCopy(image, temp, NULL);
  87.         if(drawing_box)
  88.             draw_box(temp, box);
  89.         cvShowImage(name, temp);
  90.  
  91.         if (cvWaitKey(15)==27)
  92.             break;
  93.     }
  94.  
  95.     cvReleaseImage(&image);
  96.     cvReleaseImage(&temp);
  97.     cvDestroyWindow(name);
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement