07-11-2011 04:36 AM - edited 07-11-2011 04:37 AM
Hi, I'm writing a wrapper DLL to handle calls to the OpenCV 2.1 image processing library.
When using the OpenCV function 'cv::matchTemplate' I get different results depending on if the wrapper DLL is set to run in the UI thread or any thread.
When set to the UI thread I get incorrect results (see attachement resultUI.png).
When set to any thread I get correct results (see attachment resultAny.png).
I've created a minimal DLL which shows this behaviour:
#include <cv.h>
#include <highgui.h>
extern "C" __declspec(dllexport) int matchTest(int a);
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) int matchTest(int a)
{
cv::Mat srcImg, templateImg, result32Img, result8Img;
cv::Size resultSize;
srcImg= cv::imread("source.bmp", 0);
templateImg = cv::imread("template.bmp", 0);
resultSize.width = srcImg.size().width - templateImg.size().width + 1;
resultSize.height = srcImg.size().height - templateImg.size().height + 1;
result32Img = cv::Mat(resultSize, CV_32FC1);
result8Img = cv::Mat(resultSize, CV_8UC1);
cv::matchTemplate(srcImg, templateImg, result32Img, CV_TM_CCOEFF_NORMED); //returns different results
//convert to 8 bit and write to file
result32Img.convertTo(result8Img, CV_8UC1, 256);
cv::imwrite("result.png", result8Img);
return a;
}
Why do I get different results?
Is there a way to get the 'Run in UI thread' version to return the correct results (I'm not sure that my wrapper DLL is/will be thread safe, I'm checking).
.
Thanks in advance,
Dave
07-18-2011 04:46 AM
Update:
I realised that I had also set the execution system of the VI to 'other 1'.
If I set it to 'user interface' then it doesn't matter if the CLFN is set to run in UI thread or any thread, I get the incorrect result. I presume this is because there is only one thread in the 'user interface' execution system.
The dll I posted above is just to demonstrate the issue. The actual dll I use contains more functions and calls to openCV. I can't be sure that it is thread safe, so would like to force it to run in a single thread, but not the UI thread because of the matchTemplate issue. Looking at this post it looks like I have two options, the threadconfig suggestion or the NIComputeContext route. The Compute Context route seems neater since I don't have to worry about setting theadconfig if I use the code in multiple projects. So before I go diving into it, is that right, or is there a better way?
Thanks
Dave
08-03-2011 09:27 AM
Hi Dave,
This sounds like a very specific and unique issue that not many of us have come across before. I wasn't able to find any useful information on the topic to help so far, but was wondering how you got on with the application?
Regards,
Imtiaz Chowdhury
Project Manager
Green Running / Austin Consultants