Friday, January 28, 2011

opcv code 1

copy an image
#include <cv.h>
#include <stdafx.h>
#include <highgui.h>
#include <cxcore.h>
using namespace cv;
int main( int argc, char** argv ) {
    IplImage* img = cvLoadImage( "C:\\ENEL\\OpenCV2.1\\samples\\c\\airplane.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1", img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
}
-------------------------------------------------------------------------------
displaying a video
int main( int argc, char** argv ) {
    cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
    CvCapture* capture = cvCreateFileCapture( "C:\\ENEL\\OpenCV2.1\\samples\\c\\tree.avi" );
    IplImage* frame;
    while(1) {
        frame = cvQueryFrame( capture );
        if( !frame ) break;
        cvShowImage( "Example2", frame );
        char c = cvWaitKey(33);    //control displaying speed
        if( c == 27 ) break;
    }
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Example2" );
}
-------------------------------------------------------------------------------
add a slider bar
int        g_slider_position = 0;
CvCapture* g_capture         = NULL;
void onTrackbarSlide(int pos) {
    cvSetCaptureProperty(
        g_capture,
        CV_CAP_PROP_POS_FRAMES,
        pos
    );
}
int main( int argc, char** argv ) {
    cvNamedWindow( "Example3", CV_WINDOW_AUTOSIZE );
    g_capture = cvCreateFileCapture( "C:\\ENEL\\OpenCV2.1\\samples\\c\\tree.avi" );
    int frames = (int) cvGetCaptureProperty(
        g_capture,
        CV_CAP_PROP_FRAME_COUNT
    );
    if( frames!= 0 ) {
      cvCreateTrackbar(
          "Position",
          "Example3",
          &g_slider_position,
          frames,
          onTrackbarSlide
      );
    }

    IplImage* frame;
    while(1) {
        frame = cvQueryFrame( g_capture );
        if( !frame ) break;
        cvShowImage( "Example3", frame );
        char c = cvWaitKey(1000);
        if( c == 27 ) break;
    }
    cvReleaseCapture( &g_capture );
    cvDestroyWindow( "Example3" );
    return(0);
}
-------------------------------------------------------------------------
Using ImageROI to increment all of the pixels in a region

int main(int argc, char** argv)
{
    IplImage* src;
    if( (src=cvLoadImage("C:\\ENEL\\OpenCV2.1\\samples\\c\\airplane.jpg",1)) != 0 )
    {
        int x = 10;
        int y = 10;
        int width = 100;
        int height = 100;
        int add = 500;
        cvSetImageROI(src, cvRect(x,y,width,height));
        cvAddS(src, cvScalar(add),src);
        cvResetImageROI(src);
        cvNamedWindow( "Roi_Add", 1 );
        cvShowImage( "Roi_Add", src );
        cvWaitKey();
    }
    return 0;
}
--------------------------------------------------------------
webcam capture
int main()
{
 CvCapture* capture;
    capture = cvCreateCameraCapture(0);

 while(1)
 {
     IplImage* img = cvQueryFrame(capture);
     cvShowImage( "autosize image", img );
  cvShowImage( "scalable image", img );
  if(cvWaitKey(10) == 27)
   break;
 }
}
----------------------------------------------------------------------------

No comments:

Post a Comment