I have a thing with 2 Basler usb-cameras and 2 lights. For power saving and educating myself I want lights to be ON only when the certain camera is running.
This programs opens 2 camera view windows on Windows. Cameras are toggling, only the other camera updates at a time but both windows keeps on. The not updating window is showing an old image, until it is updated too.
I have a form with 2 buttons: "Cameras on" and "Cameras off". It calls camera function and sends serial command to electronics.
This is very simplified, please ask if you want more code and I will provide it immediately:
myform.h:
<pre> private: System::Void button17_Click(System::Object^ sender, System::EventArgs^ e) {
stopCycle = 0;
stop = 0;
while (stopCycle == 0) {
String^ BackLightsOn = "a";
this->serialPort1->WriteLine(BackLightsOn);
back_camera();
String^ LightsOff = "b";
this->serialPort1->WriteLine(LightsOff);
Sleep(50);
String^ FrontLightsOn = "c";
this->serialPort1->WriteLine(FrontLightsOn);
front_camera();
String^ LightsOff = "b";
this->serialPort1->WriteLine(LightsOff);
Sleep(50);
}
}
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
stop = 1;
stopCycle = 1;
}
test.cpp:
<pre>int back_camera() {
if (stop == 0) {
Pylon::PylonAutoInitTerm autoInitTerm;
CDeviceInfo info;
String_t serialNumber = "344322557";
info.SetSerialNumber(serialNumber);
CImageFormatConverter formatConverter;
CPylonImage pylonImage;
CGrabResultPtr ptrGrabResult;
CBaslerUsbInstantCamera* pylon_camera;
cv::Mat frame, playingFrame, cannyFrame, contoursFrame;
cv::RNG rng(12345);
pylon_camera = new CBaslerUsbInstantCamera(CTlFactory::GetInstance().CreateFirstDevice(info));
pylon_camera->MaxNumBuffer = 1;
formatConverter.OutputPixelFormat = PixelType_BGR8packed;
pylon_camera->StartGrabbing();
pylon_camera->AcquisitionFrameRate = 60.0;
int i = 0;
while (i < 2) {
i++;
if (pylon_camera->IsGrabbing())
{
pylon_camera->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded() && pylon_camera->IsGrabbing())
{
const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
formatConverter.Convert(pylonImage, ptrGrabResult);
frame = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)pylonImage.GetBuffer());
cv::Size sz = frame.size();
int scale_percent = 20;
int imageWidth = sz.width;
int imageHeight = sz.height;
int down_width = imageWidth * scale_percent / 100;
int down_height = imageHeight * scale_percent / 100;
Mat resized_down;
resize(frame, resized_down, Size(down_width, down_height), INTER_LINEAR);
Mat flipped;
Mat flipped2;
flip(resized_down, flipped, 0);
flip(flipped, flipped2, 1);
cv::imshow("Back camera", flipped2);
cv::moveWindow("Back camera", 1140, 20);
int c = cv::waitKey(1);
if ((char)c == 'c') {
cv::destroyAllWindows();
break;
}
}
}
}
pylon_camera->Close();
pylon_camera->StopGrabbing();
}
if (stop == 1) {
cv::destroyAllWindows();
}
return 0;
}
int front_camera() {
if (stop == 0) {
Pylon::PylonAutoInitTerm autoInitTerm;
CDeviceInfo info;
String_t serialNumber = "57558330";
info.SetSerialNumber(serialNumber);
CImageFormatConverter formatConverter;
CPylonImage pylonImage;
CGrabResultPtr ptrGrabResult;
CBaslerUsbInstantCamera* pylon_camera;
cv::Mat frame, playingFrame, cannyFrame, contoursFrame;
cv::RNG rng(12345);
pylon_camera = new CBaslerUsbInstantCamera(CTlFactory::GetInstance().CreateFirstDevice(info));
pylon_camera->MaxNumBuffer = 1;
formatConverter.OutputPixelFormat = PixelType_BGR8packed;
pylon_camera->StartGrabbing();
pylon_camera->AcquisitionFrameRate = 60.0;
int i = 0;
while (i < 6) {
i++;
if (pylon_camera->IsGrabbing())
{
pylon_camera->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded() && pylon_camera->IsGrabbing())
{
const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
formatConverter.Convert(pylonImage, ptrGrabResult);
frame = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)pylonImage.GetBuffer());
cv::Size sz = frame.size();
int scale_percent = 20;
int imageWidth = sz.width;
int imageHeight = sz.height;
int down_width = imageWidth * scale_percent / 100;
int down_height = imageHeight * scale_percent / 100;
Mat resized_down;
resize(frame, resized_down, Size(down_width, down_height), INTER_LINEAR);
Mat flipped;
Mat flipped2;
flip(resized_down, flipped, 0);
flip(flipped, flipped2, 1);
cv::imshow("Front camera", flipped2);
cv::moveWindow("Front camera", 320, 20);
int c = cv::waitKey(1);
if ((char)c == 'c') {
cv::destroyAllWindows();
break;
}
}
}
}
pylon_camera->Close();
pylon_camera->StopGrabbing();
}
if (stop == 1) {
cv::destroyAllWindows();
}
return 0;
}
What I have tried:
I have tried that if I dont do the function toggling and run only the other function, it will run forever and the application wont become unresponsive.