0

I'm using OpenCV 2.4.10 in a 32-bit C#/C++ application, I feed Bitmaps (which were taken from videos, they are in memory) from C# to C++/OpenCV using a C++/CLI wrapper and it works fine in my PC and some other machines, but I'm having issues on a server running Windows Server 2008. My code throws a SEHException, which doesn't help much except for pointing out where the problem occurred.

The failing function is this one:

void charToMat(vector<uchar> in, Mat& out, int h, int w, int stride) {

    // If stride is unknown, assume it's channels * width;
    // Have tested with AUTO_STEP too and it's worked the same.
    stride = stride > 0 ? stride : 3 * w;

    Mat bucket(Size(w, h), CV_8UC3, in.data(), stride);
    Mat conv = Mat(Size(w, h), CV_8UC1);
    cvtColor(bucket, conv, CV_BGR2GRAY);
    conv.copyTo(out);

    conv.release();
    bucket.release();
}

Usually it gives me this exception:

System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at cv.Mat.create(Mat* , Int32 , Int32* , Int32 )
at cv.Mat.create(Mat* , Int32 rows, Int32 cols, Int32 type) in ...\mat.hpp:line 353
at cv.Mat.{ctor}(Mat* , Size_<int>* size, Int32 type) in ...\mat.hpp:line 88
at charToMat(vector<unsigned char\,std::allocator<unsigned char> >* in, Mat* out, Int32 h, Int32 w, Int32 stride) in ...\utils.cpp:line 15

To break down the problem and test it, I changed the code a bit. Instead of receiving a input, the method just creates a gray Mat image inside the function:

void charToMat(vector<uchar> in, Mat& out, int h, int w, int stride) {
    // Testing with fixed size now.
    Mat bucket(Size(100, 100), CV_8UC3, Scalar(127, 127, 127));
    Mat conv = Mat(Size(100, 100), CV_8UC1);
    cvtColor(bucket, conv, CV_BGR2GRAY);
    conv.copyTo(out);

    conv.release();
    bucket.release();
}

This has thrown that, indicating a problem in the same line 353 at mat.hpp, meaning it's not a pointer/interop issue. Instead, it just can't create a simple Mat.

System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at cv.Mat.create(Mat* , Int32 , Int32* , Int32 )
at cv.Mat.create(Mat* , Int32 rows, Int32 cols, Int32 type) in ...\mat.hpp:line 353
at cv.Mat.{ctor}(Mat* , Size_<int>* size, Int32 type, Scalar_<double>* s) ...\mat.hpp:line 94
at charToMat(...) in ...\utils.cpp:line 12

Can anyone give me a clue what to do here? If I need to, I'll get the images through the disk/imread, but it's hardly the best way to do it, and maybe imread would yield the same error.

Eric Omine
  • 489
  • 4
  • 15
  • 1
    Seems the exception occurs in line `Mat conv = Mat(Size(w, h), CV_8UC1);`. Tip: Catch the exception before returning and output / inspect it. – Deduplicator Oct 19 '15 at 21:15
  • @Deduplicator I have difficulty catching this exception because of the interop. I can get the .net exception shown above. I have tried catching a cv::Exception but haven't managed to do so, it doesn't enter the catch. Haven't tried using __except because everyone advises against it. Any tips? – Eric Omine Oct 20 '15 at 04:27
  • 1
    Did you try catching it and logging in that method? Is that method compiled natively? How about activating mixed-mode-debugging in your IDE? – Deduplicator Oct 20 '15 at 10:58
  • I removed the try-catch from the excerpt for briefness. The inner project should be compiled natively, but I activated `/clr` flag because without it I wasn't get any kind of exception. When it's fixed I intend to remove it. Both the wrapper and the wrapped projects use `/EHa`. I used `try{...}catch(System::Exception^ ex){throw ex;}catch(cv::Exception& ex){logException(ex);}` to try catching the exceptions. `logException()` just gets ex.what() and writes it to a file. I used mixed-mode debugging (auto mode, actually), but in this case, I have no VS to debug in the server, can't install either. – Eric Omine Oct 20 '15 at 12:08
  • You know native code (mostly, could throw any native type) throws exceptions derived from `std::exception` (no idea what `cv::Exception` is...)? – Deduplicator Oct 20 '15 at 12:14
  • `cv::Exception`extends `std::exception` and it comes from OpenCV. I've tested with both `cv::Exception` and `std::exception`. I'll try it again, but I think that Structured Exception Handling can only be done with `__try{}__except()` and even the MSDN [page](https://msdn.microsoft.com/en-us/library/swezty51.aspx) on that advises against using it, but maybe I could use it only for figuring this out? Also, didn't want to use it because it's too hard, didn't want to do things C style when I'm using C++, I thought maybe there was an faster way. – Eric Omine Oct 20 '15 at 12:28

0 Answers0