c++ - OpenCV IplImage AccessViolationException

标签 c++ opencv access-violation iplimage

首先,我是 OpenCV 的新手。 我尝试了大约一个星期但没有成功,看来我永远也做不到。这就是为什么我必须向遇到同样问题的人寻求帮助。

我想在 VC# 2010 中构建非常简单的应用程序,它基本上会执行以下操作:

  • 读取 JPEG 图像并将其存储到位图变量
  • 将位图变量发送给封装在VC++ dll中的函数
  • 在VC++dll中对图片进行简单的操作(比如画一个圆)
  • 将修改后的图片返回给VC#应用,并显示在PictureBox中

VC#中的代码:

[DllImport("CppTestDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern Bitmap testImage(Bitmap img);

private void button1_Click(object sender, EventArgs e)
    {
        // read the source jpeg from disk via a PictureBox
        Bitmap bmpImage = new Bitmap(pictureBox1.Image);

       //call the function testImage from the VC++ dll
   //  and assign to another PictureBox the modified image returned by the dll
        pictureBox2.Image = (System.Drawing.Image)testImage(bmpImage);
    }

VC++ dll 中的代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

#include <stdio.h>

using namespace cv;
using namespace std;

    __declspec(dllexport) char* testImage(char *image)
    {
        //the image is 640x480
        //read the jpeg image into a IplImage structure
       IplImage *inputImg= cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 3); 
       inputImg->imageData = (char *)image;

        // I also tried to copy the IplImage to a Mat structure
        // this way it is copying onl the header
        // if I call Mat imgMat(inputImg, true); to copy also the data, I receive an error for Memory read access
        Mat imgMat(inputImg);

        // no matter which circle drawing method I choose, I keep getting the error
        //  AccessViolationException was unhandled. Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

        cvCircle(inputImg,Point(100,100),100,cvScalar(0, 0, 255, 0),1,8,0);
        circle(imgMat,Point(100,100),100,Scalar(0, 0, 255, 0),1,8,0);

        // I tried both ways to return the image
        // If I try to modify the image I receive the above described error
        // If I don't modify the input image, I can send back the original image, but it is useless

        //return (char *)imgMat.data;
        return (char *)inputImg->imageData;
    }

能否请您告诉我我哪里弄错了? 或者也许提供一个小的示例代码来告诉我如何去做?

更新 如果我在 VC++ dll 中使用 cvImageLoad 从磁盘读取 jpeg 文件,绘图操作是有效的,我可以返回修改后的图像。问题只是以正确的方式将图像发送到 dll。有什么建议吗?

还有 我像这样在 VC++ 中更改了 dll

__declspec(dllexport) char* testImage(uchar* image)
{
        uchar *pixels = image;
        Mat  img(480,640,CV_8UC3,pixels);

        if (!img.data)
        {
            ::MessageBox(NULL, L"no data", L"no data in imgMat mat", MB_OK);
        }

        line(img,Point(100,100),Point(200,200),Scalar(0, 0, 255, 0),1,8,0);
        return (char *)img.data;
    }

画线操作失败,但是如果我评论画线,我可以得到返回的图像。

这是怎么回事?

最佳答案

当您将位图图像提供给将其作为 char* 的 testImage 函数时,可能会出现转换问题。我这么认为的原因是当您没有图像数据并尝试获取它时会出现这种错误。你能调试它并查看数据是否在图像中可用或使用

if(!imgMat.data)
   //error. Print something or break

编辑

我没有在 C# 中使用 Opencv,但通常人们使用 opencvsharp,emgu 或其他替代品。但你的方式似乎不合适。在 C# 中使用 opencv dll;他们说它需要 wrapper 或类似的东西。他们建议将 Emgu 用于 C#。

您是否在启用 COM 互操作的情况下编译了您的 dll? (要在 C# 中使用 c++ dll,他们说它应该编译为 COM)。但我不认为它仍然有效。 所有这些 Emgu、opencvdotnet、opencvsharp 等包装器背后一定有原因。对吧?

关于c++ - OpenCV IplImage AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12291083/

相关文章:

qt - Qt OpenCV从捕获帧写入视频,不保存

c# - 无法捕获的 AccessViolationException

c++ - 使用子模块应用程序编译 Modular Boost 1.55.0

python - 随机裁剪数据增强卷积神经网络

c++ - 这个 C++ 宏中的 # 符号是什么意思?

image - OpenCV:加载多个图像

c - 为什么这个结构文字在 VS2013 中通过地址而不是 gcc/clang 传递时被破坏?

c# - dotnet.exe 与 w3wp.exe : what's difference?

c++ - ZeroMQ:如何在 Windows 上处理 Ctrl-C

c++ - 为什么下面的重载决议会调用非模板函数?