Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************************
- * Copyright (C) 2008 Metron Hong Kong Limited. All Rights Reserved.
- *
- * Reproduction or disclosure of this file or its contents is granted without
- * prior written consent of Metron Hong Kong Limited.
- ***
- * Code name: CaptureSaveMono.cpp
- * Written by: Antonio Yu, Chief Consultant
- * Date: 1 May, 2008
- * Version: 1.0
- *
- * Adapted by Stijn De Beugher on 2/04/2012
- * Tested in combination with a AVT MANTA 201 MonoChrome camera
- ***
- * Adapted by Steven Puttemans on 03/05/2013
- * Changed compatibility to windows environments
- **********************************************************************************/
- #include <PvApi.h>
- #include <cxcore.h>
- #include <cv.h>
- #include <highgui.h>
- #include <string.h>
- #include <iostream>
- #include <time.h>
- // Added this library in order to be windows compatible
- #include <io.h>
- using namespace std;
- // camera's data type definition
- typedef struct
- {
- unsigned long UID;
- tPvHandle Handle;
- tPvFrame Frame;
- } tCamera;
- #define CH_MONO 1 // Single channel for mono images
- int main(int argc, char* argv[])
- {
- string input = "";
- string output_folder = "";
- int maxframecounter = 100;
- string extension = ".pgm";
- tCamera myCamera;
- tPvCameraInfo cameraInfo;
- unsigned long frameSize;
- tPvErr Errcode;
- cout << "Frame capturing configured, starting the actual capture." << endl;
- // Initialize the API
- if(!PvInitialize())
- {
- // Wait for the response from a camera after the initialization of the driver
- ////////////////////////////////////////////////////////////
- clock_t startT, endT;
- startT = clock();
- float start = float(startT/CLOCKS_PER_SEC);
- while(PvCameraCount() == 0)
- {
- endT = clock();
- float end = float(endT/CLOCKS_PER_SEC);
- if(end>(start+5))
- {
- break;
- }
- }
- /////////////////////////////////////////////////////////////
- unsigned long numCams = PvCameraList(&cameraInfo, 1, NULL);
- cout << "numcams = \t" << numCams << endl;
- if (numCams == 1)
- {
- // Get the camera ID
- myCamera.UID = cameraInfo.UniqueId;
- cout << "Camera ID = \t " << myCamera.UID << endl;
- // Open the camera
- if(!PvCameraOpen(myCamera.UID, ePvAccessMaster, &(myCamera.Handle)))
- {
- // Get the image size of every capture
- Errcode = PvAttrUint32Get(myCamera.Handle, "TotalBytesPerFrame", &frameSize);
- if (Errcode != ePvErrSuccess)
- throw Errcode;
- cout << "Frame size = \t" << frameSize << endl;
- // Allocate a buffer to store the image
- memset(&myCamera.Frame, 0, sizeof(tPvFrame));
- myCamera.Frame.ImageBufferSize = frameSize;
- myCamera.Frame.ImageBuffer = new char[frameSize];
- // Get the width & height of the image
- tPvUint32 width, height;
- PvAttrUint32Get(myCamera.Handle, "Width", &width);
- PvAttrUint32Get(myCamera.Handle, "Height", &height);
- cout << "Frame width = \t" << width << endl;
- cout << "Frame height = \t" << height << endl;
- // Start the camera
- PvCaptureStart(myCamera.Handle);
- // Set the camera to capture continuously
- Errcode = PvAttrEnumSet(myCamera.Handle, "AcquisitionMode", "Continuous");
- if (Errcode != ePvErrSuccess)
- throw Errcode;
- cout <<"start the acquisition" << endl;
- Errcode = PvCommandRun(myCamera.Handle, "AcquisitionStart");
- if (Errcode != ePvErrSuccess)
- throw Errcode;
- Errcode = PvAttrEnumSet(myCamera.Handle, "FrameStartTriggerMode", "Freerun");
- if (Errcode != ePvErrSuccess)
- throw Errcode;
- int framecounter = 0;
- for(framecounter =0;framecounter<maxframecounter;framecounter++)
- {
- cout << "\r"<< framecounter << flush;
- if(!PvCaptureQueueFrame(myCamera.Handle, &(myCamera.Frame), NULL))
- {
- while(PvCaptureWaitForFrameDone(myCamera.Handle, &(myCamera.Frame), 100) == ePvErrTimeout)
- {
- }
- /////////////////////////////////////////
- // Here comes the OpenCV part //
- // Trying to convert to C++ interface //
- /////////////////////////////////////////
- // Create an image header (mono image)
- IplImage *image = cvCreateImageHeader(cvSize((int)width, (int)height), IPL_DEPTH_8U, CH_MONO);
- // Set the width for each image
- image->widthStep = (int)width;
- image->imageData = (char *)myCamera.Frame.ImageBuffer; // Points the image to the buffer of the camera
- // Create a unique blueprint for the name!
- time_t t = time(0); // get time now
- struct tm * now = localtime( & t );
- stringstream date;
- date << (now->tm_year + 1900) << (now->tm_mon + 1) << now->tm_mday << now->tm_hour << now->tm_min << now->tm_sec;
- stringstream filename;
- filename << output_folder << "frame_" << date.str() << "_" << framecounter << extension;
- // Show the image in a window
- cvShowImage("View window", image); cvWaitKey(10);
- // Save the image & release the image
- cvSaveImage(filename.str().c_str(), image);
- cvReleaseImageHeader(&image);
- }
- }
- // Stop the acquisition & free the camera
- Errcode = PvCommandRun(myCamera.Handle, "AcquisitionStop");
- if (Errcode != ePvErrSuccess)
- throw Errcode;
- PvCaptureEnd(myCamera.Handle);
- PvCameraClose(myCamera.Handle);
- cout << endl << "finished" << endl;
- }
- else
- cout << "open camera error" << endl;
- }
- else
- cout << "camera not found" << endl;
- }
- else
- cout << "failed to initialise the API" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement