mardi 2 février 2016

multi-threading openCV app and CoreAnimation

My program launches a number of threads each of which calls cv::imshow on a different window, like this:

// displayMat
void displayMat(const cv::Mat& mat, unsigned int wait) {
  std::string unique_name = /* generate a unique_name somehow */
  cv::namedWindow(unique_name);
  cv::imshow(unique_name, mat);
  cv::waitKey(wait);
}

void displayMatThread(const cv::Mat& mat, unsigned int wait) {
  std::thread t {displayMat, mat, wait};
  t.detach;
}

// main
int waitTime {1000};
std::vector<cv::Mat> images = /* populate with images somehow */}; 
std::for_each(images.begin(),images.end(),std::bind(&displayMatThread, std::placeholders::_1,waitTime))
std::this_thread::sleep_for(5s)

When compiling on my macbook I almost get the desired results (which is probably a sound indication that I am doing something wrong here..):

Sometimes it works but I get this warning:

CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

Some other times it just posts this error (and then the program wither exits normally after the 5seconds have passed):

_RegisterApplication(), FAILED TO REGISTER PROCESS WITH CPS/CoreGraphics in WindowServer, err=-50

Some other times I get something like this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* NSMapInsertKnownAbsent(): key 0x1084d already in table' * First throw call stack: ( 0 CoreFoundation 0x00007fff9c156ae2 __exceptionPreprocess + 178 1 libobjc.A.dylib 0x00007fff8fe6173c objc_exception_throw + 48 2 CoreFoundation 0x00007fff9c15698d +[NSException raise:format:] + 205 3 Foundation 0x00007fff961e3868 -[NSClassicMapTable setItem:forKnownAbsentKey:] + 149 4 AppKit 0x00007fff898c44d8 CreateCLUT + 265 5 AppKit 0x00007fff898c41fb _NSReadThemePatternFromRsrc + 438 6 AppKit 0x00007fff898c3d05 _NSReadThemeImageFromplutEntry + 252 7 AppKit 0x00007fff898c3ade GetThemeImage + 342 8 AppKit 0x00007fff898c3915 +[NSColor _controlColor] + 44 9 AppKit 0x00007fff898c3884 -[NSDynamicSystemColor recacheColor] + 197 10 AppKit 0x00007fff898c36b5 -[NSDynamicSystemColor initWithSelector:] + 89 11 AppKit 0x00007fff898c35ad +[NSColor controlColor] + 80 12 AppKit 0x00007fff898f2740 -[NSTextFieldCell drawsBackground] + 41 13 AppKit 0x00007fff898f2606 -[NSTextFieldCell interiorBackgroundStyle] + 370 14 AppKit 0x00007fff898f2287 -[NSCell _textAttributes] + 93 15 AppKit 0x00007fff898f1da9 -[NSTextFieldCell _textAttributes] + 274 16 AppKit 0x00007fff898fbf73 __NSGetStringAndAttributesFromTextCell + 414 17 AppKit 0x00007fff898fb803 _NSGetTextCellBoundingRect + 135 18 AppKit 0x00007fff898fb6f1 -[NSCell cellSizeForBounds:] + 270 19 AppKit 0x00007fff898fb3ea -[NSTextFieldCell cellSizeForBounds:] + 100 20 AppKit 0x00007fff898fb380 -[NSCell cellSize] + 68 21 AppKit 0x00007fff8a04b3e1 +[NSTitledFrame _titleCellSizeForTitle:styleMask:] + 234 22 AppKit 0x00007fff8a04717d +[NSThemeFrame minFrameWidthWithTitle:styleMask:] + 170 23 libopencv_highgui.3.1.dylib 0x000000010f9d5f2e cvNamedWindow + 235 24 TowardsTheMean 0x000000010ef85c95 _ZN12_GLOBAL__N_115helper_internal10displayMatERKN2cv3MatEj + 1237 25 TowardsTheMean 0x000000010efb2805 _ZNSt3__114__thread_proxyINS_5tupleIJPFvRKN2cv3MatEjES3_jEEEEEPvS9_ + 517 26 libsystem_pthread.dylib 0x00007fff8ee55c13 _pthread_body + 131 27 libsystem_pthread.dylib 0x00007fff8ee55b90 _pthread_body + 0 28 libsystem_pthread.dylib 0x00007fff8ee53375 thread_start + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException Abort trap: 6

Some online resources mention that cv::imshow/cv::waitkey should be only called by main, but is this really the case? Is there a way around this or I am simply looking for something not supporting by my hardware? Is there some better approach to multi-threaded openCV applications (note that I'd prefer using standard ç++11/14 rather than some third party threading library)?

Aucun commentaire:

Enregistrer un commentaire