Click here to Skip to main content
15,883,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
<pre>	   // Cameras on button
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);

	}


}

		//Cameras off button
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
	stop = 1;
	stopCycle = 1;
}




test.cpp:
C++
<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.
Posted
Updated 25-Jan-23 1:03am
v3

I do not have answer for you but I have run into similar situations in the past and what I did was I added various trace messages so I could figure where in the code it was hanging. That helped to know exactly where I should begin looking to solve the problem. Given that what we have here is a piece of code that no one else can run and debug I think that is the best way to proceed.

What I mean by "trace" is some mechanism for your program to emit text messages that tell you what it is doing. They could be printf statements to a console that you allocate or just about anything. Another possibility is to use the old TraceWin program by the late Paul DiLascia which you can find at Paul's Home Page[^]. I successfully used it for several years until I wrote my own thing.

Good luck.
 
Share this answer
 
Comments
0x01AA 25-Jan-23 7:41am    
There is also that old OutputDebugString in combination with "Dbgview.exe"
I changed it to work in different way. myForm.h calls only one function from test.cpp and that function does the toggling now. Works fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900