Unhandled Exception when using cv::resize(....) with 5 threads simultaneously

asked 2018-10-15 03:48:09 -0500

BHASKAR gravatar image

updated 2018-10-15 04:44:12 -0500

berak gravatar image

Using visual c++ (15.7.5 - 2017).

When using this line (by 5 threads) :

cv::resize(img_export_5, resized_img_export_5_ext, size, 0, 0, INTER_CUBIC);

I get this error frequently:

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception. at cv.resize(_InputArray* , _OutputArray* , Size_<int>* , Double , Double , Int32 ) at UFO.UFO_microscopy.Thread_export_5.ThreadEntryPoint() in c:\users\ufo\desktop\f7 cimg updated\ufo\ufo\ufo_microscopy.h:line 2402 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

Here is the block where I get the error:

Mat img_export_1(4000, 8800, CV_8U, Scalar(0));

Mat resized_img_export_1_ext;

while (export_thread_1_cont)
{cv::Size size(calibrated_x_length_external, y_length_read_bin_file); cv::Size size_final(0, 0);

    while (!export_frame_available_1)
    {
        if (!export_thread_1_cont)  break;
        // WAITS FOR FRAME
    }
    if (!export_thread_1_cont)  break;

    for (int width = 0; width < y_length_read_bin_file; width++)
    {
        for (int length = 0; length < samplesPerRecord_read_bin_file; length = length + 1)
        {
            avg = loaded_image_array_1[width][length];

            mapped_value = ((avg - lower_limit[selected_channel_num]) / (contrast_calibration_ext[length] - lower_limit[selected_channel_num])) * 255;

            img_export_1.at<uchar>(width, length) = mapped_value;
        }
    }

    resize(img_export_1, resized_img_export_1_ext, size, 0, 0, INTER_CUBIC);

    sprintf(export_file_name, "%s\\%d.bmp", export_directory_full, selected_frame_export_1);
    imwrite(export_file_name, resized_img_export_1_ext);

    export_frame_available_1 = false;
    goto back_exp_1;
}

Note: When I use 4 threads, I get this error less frequently.

Please help me!!!!Please help me!!!Please help me!!!!

edit retag flag offensive close merge delete

Comments

what made you think, it was threadsafe, ever ?

berak gravatar imageberak ( 2018-10-15 03:52:54 -0500 )edit

System.Threading -- also, managed code here ?

berak gravatar imageberak ( 2018-10-15 03:54:17 -0500 )edit

where the hell comes this from ?

cv.resize(_InputArray* , _OutputArray* , Size_<int>* , Double , Double , Int32 )

(raw) pointers to InputArray , srsly ?

berak gravatar imageberak ( 2018-10-15 03:56:43 -0500 )edit

link text

this yellow marked line is giving the error. Basically, i am reading a 2D matrix to form a Mat. And then, trying to resize that Mat. Resizing process gives error when more than 4 threads are used.

BHASKAR gravatar imageBHASKAR ( 2018-10-15 04:15:10 -0500 )edit

I understood the root cause of the problem. I declared lots of 2D arrays which already occupied large amount of RAM. When, I used more and more threads, it needed more & more memory to create the Mat object during resize operation dynamically. Since, my app is 32 bit, it crashes when memory requirement becomes more than ~2GB.

BHASKAR gravatar imageBHASKAR ( 2018-10-16 05:40:21 -0500 )edit

again, things you should NOT do here:

  • use multithreading. (most opencv code is NOT threadsafe by design)
  • use managed code (opencv uses RAII, and it does not fit)
  • write your own for loops
  • use your own 2d arrays (use cv::Mat instead)
  • use raw pointers to anything (defeats the refcounting)
berak gravatar imageberak ( 2018-10-16 06:29:19 -0500 )edit

oh, it also has a goto . what a mess.

berak gravatar imageberak ( 2018-10-16 06:34:29 -0500 )edit