如何使用c#接口显示opecv读取的图像 [英] How do I show image read by opecv by using a c# interface

查看:106
本文介绍了如何使用c#接口显示opecv读取的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试使用c#interace运行c ++代码。我已经尝试了一些东西,现在可以将例如52返回到c#接口并将52写入文本框。当我想要显示由opencv库函数读取的图像时,我的问题出现了。



这是用于打印内容并在窗口上显示图像的NativeLib.c />

Hi everyone,

I am stdying on running c++ codes with a c# interace. I have tried something and now a can return for example "52" to c# interface and write "52 "to a textbox. My problem occurs when i want to show an image that read by opencv library functions.

That is the NativeLib.c for printing something and showing image on a window

#include "stdafx.h"
#include "NativeLib.h"
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include <string.h>
using namespace cv;
using namespace std;

MYAPI int print_line(const char* str) {
	return 52;
}
MYAPI void ShowImage()
{
    Mat image;
    image = imread("aile.jpg", CV_LOAD_IMAGE_COLOR); 
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );   
}


That is NativeLib.h
#ifndef _NATIVELIB_H_
#define _NATIVELIB_H_

#ifndef MYAPI
#define MYAPI
#endif

#ifdef __cplusplus
extern "C"{
#endif


MYAPI int print_line(const char* str);
MYAPI void ShowImage();

#ifdef __cplusplus
}
#endif

#endif // _NATIVELIB_H_



即c#interface


That is c# interface

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace InterfaceForServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("bitirme2.dll", EntryPoint = "print_line")]
        private static extern int print_line(string str);

        [DllImport("bitirme2.dll", EntryPoint = "ShowImage")]
        public static extern void ShowImage();

        public void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = print_line("Hello, PInvoke!").ToString();
            ShowImage();
        }
    }
}



i可以在textBox中看到52,但是当ShowImage运行时它会给出



InterfaceForServer.exe中发生未处理的System.Runtime.InteropServices.SEHException类型异常



其他信息:外部组件抛出异常。



我用Google搜索但是每个人都说可能有关于dll的错误。但正如我上面说的打印功能运行良好所以我不确定问题是在DLL。并且还确定了每次在输出目录重建后生成的dll。



问题出现在行



imshow(显示窗口,图片);



如果没有什么清楚请发帖



谢谢


i can see "52" in the textBox, but while ShowImage is running it gives

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

Additional information: External component has thrown an exception.

I have googled but everyone says there could be and error about dll. But as i told above print function is running well so i am not sure the problem is at dll. And also dll is generated after every rebuilt at the output directory, i determined.

The problem is occured in line

imshow( "Display window", image );

If there is nothing clear please post

Thanks

推荐答案

System.Runtime.InteropServices.SEHException指示未处理的未管理(C ++)代码中的错误。



I没有在C ++中使用OpenCV,所以我不确定会出现什么问题,但如果你将函数包装在try / catch中并将错误写入文本文件,它应该可以帮助你更好地理解发生了什么错误当它击中imshow()时。



最常见的原因是imshow的问题是加载图像时出现问题,请确保图像位于正确的位置。



System.Runtime.InteropServices.SEHException indicates an error in the un-managed (C++) code that isn't being handled.

I haven't used OpenCV in C++, so I am not sure what the problem would be, but if you wrap your function in a try/catch and write the error out to a text file, it should help you better understand what error is occurring when it is hitting imshow().

The most common cause is a problem with imshow is a problem with loading the image, make sure the image is in the correct location.

MYAPI void ShowImage()
{
try{
    Mat image;
    image = imread("aile.jpg", CV_LOAD_IMAGE_COLOR); 
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );   
}
catch(int i)
{
//write out to a log file.
}
}


非常感谢大家。

错误是关于将显示的路径在窗口。我将jpg文件复制到c#Interface项目的Debug目录,并且工作正常
Thanks a lot to everyone.
The error was about path that will show on window. I copied the jpg file to "Debug" directory of c# Interface project and, it worked clearly


这篇关于如何使用c#接口显示opecv读取的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆