There are time delay when outputing frames in C++ project with OpenCV
Hi. I set stereo system using BFS-U3-50S5C-C in C++ with OpenCV, but there are time delay when outputing frames.
I stored and show frames using cv::Mat object, and time delay is almost 3 sec.
In the spinview, there is no time delay so I think my setting code was wrong.
here is the my code and function`s call relationship is as follows.
main -> FLIR_frame_get -> AcquireImages(part that display the frame actually)
----------------------------------------------code------------------------------------------------
cv::Mat raw_frame_color;
int main(int argc, char* argv[]) {
FLIR_frame_get();
}
void FLIR_frame_get() {
FILE* tempFile = fopen("test.txt", "w+");
if (tempFile == nullptr)
{
std::cout << "Failed to create file in current folder. Please check permissions." << endl;
std::cout << "Press Enter to exit..." << endl;
getchar();
}
fclose(tempFile);
remove("test.txt");
// Print application build information
std::cout << "Application build date: " << __DATE__ << " " << __TIME__ << endl << endl;
// Retrieve singleton reference to system object
SystemPtr system = System::GetInstance();
// Print out current library version
const LibraryVersion spinnakerLibraryVersion = system->GetLibraryVersion();
std::cout << "Spinnaker library version: "
<< spinnakerLibraryVersion.major << "."
<< spinnakerLibraryVersion.minor << "."
<< spinnakerLibraryVersion.type << "."
<< spinnakerLibraryVersion.build << endl << endl;
// Retrieve list of cameras from the system
CameraList camList = system->GetCameras();
const unsigned int numCameras = camList.GetSize();
std::cout << "Number of cameras detected: " << numCameras << endl << endl;
// Finish if there are no cameras
if (numCameras == 0)
{
// Clear camera list before releasing system
camList.Clear();
// Release system
system->ReleaseInstance();
std::cout << "Not enough cameras!" << endl;
std::cout << "Done! Press Enter to exit..." << endl;
getchar();
}
int result = 0;
pCam0 = camList.GetByIndex(1);
pCam1 = camList.GetByIndex(0);
INodeMap& nodeMapTLDevice0 = pCam0->GetTLDeviceNodeMap();
INodeMap& nodeMapTLDevice1 = pCam1->GetTLDeviceNodeMap();
result = PrintDeviceInfo(nodeMapTLDevice0);
result = PrintDeviceInfo(nodeMapTLDevice1);
pCam0->Init();
pCam1->Init();
INodeMap& nodeMap0 = pCam0->GetNodeMap();
INodeMap& nodeMap1 = pCam1->GetNodeMap();
int err = ConfigureCustomImageSettings(nodeMap0);
int err1 = ConfigureCustomImageSettings(nodeMap1);
CEnumerationPtr ptrAcquisitionMode0 = nodeMap0.GetNode("AcquisitionMode");
CEnumEntryPtr ptrAcquisitionModeContinuous0 = ptrAcquisitionMode0->GetEntryByName("Continuous");
const int64_t acquisitionModeContinuous0 = ptrAcquisitionModeContinuous0->GetValue();
ptrAcquisitionMode0->SetIntValue(acquisitionModeContinuous0);
CEnumerationPtr ptrAcquisitionMode1 = nodeMap1.GetNode("AcquisitionMode");
CEnumEntryPtr ptrAcquisitionModeContinuous1 = ptrAcquisitionMode1->GetEntryByName("Continuous");
const int64_t acquisitionModeContinuous1 = ptrAcquisitionModeContinuous1->GetValue();
ptrAcquisitionMode1->SetIntValue(acquisitionModeContinuous1);
pCam0->BeginAcquisition();
pCam1->BeginAcquisition();
processor0.SetColorProcessing(SPINNAKER_COLOR_PROCESSING_ALGORITHM_HQ_LINEAR);
processor1.SetColorProcessing(SPINNAKER_COLOR_PROCESSING_ALGORITHM_HQ_LINEAR);
Mat frame0, frame1;
atomic<bool> stopFlag(false);
std::thread t0(AcquireImages, pCam0, std::ref(frame0), std::ref(processor0), "Left camera", std::ref(stopFlag));
std::thread t1(AcquireImages, pCam1, std::ref(frame1), std::ref(processor1), "Right camera", std::ref(stopFlag));
t0.join();
t1.join();
//pCam0->EndAcquisition();
//pCam1->EndAcquisition();
//pCam0->DeInit();
//pCam1->DeInit();
//pCam0 = nullptr;
//pCam1 = nullptr;
//camList.Clear();
//system->ReleaseInstance();
}
void AcquireImages(CameraPtr pCam, Mat& frame, ImageProcessor& processor, string windowName, atomic<bool>& stopFlag)
{
int key = 0;
while (!stopFlag) {
ImagePtr pResultImage = pCam->GetNextImage(); // Retrieve next received image
if (pResultImage->IsIncomplete()) // Ensure image completion
{
std::cout << "Image incomplete: "
<< Image::GetImageStatusDescription(pResultImage->GetImageStatus())
<< "..." << endl << endl;
}
else //fundamental part to output frame
{
// Convert image to RGB
ImagePtr convertedImage = processor.Convert(pResultImage, PixelFormat_BGR8);
unsigned int rows = convertedImage->GetHeight();
unsigned int cols = convertedImage->GetWidth();
unsigned int num_channels = convertedImage->GetNumChannels();
void* image_data = convertedImage->GetData();
unsigned int stride = convertedImage->GetStride();
frame = cv::Mat(rows, cols, (num_channels == 3) ? CV_8UC3 : CV_8UC1, image_data, stride);
resize(frame, frame, Size(), 0.6, 0.6);
imshow(windowName, frame);
if (windowName == "Left camera") {
moveWindow(windowName, 0, 0);
}
else {
moveWindow(windowName, frame.cols, 0);
}
if (key == 13 && windowName == "Left camera") { // Enter
stopFlag = true;
raw_frame_color = frame.clone();
break;
}
key = waitKey(10);
}
pResultImage->Release(); // Release the image
}
//pCam->EndAcquisition(); // End acquisition
}
----------------------------------------------------------------------------------------------
CPU threads are used, and 'imshow(windowName, frame);' part in 'AcquireImages' function displays cv::Mat object what stored camera frame.
The reason for using thread was to try to get rid of time delay, so using thread is not mandatory.
Strangely, time delay did not occur with the current code a few days ago, but now time delay is occurring.
If I can save the frame to cv::Mat and display that without time delay, I don't have to keep my current code.
I'd be very happy if someone could help me.
Thank you
-
Hello Jonghak,
It is not clear from the shared code how and where you computed the time delay or the exact function call with most delay?
I would recommend to insert and measure time taken at different section of the program (i.e from GetNextImage(...) downto waitkey(..)) to determine and minimize this delay.
I hope this approach helps to resolve it.
Regards,
Ifeanyi
0
Please sign in to leave a comment.
Comments
1 comment