Unhandled exception in OpenCV feature matching - c++

I'm now working on feature matching, and I wrote these codes below.
void CLPS::ExtractKeyPoints()
{
vector<DMatch> init_matches;
ULONGLONG timer;
timer = GetTickCount64();
Ptr<BRISK> fd_a = BRISK::create(20);
fd_a->detectAndCompute(S, Mat(), spoints, desc_s, false);
fd_a->detectAndCompute(T, Mat(), tpoints, desc_t, false);
timer = GetTickCount64() - timer;
cout << "Detect and compute: " << timer << " milliseconds complete. Output: " << spoints.size() << " points. \n";
timer = GetTickCount64();
FlannBasedMatcher matcher = FlannBasedMatcher(cv::makePtr<cv::flann::LshIndexParams>(10, 32, 2));
matcher.match(desc_s, desc_t, init_matches);
timer = GetTickCount64() - timer;
cout << "Matching time: " << timer << " milliseconds complete. " << endl;
Point2f spt, tpt;
int num_matches = 0;
vector<DMatch>::iterator iter_matches;
coord_matches.create(1, 3, CV_32FC1);
Mat cm_row;
cm_row.create(1, 3, CV_32FC1);
if (!init_matches.empty())
{
for (iter_matches = init_matches.begin(); iter_matches != init_matches.end(); ++iter_matches)
{
spt = spoints[iter_matches->queryIdx].pt;
tpt = tpoints[iter_matches->trainIdx].pt;
if (std::abs(spt.y - tpt.y) < VERTICAL_EPSILON && tpt.x - spt.x < upperbound && tpt.x - spt.x > lowerbound)
{
num_matches++;
coord_matches.reserve(num_matches);
cm_row.at<float>(0, 0) = spt.x;// 与 CV_32FC1 对应
cm_row.at<float>(0, 1) = spt.y;
cm_row.at<float>(0, 2) = tpt.x - spt.x;
coord_matches.push_back(cm_row);
}
}
cout << coord_matches.rows << " matches found." << endl;
}
else
{
cerr << "No match is found. Validate your source data or report to us if there is a bug. " << endl;
}
}
Then I just call it like this:
CLPS lps;
lps.ExtractKeyPoints();
However, it triggers an exception with following message when this function is returning:
Unhandled exception at 0x00007FFC0797D328 (ucrtbase.dll) in dars_lps.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
where dars_lps.exe is my application name. Then the program jumps to some destructors in <vector>.
I'm using Visual Studio 2015 on Windows 8.1 Update 1, and my OpenCV version is 3.1. I have confirmed that I'm linking to correct version of library files (i.e. vc14).
I was once working on Visual Studio 2010 on Windows 7 SP1, OpenCV 2.4.9, but it never reported such an exception.
I know this question may be similar to some other questions elsewhere (e.g. this on OpenCV site), occuring when calling different functions, but all of them are staying unsolved. I suspect that the problem lies in BRISK or FlannBasedMatcher, but I can't comment out those code (or this function will be virtually empty, and obviously, no more exceptions).
I also noticed that the problem was rising up after OpenCV 3.0 was released, and most of the similar problems happen in newer versions of Visual Studio or Windows. Both 64-bit and 32-bit platforms have this kind of problems. There does exist a report of such problem in Visual Studio 2015 here, but it was OpenCV 3.0 that is used, when there were no library built for Visual Studio 2015 yet.
Is it still a bug to be fixed or an error of syntax by myself?

Related

Qt OpenCv C++ QCameraInfo::availableCameras() return an empty list, no devices captured [SOLVED]

Asking this because i can't figure it out.
Environment: Windows 10 x64, Qt Creator 4.11.1, OpenCv 3.9, msvc 2017_64
I tried this as like the Qt Documentation says:
QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras();
for (const QCameraInfo &cameraInfo : availableCameras) {
std::cout << cameraInfo.description().toStdString() << std::endl;
}
if(availableCameras.isEmpty())
cout << availableCameras.size() << endl;
cout << "Empty" << endl;
But in output i get: "0, empty"
I don't know if it's a bug, but with another blank project without including OpenCv this code works. Can someone explain?
-EDIT-
I solved the problem deleting the directory
"build-ProjectName-Desktop_Qt_5_14_1_MSVC2017_64bit-Release"
So, i rebuilding and deploying with "windeployqt.exe ." in the directory of my project.exe

Access violation while initializing matlab-compiler dll / lib in c++ only during debugging

what I'm trying to do is integrate a MATLAB-Compiler dll/lib to an new c++ project.
I followed this instruction: How do I integrate my C++ shared Library generated from MATLAB which seams working good (no build errors and intelisense is working good, so it seams all required information are there).
I'm using a very simple mathlab code / function for testing:
function output = extest( arg1,arg2 )
output = arg1+arg2;
end
And the "default" c++ code for matlab functions:
#include "extest.h"
#include <cstdlib>
#include <stdio.h>
int main(int argc, char** argv){
mclmcrInitialize();
if (!mclInitializeApplication(NULL,0)){
std::cerr << "could not initialize the application properly" << std::endl;
return -1;
}
if(!extestInitialize()){
std::cerr << "could not initialize the library properly" << std::endl;
return -1;
}
else{
try{
//code itself (not jet reached therefore removed)
}catch(const mwException& e){
std::cerr << e.what() << std::endl;
return -2;
}
catch(...){
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
extestTerminate();
}
mclTerminateApplication();
return 0;
}
After e few moments after the debugger tries to run the line if(!extestInitialize()) the following error gets thrown.
Exception thrown at 0x000002BF72E0EE55 in DllTestingCpp.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
I can hit visual studios continue > button and it is continued after lets say 20x click on it. Starting the code by ctrl + F5 (without debugging) everything is working good.
Any ideas why this happens in debug mode? Or better how I can get rid of this error?
PS: extest is my lib name and using Matlab R2017a 64bit and Visual Studio 2017 (debugging with x64),
The same problem (Matlab2017 + VS 2015) for me.
Probably there is some conflict with java used by MATLAB.
I've fixed it by
const char *args[] = {"-nojvm"};
const int count = sizeof(args) / sizeof(args[0]);
mclInitializeApplication(args, count))
instead of
mclInitializeApplication(NULL,0)
I had the same issue(using VS2019) and I found the following answer here:
https://uk.mathworks.com/matlabcentral/answers/182851-how-do-i-integrate-my-c-shared-library-generated-from-matlab-r2013b-in-visual-studio-2013
I encountered this same issue and reported it to Mathworks. They responded that for VS2013 and later, the debugger is set to break when 0xc0000005 occurs, even though in this case it is handled by the JVM.
The fix is to go to Debug>Windows>Exception Settings>Win32 and uncheck '0xc0000005 Access Violation'. In VS2012, this setting is unchecked by default.
This seems to work o.k.

OpenCV gives Assertion failed error when running on GPU using OpenCL

I have an Nvidia GTX 970M GPU & I am trying to run a face detection algorithm in c++ that runs on the GPU using OpenCL.
The function where this error occurs is :
ocl::OclCascadeClassifier::detectMultiScale()
The error I get is :
OpenCV Error: Assertion failed (localThreads[0] * localThreads[1] * localThreads[2] <= kernelWorkGroupSize) in cv::ocl::openCLVerifyKernel
I know that this problem is related to the GPU of the device but I do not know how to fix this. I have tried using OpenCV versions 2 and 3 but both give the same problem.
The problem was that it was trying to use the Intel HD Graphics GPU instead of the Nvidia GPU. I solved this by choosing the Nvidia GPU as the OpenCL Device.
The code I used was :
cv::ocl::DevicesInfo devInfo;
int res = cv::ocl::getOpenCLDevices(devInfo);
if (res == 0)
{
std::cerr << "There is no OPENCL Here !" << std::endl;
}
else
{
for (unsigned int i = 0; i < devInfo.size(); ++i)
{
std::cout << "Device : " << devInfo[i]->deviceName << " is present" << std::endl;
}
}
cv::ocl::setDevice(devInfo[1]);

How do I specify context (platform/device information) when using OpenCL?

How do I specify context (platform/device information) when using OpenCL calls in place of OpenCV calls when using OpenCL library for OpenCV 2.4.8 in C++?
I could do it for OpenCV version 2.4.6 but I could not work it out for OpenCV version 2.4.8
Here's what I did for ver. 2.4.6:
std::vector<ocl::Info> oclinfo;
int ocld = ocl::getDevice(oclinfo);
cout<< ocld;
for ( int i=0; i< oclinfo.size(); i++ )
{
cout << "OpenCL Device" << i << ":" << oclinfo[i].DeviceName[0] << endl;
}
ocl::setDevice(oclinfo[0], 0);
I've not used version 2.4.8, but for version 2.4.9 you can use this link
also you can set environment variable OPENCV_OPENCL_DEVICE for default config.

"Access violation" Error on displaying string of simple Oracle Query (VS10 Exp C++)

I am struggling with an issue regarding running a SQL statement to an Oracle database through C++, using occi. My code is as follows:
#include <iostream>
#include "occi.h"
namespace oc = oracle::occi;
int main() {
std::cout << "Setting up environment...\n";
oc::Environment * env = oc::Environment::createEnvironment();
std::cout << "Setting up connection...\n";
oc::Connection * conn = env->createConnection("user","pass","server");
std::cout << "Creating statement...\n";
//Very simply query...
oc::Statement * stmt = conn->createStatement("SELECT '1' FROM dual");
std::cout << "Executing query...\n";
oc::ResultSet * rs = stmt->executeQuery();
while(rs->next()) {
std::cout << rs->getString(1) << std::endl; //Error is thrown at this line, but after printing since I can see '1' on the console.
}
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
oc::Environment::terminateEnvironment(env);
return 0;
}
The error that is shown is:
Unhandled exception at 0x1048ad7a (msvcp100d.dll) in MyDatabaseApp.exe: 0xC0000005: Access violation reading location 0xccccccd0.
My program stops inside 'xstring' at the following line of code:
#if _ITERATOR_DEBUG_LEVEL == 0
....
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
typedef typename _Alloc::template rebind<_Elem>::other _Alty;
_String_val(_Alty _Al = _Alty())
: _Alval(_Al)
{ // construct allocator from _Al
....
}
~_String_val()
{ // destroy the object
typename _Alloc::template rebind<_Container_proxy>::other
_Alproxy(_Alval);
this->_Orphan_all(); //<----------------------Code stops here
_Dest_val(_Alproxy, this->_Myproxy);
_Alproxy.deallocate(this->_Myproxy, 1);
this->_Myproxy = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
If I change my query to:
oc::Statement * stmt = conn->createStatement("SELECT 1 FROM dual");
and the loop statement to:
std::cout << rs->getInt(1) << std::endl;
It works fine with no errors. I think this is because getting an integer simply returns a primitive, but when an object is being returned it is blowing up (I think on a destructor, but I'm not sure why...)
I have been playing around with this for hours today, and I am pretty stuck.
Some information about my system:
OS - Windows XP
Oracle Version - 10g
IDE - Microsoft Visual Studio 2010 Express C++
My project properties are as follows:
C/C++ - General - Additional Include Directories = C:\oracle\product\10.2.0\client_1\oci\include;%(AdditionalIncludeDirectories)
C/C++ - Code Generation - Multi-threaded Debug DLL (/MDd)
Linker - General - Additional Library Directories = C:\oracle\product\10.2.0\client_1\oci\lib\msvc\vc8;%(AdditionalLibraryDirectories)
Linked - Input - Additional Dependencies = oraocci10.lib;oraocci10d.lib;%(AdditionalDependencies)
I hope I haven't been confusing with too much info... Any help or insight would be great, Thanks in advance!
EDIT If I rewrite my loop, storing the value in a local variable, the error is thrown at the end of the loop:
while(rs->next()) {
std::string s = rs->getString(1); //s is equal to "1" as expected
std::cout << s << std::endl; //This is executed successfully
} //Error is thrown here
Usually such kind of problems come from differences in build environments (IDE) of end user and provider.
Check this.
Related problems:
Unhandled exception at 0x523d14cf (msvcr100d.dll)?
Why does this program crash: passing of std::string between DLLs
First try to use correct lib and dll. If compiled in debug mode then all libs and dlls must be debug. Use VC++ Modules view to be sure that proper DLL loaded.
I was lucky with my application to have all libs compiled for MSVC2010. So I just check debug and release mode DLLs and got working application.
I revisited this issue about a month ago and I found that the MSVC2010 occi library was built for Oracle 11g. We are running Oracle 10g, so I had to use the MSVC2005 library. So I installed the outdated IDE and loaded the Debug library and it worked (for some reason the release version wouldn't work though).
EDIT
For anyone who is having the same problem I was, if downgrading the IDE from MSVC2010 to MSVC2005 with the appropriate libraries doesn't work, you could try upgrading the Oracle client from 10g to 11g and use the MSVC2010 library, as suggested by harvyS. In retrospect this would've probably been the better solution.