Therefore, how can I easily interface between managed and unmanaged code in the following example:
Code: Select all
#pragma once
#pragma unmanaged
#include "opencv2\imgproc\imgproc.hpp"
#pragma managed
namespace WorkTest {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
#pragma unmanaged
class DoWorkAsUnmanaged
{
public:
// constructor
DoWorkAsUnmanaged()
{
result = new cv::Mat();
};
// destructor
~DoWorkAsUnmanaged()
{
delete result;
};
cv::Mat* DoWork(cv::Mat *image0, cv::Mat *image1, cv::Mat *image2)
{
cv::add(*image0, *image1, *result);
cv::add(*result, *image2, *result);
return result;
}
private:
cv::Mat *result;
};
#pragma managed
/// Form1
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
/// Clean up any resources being used.
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// Required designer variable.
System::ComponentModel::Container ^components;
private: System::Windows::Forms::Button^ button1;
#pragma region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(103, 33);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Go!";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 88);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"C++ Test";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
array<Emgu::CV::Image<Bgr, Byte>^>^ image = gcnew array<Emgu::CV::Image<Bgr, Byte>^>(3);
try{
image[0] = gcnew Emgu::CV::Image<Bgr, Byte>("C:\\Temp\\image0.png");
image[1] = gcnew Emgu::CV::Image<Bgr, Byte>("C:\\Temp\\image1.png");
image[2] = gcnew Emgu::CV::Image<Bgr, Byte>("C:\\Temp\\image2.png");
}
catch (CvException ^ex) {
Debug::WriteLine(ex->Message);
}
DoWorkAsUnmanaged *workObject = new DoWorkAsUnmanaged();
// need help HERE!!
? = workObject->DoWork(?, ?, ?);
}
};
}
I realize the DoWork() example is trivial, but the real DoWork() functions are much more involved as far as how they operate on the images. The native code speed advantage is too large to ignore in my case.
Thanks in advance for any help!