Unhandled exception (opencv_world300.dll)

asked 2016-01-14 12:00:17 -0500

Hi there,

I'm quite new to OpenCV and although I'm having fun learning it, I also have a problem which only occurs occasionally.

I've opened up a USB microscope and I want to be able to capture still images. I created some code and in my initial tests everything seem to be working, I then put it into it's own method and that is where the problems started. Sometimes when I press the space bar to capture an image, the image is sometimes set at an extremely low resolution and then occasionally throw an error, so I took the code out of the function and place it back into my main code.

This has sorted out the resolution problem but I still get the error and I can't figure out why. It seems to be something to do with when I'm writing the image to file (imwrite)

I've had a look on this forum and google and it might be something to do with the fact I'm using a 64-Bit system but I could be making some kind of amateurish mistake.

I've posted the code below....

int main(int argc, char** argv){
VideoCapture Microscope(0);
if (!Microscope.isOpened())
    cout << "Microscope wouldn't open" << endl;

double dWidth = Microscope.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = Microscope.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

namedWindow("Microscope", CV_WINDOW_AUTOSIZE);
namedWindow("Tools", CV_WINDOW_AUTOSIZE);

setMouseCallback("Microscope", MousePositionFunc, NULL);

cvCreateTrackbar("Brightness", "Tools", &g_slider_position, 100, onTrackbarSlide);

while (1)
    Mat frame; //Matrix Storage for frames
    bool bSuccess = Microscope.read(frame); // read a new frame from video
    if (!bSuccess) //if not success, break loop
        cout << "ERROR: Cannot read a frame from video file" << endl;

    imshow("Microscope", frame);

    Mat output = frame + Scalar(g_slider_position, g_slider_position, g_slider_position);
    namedWindow("Output", CV_WINDOW_AUTOSIZE);
    imshow("Output", output);

    int mKey;
    mKey = waitKey(33);
    if (mKey == 27)
        cout << "User Pressed Escape" << endl;

    else if (mKey == 32)
        Mat img(dHeight, dWidth, CV_16UC3, Scalar(0, 50000, 50000));
        vector<int> compression_params; //vector that stores the compression parameters of the image
        compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique
        compression_params.push_back(98); //specify the compression quality
        img = frame;

        string String = ("Screengrab" + static_cast<ostringstream*>(&(ostringstream() << counterpress))->str() + ".jpg"); // create string and cast integer to assign a value
        imwrite(String, img, compression_params); //write the image to file
        cout << "Image Saved " << counterpress << " times" << endl;

        cout << mKey << " is pressed" << endl;



return 0;}

I would appreciate the help of anyone who can figure out what I am doing wrong, if anything??

Thanks. :)

edit retag flag offensive close merge delete


I have try your program without callback MousePositionFunc and onTrackbarSlide. There is no problem with my usb camera. I don't understand

 Mat img(dHeight, dWidth, CV_16UC3, Scalar(0, 50000, 50000));

and this

 img = frame;

you can do this only

imwrite(String, frame, compression_params);
LBerger gravatar imageLBerger ( 2016-01-14 13:18:58 -0500 )edit

Hi LBerger,

Its not the USB camera or Microscope (In this case) which is the problem, that all works fine. I get the captured images into the window named "Microscope". I've also built a track bar which changes the brightness which is shown in the window named "Output". All I am trying to do is write the image frame from the camera when you press the space bar so it takes a still image.

What I don't get is that sometimes it works fine and at other times, it throws an error.

I'm very confused.

hybridrules gravatar imagehybridrules ( 2016-01-14 15:21:48 -0500 )edit

"string String" <-- there is a class named String, too, so please use another name

berak gravatar imageberak ( 2016-01-15 01:02:20 -0500 )edit

also, i doubt, if you can write 16bit into a jpeg (png, maybe)

berak gravatar imageberak ( 2016-01-15 01:03:33 -0500 )edit

As I said there is no problem. I have tested program in your post without callback. You should modified too String as berak said. I don't know if your image is in 16 bits but img is in 16 bits after you replace your image by image captured so I think it's only an 8 bits image (24 bits in RGB). I have pressed space bar and there is no problem

What is your system and opencv version ?

About 16 bits images in jpg I think answer in this post is good

LBerger gravatar imageLBerger ( 2016-01-15 01:38:25 -0500 )edit

Thanks for the suggestions guys.

I've made the change as berak as suggested thinking this might be the problem seeing as it's an amateur error. It worked a few times and then it stops again. But thanks for pointing that out. :)

I'm using MS Visual Studio Ultimate 2013, Window 7 64 bit and opencv3.

I've read your suggestions and I might need to look into it, the function code where it breaks for seemed to work fine in my previous version. Unfortunately, I've not got time at the moment, but I'll have a look in a few hours and see if I can sort this out, thanks for the suggestions and I''ll keep you updated.

hybridrules gravatar imagehybridrules ( 2016-01-15 04:14:51 -0500 )edit

Why are you initializing the matrix with Scalar(0, 50000, 50000) ?

Unsigned short maximum value is around 65000. If for some reason the sum operation (for brightness) creates a pixel with value higher than 16bit unsigned maximum, the behaviour will be undefined, it may or may not crash the program.

Pedro Batista gravatar imagePedro Batista ( 2016-01-15 04:34:07 -0500 )edit

I made the changes as suggested but the problem still persists. I think it might be something to do with way I'm trying to casting an integer to be used in the string of the imwrite. I've just tried another method but it's still not right. I am not converting the saves to pngs and used the same code of the opencv website.

The error appears to be something to do with argv..

argv 0x00000000003f72a0 {0x00000000003f72b0 "myfiles"} char * *

I could always post the complete code and if anyone wants to test it to see if they come up with the same error and let us know their 32 or 64 bit. I'd be happy to do that but it doesn't look like I can post it without answering my own question.

hybridrules gravatar imagehybridrules ( 2016-01-15 19:26:01 -0500 )edit

After much testing, I have removed the string that I am trying to cast and just sending the image straight to the imwrite as a png and its working fine no matter how many times I press the space bar to save it. This is clearly a problem with the way I am casting the int into the string.

I have tried using stringstream but the same errors occurs and I do not know of any way to solve it.

Does anyone know a way I can save the images I capture from the camera into a sequence. So when I press the spacebar, it saves as "screengrab1"...."screengrab2" etc etc.

Thanks in advance.

hybridrules gravatar imagehybridrules ( 2016-01-18 11:32:44 -0500 )edit

I do it by generating a string in a similar way you are doing.

something like:

std::stringstream ss;

ss << sequenceNumber;

std::string fileName = "Image" + ss.str() + ".jpg";

Never failed for me

Pedro Batista gravatar imagePedro Batista ( 2016-01-18 11:50:36 -0500 )edit