Convert Emgucv MCvMat to opencv Mat

  • Thread starter Thread starter Que2701
  • Start date Start date
Q

Que2701

Guest
We have a C++ program that performs homography. The estimation results have to be pushed back to a C# application. How the entire thing is supposed to work is: User plays video from C# application, crops a region of interest where the frame together with the rectangle points gets send to the C++ program. On the C# side, Emgucv is being used to serve the frames. We are trying to create a C++ wrapper for C# and the problem so far seems to be passing frames as something equivalent to cvMat. The error is "OpenCV: Array should be CvMat or IplImage"

Here is the C++ function

include <opencv2/core/core.hpp>
include <opencv2/calib3d/calib3d.hpp>
include <opencv2/imgproc/imgproc.hpp>
include <math.h>
include <iostream>
include <vector>

typedef unsigned int uint;

void CalculateMatrices(cv::Mat &x,cv::Mat &y,cv::Size image_size,std::string calibration_file, double camera_height, double camera_angle)
{
cv::Mat K;

cv::FileStorage reader(calibration_file,cv::FileStorage::READ);
reader["intrinsic"] >> K;
reader.release();

double fx,fy,v0,u0;
fx = K.at<double>(0,0);
fy = K.at<double>(1,1);
u0 = K.at<double>(0,2);
v0 = K.at<double>(1,2);

y = cv::Mat::zeros(image_size,K.type());
x = cv::Mat::zeros(image_size,K.type());
for(int i = 0; i < image_size.height; ++i)
{
for(int j = 0; j < image_size.width; ++j)
{
double ynow = camera_height*tan(camera_angle + atan((double(image_size.height - i)-v0)/fy));
y.at<double>(i,j) = ynow;
double hypot = sqrt(ynow*ynow + camera_height*camera_height);
double xnow = ((double(j)-u0)*hypot)/fx;
x.at<double>(i,j) = xnow;
}
}
}

The C++ wrapper class looks like below:

// Estimation.cpp : Defines the exported functions for the DLL application.
//

include "stdafx.h"
//include the location of the header and source files
include ".\main.cpp"


extern "C" _declspec(dllexport) void CalcteMatrices(cv::Mat x,cv::Mat y,cv::Size image_size,std::string calibration_file, double camera_height, double camera_angle)
{
CalculateMatrices(x,y,image_size,calibration_file,camera_height,camera_angle);
}


After reading the Emgucv documentation, it appears the equivalent of cvMat is MCvMat but the tutorials shows how to work with Image object. A bit uncertain if cvMat is a pointer, if so to what datatype? and do we need unsafe directives in wrapper class?

The C# application is as below

[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);

static MCvMat image;
static MCvMat x_map;
static MCvMat y_map;

static System.Drawing.Point im_point; //equivalent to CvPoint
static System.Drawing.Point pt1;
static System.Drawing.Point pt2;

static System.Drawing.Point re_pt;

static int count = 0;
const double M_PI = Math.PI;

//global variables
static double h = 1.475;
static double theta = 45.0 * M_PI / 180.0;


[DllImport(@"C:\Users\tumelo\Documents\Visual Studio 2012\Projects\Wrapper\Estimation_Test1\Estimation\Debug\Estimation.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void CalcteMatrices(MCvMat x, MCvMat y, System.Drawing.Size size, string c_file, double cam_height, double cam_width);



static void Main(string[] args)
{

string calib_file = (".\\calib_video2.xml");
IntPtr pIntr = CvInvoke.cvLoadImage(".\\20150115_123318.jpg",LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);

try
{
System.Drawing.Size a = CvInvoke.cvGetSize(pIntr);

CalcteMatrices(x_map, y_map, a, calib_file, h, theta);
DeleteObject(pIntr);
}
catch (Exception ex)
{
}
///Image<Bgr, Byte> image = new Image<Bgr, Byte>(@"C:\VSP\PicturesDet_5aee9e19-4f2d-4700-92f4-b9a60583aaa2_Fr_670f7fb6-72c0-4693-8dd3-805b1c24d47e_20150202_125726AM_2JPG.jpg");
Console.WriteLine("****The application is running****");
Console.WriteLine("==================================");

Console.Read();
}

We have turned the web upside down without any luck. How can we read an image in C# emgucv and send it to C++ for processing?Please help









Continue reading...
 
Back
Top