Ask Your Question

Revision history [back]

Actually what I would suggest is moving past the old C-style API and use the new C++-style API which uses the Mat element more wisely and doesn't leave you with the tricky things that IplImage pointers generate. It is actually quite simple to crop an image then and only visualizing the necessary part.

#include "stdafx.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main()
{
    //Create window
    namedWindow("Camera_Output", 1);    

    //Capture using camera 1 connected to system
    VideoCapture capture(1);  
    set( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
    set( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );

    //Create loop for live streaming
    while(1){ 
        /* Create image frames from capture */
        Mat framein;
        capture >> framein; 

        /* Set the Region of Interest and apply it directly to create the output image */
        Mat frameout = framein( Rect(0, 0, 320, 240) );

        /* Show image frames on created window */
        imshow("Camera_Output", frameout);   

        /* Capture Keyboard stroke
           ESC key loop will break. */
        int key = cvWaitKey(10);     
        if (key == 27){
            break; 
        }
    }

    // Once using the C++ interface, destroying objects is done automatically by the garbage collector.
    // This is interesting because many people forget to dereference and destroy items.
    return 0;
}

It doesn't only reduce the amount of code and unnecessary calls, it also gives you the possibility to use the region of interest parameter without creating tons of new elements.

click to hide/show revision 2
changed wrong code snippet

Actually what I would suggest is moving past the old C-style API and use the new C++-style API which uses the Mat element more wisely and doesn't leave you with the tricky things that IplImage pointers generate. It is actually quite simple to crop an image then and only visualizing the necessary part.

#include "stdafx.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main()
{
    //Create window
    namedWindow("Camera_Output", 1);    

    //Capture using camera 1 connected to system
    VideoCapture capture(1);  
    set( capture.set( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
    set( capture.set( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );

    //Create loop for live streaming
    while(1){ 
        /* Create image frames from capture */
        Mat framein;
        capture >> framein; 

        /* Set the Region of Interest and apply it directly to create the output image */
        Mat frameout = framein( Rect(0, 0, 320, 240) );

        /* Show image frames on created window */
        imshow("Camera_Output", frameout);   

        /* Capture Keyboard stroke
           ESC key loop will break. */
        int key = cvWaitKey(10);     
        if (key == 27){
            break; 
        }
    }

    // Once using the C++ interface, destroying objects is done automatically by the garbage collector.
    // This is interesting because many people forget to dereference and destroy items.
    return 0;
}

It doesn't only reduce the amount of code and unnecessary calls, it also gives you the possibility to use the region of interest parameter without creating tons of new elements.