C++ 使用鼠标从视频上的矩形设置ROI

C++ 使用鼠标从视频上的矩形设置ROI,c++,c,image-processing,opencv,computer-vision,C++,C,Image Processing,Opencv,Computer Vision,我有视频,当程序运行时,视频的第一帧被作为图像,用户可以在图像上绘制一个矩形,绘制矩形后,用户必须右键单击图像以确认该矩形。当鼠标右键单击图像时,图像消失,视频开始播放其上绘制的矩形 我能够完美地绘制矩形,但我不能将该矩形设置为ROI 我想做的是将该矩形设置为感兴趣区域(ROI),以便对该ROI进行一些图像处理。我无法将绘制的矩形设置为ROI 我正在Visual Studio 2010中使用OpenCV。稍后,我将尝试在QT creator中集成此程序 任何帮助都将不胜感激 提前谢谢 我的全部代

我有视频,当程序运行时,视频的第一帧被作为图像,用户可以在图像上绘制一个矩形,绘制矩形后,用户必须右键单击图像以确认该矩形。当鼠标右键单击图像时,图像消失,视频开始播放其上绘制的矩形

我能够完美地绘制矩形,但我不能将该矩形设置为ROI

我想做的是将该矩形设置为感兴趣区域(ROI),以便对该ROI进行一些图像处理。我无法将绘制的矩形设置为ROI

我正在Visual Studio 2010中使用OpenCV。稍后,我将尝试在QT creator中集成此程序

任何帮助都将不胜感激

提前谢谢

我的全部代码如下:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
void my_mouse_回调(int事件、int x、int y、int标志、void*param);
bool destroy=false;
CvRect盒;
IplImage*图像;
IplImage*框架2;
布尔图_框=假;
无效绘图框(IplImage*img,CvRect)
{
cv矩形(img,cvPoint(box.x,box.y),cvPoint(box.x+box.width,box.y+box.height),
cv标量(0,0255),2;
CvRect rect2=CvRect(box.x,box.y,box.width,box.height);
//cvSetImageROI(image,rect2);//这里我想将绘制的rect设置为ROI
}
//实现鼠标回调
void my_mouse_回调(int事件、int x、int y、int标志、void*param){
IplImage*图像=(IplImage*)参数;
开关(事件){
案例CV\u事件\u鼠标移动:
如果(图纸框)
{
box.width=x-box.x;
box.height=y-box.y;
}
打破
案例CV\u事件\u LBUTTONDOWN:
绘图框=真;
box=cvRect(x,y,0,0);
打破
案例CV事件按钮:
绘图框=假;
如果(方框宽度<0)
{
box.x+=box.width;
方框宽度*=-1;
}
如果(框高度<0)
{
box.y+=box.height;
箱高*=-1;
}
绘制框(图像、框);
打破
案例CV_事件_RBUTTONUP:
毁灭=真实;
}
}
int main()
{
const char*name=“框示例”;
cvNamedWindow(姓名);
box=cvRect(0,0,1,1);
CvCapture*capture=cvCreateFileCapture(“C:\\video.mp4”);
图像=cvQueryFrame(捕获);
IplImage*temp=cvCloneImage(图像);
//设置回调
cvSetMouseCallback(名称,my_mouse_回调,(void*)图像);
//IplImage*img2=cvCreateImage(cvGetSize(temp),temp->depth,temp->nChannels);
//cvNot(温度,温度);
/*复制子映像*/
//cvCopy(临时、临时、空);
//主回路
而(1)
{
如果(销毁){cvDestroyWindow(名称);break;}
cvCopyImage(图像、温度);
如果(图纸框)
牵引箱(温度、箱);
cvMoveWindow(名称200100);
cvShowImage(名称、温度);
如果(cvWaitKey(15)==27)
打破
}
//cvReleaseImage(&image);
cvReleaseImage(&temp);
窗口(名称);
cvNamedWindow(“示例2”,CV\u窗口\u自动调整大小);
cvMoveWindow(“示例2”,150,150);
而(1)
{
frame2=cvQueryFrame(捕获);
画框(框2,框);
如果(!frame2)中断;
cvShowImage(“示例2”,框架2);
char c=cvWaitKey(33);
如果(c==27)断开;
}
cvReleaseCapture(&capture);
CVD窗口(“示例2”);
返回0;
}

你就快到了。不过有一个问题:
case CV\u EVENT\RBUTTONUP
需要
中断
,我还要在
默认
案例中添加一个
中断

下面的代码设置ROI,对其执行简单的灰度处理,然后将处理后的ROI复制回原始图像

出于测试目的,我更改了您的代码以使用我的相机,而不是加载文件

输出

#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;
}

代码

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
void my_mouse_回调(int事件、int x、int y、int标志、void*param);
bool destroy=false;
CvRect盒;
布尔图_框=假;
无效绘图框(IplImage*img,CvRect)
{
cv矩形(img,cvPoint(box.x,box.y),cvPoint(box.x+box.width,box.y+box.height),
cv标量(0,0255),2;
CvRect rect2=CvRect(box.x,box.y,box.width,box.height);
//cvSetImageROI(image,rect2);//这里我想将绘制的rect设置为ROI
}
//实现鼠标回调
void my_mouse_回调(int事件、int x、int y、int标志、void*param)
{
IplImage*帧=(IplImage*)参数;
开关(事件)
{
案例CV\u事件\u鼠标移动:
{
如果(图纸框)
{
box.width=x-box.x;
box.height=y-box.y;
}
}
打破
案例CV\u事件\u LBUTTONDOWN:
{
绘图框=真;
box=cvRect(x,y,0,0);
}
打破
案例CV事件按钮:
{
绘图框=假;
如果(方框宽度<0)
{
box.x+=box.width;
方框宽度*=-1;
}
如果(框高度<0)
{
box.y+=box.height;
箱高*=-1;
}
画框(框、盒);
}
打破
案例CV_事件_RBUTTONUP:
{
毁灭=真实;
}
打破
违约:
打破
}
}
int main()
{
const char*name=“框示例”;
cvNamedWindow(姓名);
box=cvRect(0,0,1,1);
CvCapture*capture=cvCaptureFromCAM(0);
如果(!捕获)
{
printf(!!!cvCaptureFromCAM失败\n”);
返回1;
}
IplImage*image=cvQueryFrame(捕获);
如果(!图像)
{
printf(!!!失败的cvQueryFrame#1\n);
返回2;
}
IplImage*temp=cvCloneImage(图像);
//设置回调
cvSetMouseCallback(名称,my_mouse_回调,(void*)图像);
//M