taking still image works with specified ROI

This commit is contained in:
Jan Grewe 2024-03-06 09:18:43 +01:00
parent 6a82c8d640
commit adf48f3002
4 changed files with 66 additions and 25 deletions

View File

@ -56,6 +56,7 @@ CameraPreview::CameraPreview(QWidget *parent):cameraname(""), camera(nullptr), Q
takeStill();
}
void CameraPreview::setCamera(QString &device){
qDebug() << "update camera! ";// << device.toStdString();
cameraname = device;

View File

@ -628,13 +628,12 @@ void PylonRecorder::connectCamera() {
msgBox.exec();
} else {
cameraConfiguration();
if (layout.mode == CameraMode::single) {
if (layout.mode == CameraMode::single && layout.devices.size() == 1) {
qDebug() << "single camera mode";
assert(layout.devices.size() == 1);
std::string cname = layout.devices[0];
std::string message;
qDebug() << "connecting to camera " << cname.c_str();
pyloncam = new PylonWrapper(cname);
pyloncam = new PylonWrapper(layout);
bool success = pyloncam->openCamera(message);
if (success) {
cameraConnectedLabel->setText("connected");

View File

@ -1,29 +1,29 @@
#include "pylonwrapper.h"
PylonWrapper::PylonWrapper(const std::string &fullName):
valid(false), fullName(fullName), camera(nullptr) {
valid(false), fullName(fullName), camera(nullptr), withLayout(false) {
qDebug() << "Constructor with name";
Pylon::PylonInitialize();
}
PylonWrapper::PylonWrapper(const CameraLayout &layout): valid(false), withLayout(true), camera(nullptr) {
qDebug() << "Constructor with layout";
this->fullName = layout.devices[0];
this->layout = layout;
Pylon::PylonInitialize();
// camera = new Pylon::CInstantCamera();
// std::cerr << "Wrapper:camera is open" << camera->IsOpen() << std::endl;
// std::cerr << "Wrapper:is valid" << valid << std::endl;
}
PylonWrapper::~PylonWrapper() {
std::cerr << "wrapper destructor" << std::endl;
qDebug() << "wrapper destructor";
if (camera != nullptr){
std::cerr << "check camera!" << std::endl;
if (camera->IsOpen()) {
std::cerr << "close camera!" << std::endl;
qDebug() << "Camera open, closing it!";
camera->Close();
// std::cerr << "terminate pylon" << std::endl;
// terminate();
}
std::cerr << "delete camera!" << std::endl;
delete camera;
camera = nullptr;
}
std::cerr << "wrapper: deleted camera" << std::endl;
qDebug() << "Successfully deleted camera";
}
void PylonWrapper::terminate() {
@ -141,36 +141,71 @@ ImageSettings PylonWrapper::getImageSettings() {
bool PylonWrapper::grabFrame(MyImage &img) {
Pylon::CGrabResultPtr frame;
if (valid) {
camera->StartGrabbing();
camera->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException);
camera->StopGrabbing();
}
qDebug() << "Setting width" << layout.rois[0].width;
Pylon::CIntegerParameter(camera->GetNodeMap(), "Width").SetValue(layout.rois[0].width);
qDebug() << "Setting height" << layout.rois[0].height;
Pylon::CIntegerParameter(camera->GetNodeMap(), "Height").SetValue(layout.rois[0].height);
qDebug() << "Setting xoffset" << layout.rois[0].x;
Pylon::CIntegerParameter(camera->GetNodeMap(), "OffsetX").SetValue(layout.rois[0].x);
qDebug() << "Setting yoffset" << layout.rois[0].y;
Pylon::CIntegerParameter(camera->GetNodeMap(), "OffsetY").SetValue(layout.rois[0].y);
camera->StartGrabbing();
camera->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException);
camera->StopGrabbing();
}
img.setFrame(frame);
return frame.IsValid();
}
bool PylonWrapper::openCamera(std::string &message) {
void PylonWrapper::resetCamera() {
std::string n = std::string(Pylon::CStringParameter(camera->GetNodeMap(), "DeviceUserID").GetValue());
int64_t maxWidth = 2048;
int64_t maxHeight = 1536;
Pylon::CIntegerParameter(camera->GetNodeMap(), "Width").SetValue(maxWidth);
Pylon::CIntegerParameter(camera->GetNodeMap(), "Height").SetValue(maxHeight);
Pylon::CIntegerParameter(camera->GetNodeMap(), "OffsetX").SetValue(0);
Pylon::CIntegerParameter(camera->GetNodeMap(), "OffsetY").SetValue(0);
}
bool PylonWrapper::openCamera(std::string &message) {
qDebug() << "opening camera";
try {
camera = new Pylon::CInstantCamera();
// Pylon::CInstantCamera camera( pTl->CreateDevice( lstDevices[0] );
// Pylon::IPylonDevice *pDevice = Pylon::CTlFactory::GetInstance().CreateFirstDevice();
Pylon::String_t fname(fullName.c_str());
Pylon::IPylonDevice *pDevice = Pylon::CTlFactory::GetInstance().CreateDevice(fname);
camera->Attach(pDevice);
camera->Open();
resetCamera();
valid = camera->IsOpen();
message = "Successfully opened camera!";
} catch (const Pylon::GenericException &e) {
message = e.GetDescription();
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
valid = false;
return valid;
}
if (!withLayout) {
qDebug() << "opening camera without layout, creating a new one";
ImageSettings s = getImageSettings();
layout = CameraLayout();
layout.devices.push_back(fullName);
ROI r = ROI();
r.x = 0;
r.y = 0;
r.width = s.width;
r.height = s.height;
layout.rois.push_back(r);
layout.layout = Layout::horizontal;
layout.mode = CameraMode::single;
}
return valid;
}
void PylonWrapper::closeCamera() {
std::cerr << "camera close !" << std::endl;
qDebug() << "Close camera!";
if (camera->IsOpen()) {
try {
camera->Close();
@ -178,9 +213,10 @@ void PylonWrapper::closeCamera() {
delete camera;
camera = nullptr;
} catch (const Pylon::GenericException &e) {
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
qWarning() << "An exception occurred." << e.GetDescription();
}
}
qDebug() << "Successfully closed camera!";
}
Pylon::CInstantCamera *PylonWrapper::getCamera() {

View File

@ -3,6 +3,8 @@
#include <pylon/PylonIncludes.h>
#include <pylon/BaslerUniversalInstantCamera.h>
#include "mylogger.h"
#include "util.h"
#include "myimage.h"
struct ImageSettings {
@ -15,7 +17,8 @@ struct ImageSettings {
class PylonWrapper
{
public:
PylonWrapper(const std::string &fullName);
PylonWrapper(const std::string &name);
PylonWrapper(const CameraLayout &layout);
~PylonWrapper();
ImageSettings getImageSettings();
@ -32,11 +35,13 @@ public:
double gain();
bool gain(double gain_db);
Pylon::CInstantCamera *getCamera();
void resetCamera();
private:
Pylon::CInstantCamera *camera;
bool valid;
bool valid, withLayout;
std::string fullName;
CameraLayout layout;