-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenCVGrab.cpp
More file actions
139 lines (111 loc) · 5.06 KB
/
openCVGrab.cpp
File metadata and controls
139 lines (111 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* openCVGrab: A sample program showing to convert Pylon images to opencv MAT.
Copyright 2017 Matthew Breit <matt.breit@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
THIS SOFTWARE REQUIRES ADDITIONAL SOFTWARE (IE: LIBRARIES) IN ORDER TO COMPILE
INTO BINARY FORM AND TO FUNCTION IN BINARY FORM. ANY SUCH ADDITIONAL SOFTWARE
IS OUTSIDE THE SCOPE OF THIS LICENSE.
*/
// Include files to use OpenCV API.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
// Use sstream to create image names including integer
#include <sstream>
// Namespace for using pylon objects.
using namespace Pylon;
// Namespace for using GenApi objects
using namespace GenApi;
// Namespace for using opencv objects.
using namespace cv;
// Namespace for using cout.
using namespace std;
// Number of images to be grabbed.
static const uint32_t c_countOfImagesToGrab = 1000;
int main(int argc, char* argv[])
{
// The exit code of the sample application.
int exitCode = 0;
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
// is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
try
{
// Create an instant camera object with the camera device found first.
cout << "Creating Camera..." << endl;
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
// or use a device info object to use a specific camera
//CDeviceInfo info;
//info.SetSerialNumber("21694497");
//CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice(info));
cout << "Camera Created." << endl;
// Print the model name of the camera.
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
// The parameter MaxNumBuffer can be used to control the count of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 10;
// create pylon image format converter and pylon image
CImageFormatConverter formatConverter;
formatConverter.OutputPixelFormat= PixelType_BGR8packed;
CPylonImage pylonImage;
// Create an OpenCV image
Mat openCvImage;
// Start the grabbing of c_countOfImagesToGrab images.
// The camera device is parameterized with a default configuration which
// sets up free-running continuous acquisition.
camera.StartGrabbing( c_countOfImagesToGrab);
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while ( camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
// Access the image data.
cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
// Convert the grabbed buffer to pylon imag
formatConverter.Convert(pylonImage, ptrGrabResult);
// Create an OpenCV image out of pylon image
openCvImage= cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());
// Create a display window
namedWindow( "OpenCV Display Window", CV_WINDOW_NORMAL);//AUTOSIZE //FREERATIO
// Display the current image with opencv
imshow( "OpenCV Display Window", openCvImage);
// Define a timeout for customer's input in ms.
// '0' means indefinite, i.e. the next image will be displayed after closing the window
// '1' means live stream
waitKey(1);
}
else
{
cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
}
}
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
exitCode = 1;
}
// Comment the following two lines to disable waiting on exit.
cerr << endl << "Press Enter to exit." << endl;
while( cin.get() != '\n');
return exitCode;
}