Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <highgui.h>
- #include <cv.h>
- #include <cxcore.h>
- #include <stdio.h>
- [CUT]
- int mouseRect();
- int main(){
- printf("Menu - Select Apps \n \n");
- [CUT]
- printf("8 - Mouse Rect");
- printf("\n Your Choose: ");
- char c = getchar();
- switch (c) {
- [CUT]
- case '8':
- printf("\n Mouse Rect");
- mouseRect();
- break;
- default:
- break;
- }
- return 0;
- }
- void my_mouse_callback(int event, int x, int y, int flags, void* param);
- CvRect box;
- bool drawing_box = false;
- void draw_box (IplImage *img, CvRect rect) {
- cvRectangle(img, cvPoint(box.x, box.y), cvPoint(box.x+box.width, box.y+box.height), CV_RGB(0, 0, 255), 1, 8, 0);
- }
- // implementazione mouse callback
- void my_mouse_callback(int event, int x, int y, int flags, void *param) {
- IplImage *image = (IplImage *)param;
- switch (event) {
- case CV_EVENT_MOUSEMOVE:
- if (drawing_box) {
- box.width = x - box.x;
- box.height = y - box.y;
- }
- break;
- case CV_EVENT_LBUTTONDOWN:
- drawing_box = true;
- box = cvRect(x, y, 0, 0);
- break;
- case CV_EVENT_LBUTTONUP:
- drawing_box = false;
- if(box.width < 0) {
- box.x += box.width;
- box.width *= -1;
- }
- if(box.height < 0) {
- box.y += box.height;
- box.height *= -1;
- }
- draw_box(image, box);
- break;
- }
- }
- int mouseRect(int argc, char** argv[]) {
- const char *name = "Mouse Box";
- box = cvRect(-1, -1, 0, 0);
- IplImage *image = cvLoadImage("/home/maurizio/openCV/aur.jpg", CV_LOAD_IMAGE_COLOR);
- cvZero(image); // azzera img
- IplImage *temp = cvCloneImage(image);
- cvNamedWindow(name, CV_WINDOW_AUTOSIZE);
- // set up callback
- cvSetMouseCallback(name, my_mouse_callback, (void *)image);
- // main loop
- while (1){
- cvCopy(image, temp, NULL);
- if(drawing_box)
- draw_box(temp, box);
- cvShowImage(name, temp);
- if (cvWaitKey(15)==27)
- break;
- }
- cvReleaseImage(&image);
- cvReleaseImage(&temp);
- cvDestroyWindow(name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement