从长方形的视频设置鼠标的投资回报率 [英] Setting ROI with mouse from a rectangle on a video

查看:100
本文介绍了从长方形的视频设置鼠标的投资回报率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有视频,运行视频的第一帧被取为图像和允许用户在图像上绘制一个矩形,矩形绘制后的程序时,用户必须右键点击图片进行确认矩形。当鼠标右键单击图像消失,视频开始与它绘制的矩形玩。

我能够完美地绘制矩形,但我不能设置矩形的投资回报率。

我想要做的是设置矩形的利息率(ROI)的区域做对即ROI一些图像处理。我无法设置我画的ROI的矩形。

我使用的OpenCV与Visual Studio 2010。稍后我会尽量在Qt Creator的这个方案集成。

任何帮助将是AP preciated。

先谢谢了。

我的全code是如下:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; opencv2 \\ opencv.hpp>
#包括LT&; opencv2 \\ highgui \\ highgui.hpp>
#包括LT&; OpenCV的/ highgui.h>
#包括LT&; OpenCV的/ cxcore.h>
#包括LT&; OpenCV的\\ cvaux.h>使用命名空间的简历;
使用命名空间std;
无效my_mouse_callback(INT事件,诠释的x,INT Y,INT标志,无效*参数);
布尔摧毁=假;
CvRect箱;
* IplImage的形象;
* IplImage结构式2;
布尔drawing_box = FALSE;无效draw_box(*的IplImage IMG,CvRect RECT)
{
cvRectangle(IMG,cvPoint(box.x,box.y),cvPoint(box.x + box.width,box.y + box.height)
            cvScalar(0,0,255),2);CvRect RECT2 = cvRect(box.x,box.y,box.width,box.height);
// cvSetImageROI(图像,RECT2); //这里我想设置绘制矩形的投资回报率
}//实现鼠标回调
无效my_mouse_callback(INT事件,诠释的x,INT Y,INT标志,无效*参数){
* IplImage的图像=(*的IplImage)参数;开关(事件){
    案例CV_EVENT_MOUSEMOVE:
        如果(drawing_box)
        {
            box.width = X-box.x;
            box.height = Y-box.y;
        }
        打破;    案例CV_EVENT_LBUTTONDOWN:
        drawing_box = TRUE;
        盒= cvRect(X,Y​​,0,0);
        打破;    案例CV_EVENT_LBUTTONUP:
        drawing_box = FALSE;
        如果(box.width℃,)
        {
            box.x + = box.width;
            box.width * = -1;
        }
        如果(box.height℃,)
        {
            box.y + = box.height;
            box.height * = -1;
        }
        draw_box(图像,盒);
        打破;
    案例CV_EVENT_RBUTTONUP:
        摧毁=真;
   }
}诠释的main()
{
    为const char * name =框示例
    cvNamedWindow(名);   箱= cvRect(0,0,1,1);   CvCapture *捕获= cvCreateFileCapture(C:\\\\ video.mp4);
   图像= cvQueryFrame(捕捉);  * IplImage的TEMP = cvCloneImage(图片);
 //设置回调
  cvSetMouseCallback(姓名,my_mouse_callback,(无效*)图像);
// *的IplImage IMG2 = cvCreateImage(cvGetSize(TEMP),TEMP-GT&;深度,TEMP-GT&; N沟道);// cvNot(温度,温度);
   / *复制子图像* /
   // cvCopy(温度,温度,NULL);  //主回路
   而(1)
{
    如果(破坏){cvDestroyWindow(名);打破;}
    cvCopyImage(图像,温度);
    如果(drawing_box)
        draw_box(温度,盒);
    cvMoveWindow(姓名,200,100);
    cvShowImage(姓名,温度);    如果(cvWaitKey(15)== 27)
        打破;
 } // cvReleaseImage(安培;图片);
  cvReleaseImage(安培,温度);
 cvDestroyWindow(名); cvNamedWindow(例2,CV_WINDOW_AUTOSIZE);
  cvMoveWindow(例2,150,150);
  而(1)
    {
式2 = cvQueryFrame(捕捉);
draw_box(帧2,盒);
    如果中断(式2!);
        cvShowImage(例2式2);
        焦C = cvWaitKey(33);
    如果(C == 27)破;
   }
cvReleaseCapture(安培;捕捉);
cvDestroyWindow(例2);
   返回0;
}


解决方案

您是几乎没有。有一个问题,但:情况下CV_EVENT_RBUTTONUP 需要,我也想补充破解默认情况。

以下code设置ROI,就可以执行简单的灰度处理,然后复制处理的ROI回原始图像

有关测试,我改变了你的code用我的相机,而不是加载文件的。

输出

code

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&string.h中GT;#包括LT&;&cv.h GT;
#包括LT&;&highgui.h GT;使用命名空间的简历;
使用命名空间std;无效my_mouse_callback(INT事件,诠释的x,INT Y,INT标志,无效*参数);布尔摧毁=假;
CvRect箱;
布尔drawing_box = FALSE;无效draw_box(*的IplImage IMG,CvRect RECT)
{
  cvRectangle(IMG,cvPoint(box.x,box.y),cvPoint(box.x + box.width,box.y + box.height)
              cvScalar(0,0,255),2);  CvRect RECT2 = cvRect(box.x,box.y,box.width,box.height);
  // cvSetImageROI(图像,RECT2); //这里我想设置绘制矩形的投资回报率
}//实现鼠标回调
无效my_mouse_callback(INT事件,诠释的x,INT Y,INT标志,无效*参数)
{
  * IplImage结构框架=(*的IplImage)参数;  开关(事件)
  {
      案例CV_EVENT_MOUSEMOVE:
      {
          如果(drawing_box)
          {
              box.width = X-box.x;
              box.height = Y-box.y;
          }
      }
      打破;      案例CV_EVENT_LBUTTONDOWN:
      {
          drawing_box = TRUE;
          盒= cvRect(X,Y​​,0,0);
      }
      打破;      案例CV_EVENT_LBUTTONUP:
      {
          drawing_box = FALSE;
          如果(box.width℃,)
          {
              box.x + = box.width;
              box.width * = -1;
          }          如果(box.height℃,)
          {
              box.y + = box.height;
              box.height * = -1;
          }          draw_box(架,盒);
      }
      打破;      案例CV_EVENT_RBUTTONUP:
      {
          摧毁=真;
      }
      打破;      默认:
      打破;
   }
}诠释的main()
{
  为const char * name =框示例
  cvNamedWindow(名);
  箱= cvRect(0,0,1,1);  CvCapture *捕获= cvCaptureFromCAM(0);
  如果(!捕获)
  {
    的printf(!失败cvCaptureFromCAM \\ n);
    返回1;
  }  * IplImage的图像= cvQueryFrame(捕捉);
  如果(!图片)
  {
    的printf(!!!失败cvQueryFrame#1 \\ n);
    返回2;
  }  * IplImage的TEMP = cvCloneImage(图片);  //设置回调
  cvSetMouseCallback(姓名,my_mouse_callback,(无效*)图像);  //主回路
  而(1)
  {
    如果(破坏)
    {
      cvDestroyWindow(名);打破;
    }
    cvCopyImage(图像,温度);    如果(drawing_box)
        draw_box(温度,盒);    cvMoveWindow(姓名,200,100);
    cvShowImage(姓名,温度);    如果(cvWaitKey(15)== 27)
        打破;
  }  cvReleaseImage(安培,温度);
  cvDestroyWindow(名);  cvNamedWindow(例2,CV_WINDOW_AUTOSIZE);
  cvMoveWindow(例2,150,150);  //检索从设备的单个帧,并设置在ROI
  *的IplImage = vid_frame cvQueryFrame(捕捉);
  如果(!vid_frame)
  {
    的printf(!!!失败cvQueryFrame#2 \\ n);
    返回2;
  }  cvSetImageROI(vid_frame,盒);  //分配空间用于单信道的ROI(存储灰度帧)
  的IplImage * gray_roi = cvCreateImage(cvSize(box.width,box.height),IPL_DEPTH_8U,1);
  的IplImage * rgb_roi = cvCreateImage(cvSize(box.width,box.height),IPL_DEPTH_8U,3);  而(1)
  {
    如果(!vid_frame)
    {
        vid_frame = cvQueryFrame(捕捉);
        如果(!vid_frame)
        {
            的printf(!!!失败cvQueryFrame#3 \\ n);
            打破;
        }
    }    draw_box(vid_frame,盒);    //设置投资回报率,并执行一些处理(在这种情况下,转换ROI为灰度)
    cvSetImageROI(vid_frame,盒);
    cvCvtColor(vid_frame,gray_roi,CV_BGR2GRAY);
        // cvShowImage(例2,gray_roi);    / *此时gray_roi具有兵卫ROI的大小和包含经处理的图像。
     *为了好玩,我们复制处理后的图像回到原始图像和显示在屏幕上!
     * /
    cvCvtColor(gray_roi,rgb_roi,CV_GRAY2BGR);    //由于投资回报率仍然设置,cvCopy是受它的影响
    cvCopy(rgb_roi,vid_frame,NULL);    //现在重置投资回报率如此cvShowImage显示完整的图像
    cvResetImageROI(vid_frame);
    cvShowImage(例2,vid_frame);    焦C = cvWaitKey(33);
    如果(C == 27)破;    vid_frame = NULL;
  }
  cvSaveImage(processed.jpg,vid_frame);  cvReleaseImage(安培; gray_roi);
  cvReleaseImage(安培; rgb_roi);
  cvReleaseCapture(安培;捕捉);
  cvDestroyWindow(例2);  返回0;
}

I have video, when the program is run the video's first frame is taken as an image and the user is allowed to draw a rectangle on the image, after the rectangle is drawn, the user must right click on the image to confirm the rectangle. When the mouse the right-clicked the image disappears and the video starts to play with the drawn rectangle on it.

I am able to draw the rectangle perfectly but I can't set that rectangle as ROI.

What I want to do is to set that rectangle as Region of Interest (ROI) to do some image processing on that ROI. I am unable to set the rectangle which I draw as ROI.

I am using OpenCV with Visual Studio 2010. Later on I will try to integrate this program in QT creator.

Any help would be appreciated.

Thanks in advance.

My full code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include<opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <opencv\cvaux.h>

using namespace cv;
using namespace std;
void my_mouse_callback( int event, int x, int y, int flags, void* param );
bool destroy=false;
CvRect box;
IplImage* image;
IplImage* frame2;
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),
            cvScalar(0,0,255) ,2);

CvRect rect2=cvRect(box.x,box.y,box.width,box.height);
//cvSetImageROI(image, rect2);   //here I wanted to set the drawn rect as ROI
}

// Implement 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;
    case CV_EVENT_RBUTTONUP:
        destroy=true;
   }
}

int main()
{
    const char* name = "Box Example";
    cvNamedWindow( name );

   box = cvRect(0,0,1,1);

   CvCapture* capture = cvCreateFileCapture( "C:\\video.mp4" );
   image = cvQueryFrame( capture );

  IplImage* temp = cvCloneImage( image );
 // Set up the callback
  cvSetMouseCallback( name, my_mouse_callback, (void*) image);


//IplImage *img2 = cvCreateImage(cvGetSize(temp),temp->depth,temp->nChannels);

//cvNot(temp,temp);
   /* copy subimage */
   //cvCopy(temp, temp, NULL);

  // Main loop
   while( 1 )
{
    if(destroy) {cvDestroyWindow(name); break;}
    cvCopyImage( image, temp );
    if( drawing_box ) 
        draw_box( temp, box );
    cvMoveWindow(name, 200, 100);
    cvShowImage( name, temp );

    if( cvWaitKey( 15 )==27 ) 
        break;
 }

 //cvReleaseImage( &image );
  cvReleaseImage( &temp );
 cvDestroyWindow( name );

 cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
  cvMoveWindow("Example2", 150, 150);


  while(1) 
    {
frame2 = cvQueryFrame( capture );
draw_box(frame2,box);
    if( !frame2 ) break;
        cvShowImage( "Example2", frame2 );
        char c = cvWaitKey(33);
    if( c == 27 ) break;
   }
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
   return 0;
}

解决方案

You were almost there. One problem though: case CV_EVENT_RBUTTONUP needs to break, and I would also add a break on default case.

The following code sets the ROI, performs a simple grayscale processing on it and then copies the processed ROI back to the original image.

For testing purposes I changed your code to use my camera instead of loading a file.

Output:

Code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <cv.h>
#include <highgui.h>

using namespace cv;
using namespace std;

void my_mouse_callback( int event, int x, int y, int flags, void* param );

bool destroy=false;
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),
              cvScalar(0,0,255) ,2);

  CvRect rect2=cvRect(box.x,box.y,box.width,box.height);
  //cvSetImageROI(image, rect2);   //here I wanted to set the drawn rect as ROI
}

// Implement mouse callback
void my_mouse_callback( int event, int x, int y, int flags, void* param )
{
  IplImage* frame = (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(frame, box);
      }
      break;

      case CV_EVENT_RBUTTONUP:
      {
          destroy=true;
      }
      break;

      default:
      break;
   }
}

int main()
{
  const char* name = "Box Example";
  cvNamedWindow( name );
  box = cvRect(0,0,1,1);

  CvCapture* capture = cvCaptureFromCAM(0); 
  if (!capture)
  {
    printf("!!! Failed cvCaptureFromCAM\n");
    return 1;
  }

  IplImage* image = cvQueryFrame(capture);
  if (!image)
  {
    printf("!!! Failed cvQueryFrame #1\n");
    return 2;
  }

  IplImage* temp = cvCloneImage(image);

  // Set up the callback
  cvSetMouseCallback(name, my_mouse_callback, (void*) image);

  // Main loop
  while( 1 )
  {
    if (destroy) 
    {
      cvDestroyWindow(name); break;
    }
    cvCopyImage(image, temp);

    if (drawing_box) 
        draw_box(temp, box);

    cvMoveWindow(name, 200, 100);
    cvShowImage(name, temp);

    if (cvWaitKey(15) == 27) 
        break;
  }

  cvReleaseImage(&temp);
  cvDestroyWindow(name);

  cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
  cvMoveWindow("Example2", 150, 150);

  // Retrieve a single frame from the device  and set the ROI
  IplImage* vid_frame = cvQueryFrame(capture);
  if (!vid_frame)
  {
    printf("!!! Failed cvQueryFrame #2\n");
    return 2;
  }

  cvSetImageROI(vid_frame, box);

  // Allocate space for a single-channel ROI (to store grayscale frames)
  IplImage* gray_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 1);
  IplImage* rgb_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 3);

  while(1) 
  {
    if (!vid_frame)
    {
        vid_frame = cvQueryFrame(capture);
        if (!vid_frame)
        {
            printf("!!! Failed cvQueryFrame #3\n");
            break;
        }
    }

    draw_box(vid_frame,  box);

    // Set ROI and perform some processing (in this case, converting the ROI to grayscale)
    cvSetImageROI(vid_frame, box);
    cvCvtColor(vid_frame, gray_roi, CV_BGR2GRAY);
        //cvShowImage("Example2", gray_roi);

    /* At this point gray_roi has the size of thei ROI and contains the processed image.
     * For fun, we copy the processed image back to the original image and display it on the screen!
     */
    cvCvtColor(gray_roi, rgb_roi, CV_GRAY2BGR);

    // As the ROI is still set, cvCopy is affected by it
    cvCopy(rgb_roi, vid_frame, NULL);

    // Now reset the ROI so cvShowImage displays the full image
    cvResetImageROI(vid_frame);
    cvShowImage("Example2", vid_frame);

    char c = cvWaitKey(33);
    if( c == 27 ) break;

    vid_frame = NULL;
  }
  cvSaveImage("processed.jpg", vid_frame);

  cvReleaseImage(&gray_roi);
  cvReleaseImage(&rgb_roi);
  cvReleaseCapture( &capture );
  cvDestroyWindow( "Example2" );

  return 0;
}

这篇关于从长方形的视频设置鼠标的投资回报率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆