简体   繁体   中英

rotate video opencv C++

I have trying to rotate 90 degree the video from camera, I try to use cvTranspose and cvFlip but the results is:

Unhandled exception at 0x752bc41f in CameraStero.exe: Microsoft C++ exception: cv::Exception at memory location 0x0021faf8..

here my code.

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

using namespace cv;



int main(int argc, char** argv)
{
 VideoCapture cap1;

 cap1.open(0);
 cap1.set(CV_CAP_PROP_FPS, 0.55); 
 cap1.set(CV_CAP_PROP_FRAME_WIDTH, 800.0); 
 cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 800.0); 


 Mat img1, img2;

for(;;){


CvCapture* cap;
IplImage* frame;
cap = cvCreateCameraCapture( 0 );
cvGrabFrame(cap);
frame=cvRetrieveFrame(cap);




cvTranspose(frame, frame);
cvFlip(frame, frame, 1);


cvShowImage("Img1", frame);

    if(waitKey(1)=='q')
            break;
  }
 }

any suggestions to correct the code?

You are mixing OpenCV c++ and c interface. Here how it is done in OpenCV/C.

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

int main(int argc, char** argv)
{
    CvCapture* cap = cvCreateCameraCapture( 0 );

    for(;;)
    {
       IplImage* frame=cvQueryFrame(cap);

       cvTranspose(frame, frame);
       cvFlip(frame, frame, 1);

       cvShowImage("Img1", frame);

       if(waitKey(30)=='q')
           break;
   }

   cvReleaseCapture(cap);
 }
  1. You should leave the capture parameters being default as it may cause errors if the camera doesn't support the setting your set. So delete the following lines:

     cap1.set(CV_CAP_PROP_FPS, 0.55); cap1.set(CV_CAP_PROP_FRAME_WIDTH, 800.0); cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 800.0); 

    or if you do need to change width and height, you should make sure the width/height to be set should be smaller than default values. The following code will work too:

     cap1.set(CV_CAP_PROP_FPS, 0.55); cap1.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 240); 
  2. Like @SRF said, you are mixing up OpenCV C/C++ interface, so you either use

     cap = cvCreateCameraCapture( 0 ); 

    or

     cap1.open(0); 

    , but not the both.

  3. When you want to show the image your captured in one window, you should create the window first by:

     cv::namedWindow("Img1", 1); 
  4. Don't forget to return 0; in int main() . Although some compiler don't restrict this, you should do this as always.


After fixing all these, the code should look like this (works fine on my PC):

int main()
{
    VideoCapture cap1;

    cap1.open(0);
    cap1.set(CV_CAP_PROP_FPS, 0.55); 
    cap1.set(CV_CAP_PROP_FRAME_WIDTH, 320); 
    cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 240); 
    cv::namedWindow("Img1", 1);

    Mat frame;
    for(;;)
    {
        Mat frame;
        cap1 >> frame;

        transpose(frame, frame);
        flip(frame, frame, 1);

        imshow("Img1", frame);

        if(waitKey(1)=='q')
            break;
    }

    cap1.release();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM