Home Giving "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred"
Reply: 0

Giving "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred"

MD Rijwan
1#
MD Rijwan Published in 2018-01-11 22:47:16Z

I am working in visual studio 2015 and this is the window form I have created for the project. I want to show the Gray,binary,rotated and Adaptive Binary image of one source image in picture boxes. I build the project successfully and the browse button to open and find the source image is working fine but when i click process button it gives the error:

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred.

Additional information: External component has thrown an exception.

And the code always get stuck at ToAdaptiveBinary function!! My complete code is:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) { //Browse Button click Event
    OpenFileDialog^ dgOpen = gcnew OpenFileDialog();
    Mat res;
    dgOpen->Filter = "Image(*.bmp; *.jpg) | *.bmp; *.jpg | All files(*.*)|*.*||";
    if (dgOpen->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
    {
        return;
    }
    Bitmap^ bmpsrc = gcnew Bitmap(dgOpen->FileName);
    ptbSource->Image = bmpsrc;
    ptbSource->Refresh();
    src = imread(ConvertString2Char(dgOpen->FileName));
    //imshow("Source Image showing via OpenCV", src);
}
private: char* ConvertString2Char(System::String^ str) {
    char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
    return str2;
}
private: System::Void prcButton_Click(System::Object^  sender, System::EventArgs^  e) { //Process Button click event
    Mat gray;
    Mat blue;
    Mat red;
    Mat green;
    Mat rotate;
    Mat binary;
    Mat adaptivebinary;
    Mat resizeimg;

    resizeimg = ToResize(src);     //Resizing the original image
    imshow("resize image", resizeimg);

    gray = ToGray(resizeimg);
    DrawCVImage(ptbGray, gray);   //ptbGray is picture box to show gray image
    binary = ToBinary(gray);
    DrawCVImage(ptbBinary, binary);  //ptbBinary is picture box to show Bianry image
    adaptivebinary = ToAdaptiveBinary(gray);
    DrawCVImage(ptbAdaptBinary, adaptivebinary);  //ptbAdaptiveBinary is picture box to show adaptive Binary image

    rotate - ToRotate(resizeimg, 45, 1);   //To rotate the source image
    DrawCVImage(ptbRotate, rotate);


    red = ToHistogram(resizeimg)[0];       //To draw the histograms
    green = ToHistogram(resizeimg)[1];
    blue = ToHistogram(resizeimg)[2];
    DrawCVImage(ptbRed, red);        //ptbRed is picture box to show histogram of Red color
    DrawCVImage(ptbGreen, green);
    DrawCVImage(ptbBlue, blue);

}
private: Mat ToGray(Mat src) {    //To convert into gray
    Mat dst;
    cvtColor(src, dst, CV_RGB2BGR);
    return dst;
}
private: Mat ToBinary(Mat src) { //To convert into Binary
    Mat dst;
    threshold(src, dst, 100, 255, CV_THRESH_BINARY);
    return dst;
}
private: Mat ToAdaptiveBinary(Mat src) {  //To Display Image with threshold
    Mat dst;
    adaptiveThreshold(src, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 35, 5);
    return dst;
}
private: Mat ToResize(Mat src) {
    Mat dst;
    resize(src, dst, cv::Size(320, 240), 0, 0, 1);
    return dst;
}
private: Mat ToRotate(Mat src, double angle, double scale) {
    Mat dst;
    Point2f center(src.cols / 2, src.rows / 2);
    Mat mat_rot = getRotationMatrix2D(center, angle, scale);
    warpAffine(src, dst, mat_rot, src.size());
    return dst;
}
private: vector<Mat> ToHistogram(Mat src) {
    vector<Mat> result;
    vector<Mat> img_rgb;
    Mat img_r, img_g, img_b;
    int chart_w = 400;
    int chart_h = 400;
    int size_histogram = 255;
    float range[] = { 0,255 };
    const float* hist_range = { range };
    split(src, img_rgb);
    calcHist(&img_rgb[0], 1, 0, Mat(), img_b, 1, &size_histogram, &hist_range, true, false);
    calcHist(&img_rgb[1], 1, 0, Mat(), img_g, 1, &size_histogram, &hist_range, true, false);
    calcHist(&img_rgb[2], 1, 0, Mat(), img_r, 1, &size_histogram, &hist_range, true, false);
    int bin = cvRound((double)chart_w / size_histogram);
    Mat disp_r(chart_w, chart_w, CV_8UC3, Scalar(255, 255, 255));
    Mat disp_g = disp_r.clone();
    Mat disp_b = disp_r.clone();

    normalize(img_b, img_r, 0, disp_b.rows, NORM_MINMAX, -1, Mat());
    normalize(img_g, img_g, 0, disp_b.rows, NORM_MINMAX, -1, Mat());
    normalize(img_r, img_b, 0, disp_b.rows, NORM_MINMAX, -1, Mat());

    for (int i = 1; i < 255; i++) {
        line(disp_r, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h - cvRound(img_r.at<float>(i))),
            Scalar(0, 0, 255), 2, 8, 0);
        line(disp_g, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h - cvRound(img_r.at<float>(i))),
            Scalar(0, 255, 0), 2, 8, 0);
        line(disp_b, cv::Point(bin*(i), chart_h), cv::Point(bin*(i), chart_h - cvRound(img_r.at<float>(i))),
            Scalar(255, 0, 0), 2, 8, 0);
    }

    result.push_back(disp_r);
    result.push_back(disp_g);
    result.push_back(disp_b);
    return result;
}
private: void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
    System::Drawing::Graphics^ graphics = control->CreateGraphics();
    System::IntPtr ptr(colorImage.ptr());
    System::Drawing::Bitmap^ b;
    switch (colorImage.type())
    {
    case CV_8UC3:
        b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step,
            System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
        break;
    case CV_8UC1:
        b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step,
            System::Drawing::Imaging::PixelFormat::Format8bppIndexed, ptr);
        break;
    default:
        break;
    }
    System::Drawing::RectangleF rect(0, 0, (float)control->Width, (float)control->Height);
    graphics->DrawImage(b, rect);
}

if someone could explain how to solve this issue, I would greatly appreciate it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303534 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO