如何通过使用c ++/cli从C#流到OpenCV Mat? [英] How to come from C# stream to OpenCV Mat by using c++/cli?

查看:116
本文介绍了如何通过使用c ++/cli从C#流到OpenCV Mat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Azure Blob存储中读取一些图像.这是c#中的流.

I am reading some image from Azure Blob storage. This is a stream in c#.

在c ++/cli中,我需要这样做:

In c++/cli I need to do this:

cv::Mat img_object = imdecode(data, IMREAD_UNCHANGED);

其中数据的类型为cv :: InputArray

where data is of type cv::InputArray

所以问题是我如何最好地从c#转到cv :: mat?

so the question is how do I best get from c# to the cv::mat?

public ref class Client
{
public:
    // TODO: Add your methods for this class here.
    void Method(Stream ^ img1, Stream ^ img2);
};

void Client::Method(...){

}

我只是放入Stream ^ img1,Stream ^ img2,那不是必须的,只是不确定从c#流到cv :: Mat类的最佳方法是什么.

I just put in Stream ^ img1, Stream ^img2, thats not a requirement, just not sure what is the best way to get from the c# stream to the cv::Mat class.

我知道我可以将流保存在临时文件位置上,而改用imread,但是仅在内存中进行操作会很好.

I know I could save the stream on a temp file location and use imread instead, but would be nice just to do it in memory.

在csharp中,我只是这样:(我知道它不是正确的方法,但是出于本示例的目的,它说明了这一点).

in csharp i just have somehting like this: (I know its not the correct methods, but for the purpose of this example it illustrate the point).

Client client = new Client();
client.Method(blob.GetStream(), blob.GetStream())

更新:先尝试

void CVService::Stiching::StichingClient::FindHomography(cli::array<unsigned char>^ pCBuf1, cli::array<unsigned char> ^ pCBuf2)
{

    pin_ptr<unsigned char> data1 = &pCBuf1[0];
    pin_ptr<unsigned char> data2 = &pCBuf2[0];  

    Mat img_object = imdecode(*data1,IMREAD_COLOR);
    Mat img_scene = imdecode(*data2, IMREAD_COLOR);

    if (!img_object.data || !img_scene.data)
    {
        return;
    }
    imwrite("c:\\test.jpg", img_object);
}

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        StichingClient client = new StichingClient();

        client.FindHomography(ReadByteArray(path1).Result
            , ReadByteArray(path2).Result);

        return base.OnStart();
    }

    private async Task<byte[]> ReadByteArray(string p)
    {
        using (var fs = File.OpenRead(p))
        {
            using (var ms = new MemoryStream())
            {
                await fs.CopyToAsync(ms);

                return ms.ToArray();
            }
        }
    }

}

问题是加载的cv :: mat的data == 0且未将映像写入磁盘.我已验证传递的char数组包含数据.有什么方法可以找出为什么解码不解码图像的原因吗?

Problem is that the loaded cv::mat is having data== 0 and not writing the image to disk. I have verified that the passed char arrays contains data. Is there any way to find out why imdecode do not decode the images?

在c ++/cli代码部分中的以下测试有效:

the following test in c++/cli code part works:

Mat test1 = imread("DSC_023.tif");
std::vector<unsigned char> buf;     
imencode(".jpg", test1, buf);
Mat test2 = imdecode(buf, IMREAD_COLOR);

在上面的4行中,test2是加载的图像,并且imdecode起作用.所以想知道为什么当数据以字节数组形式输入时它不起作用

With the above 4 lines the test2 is the loaded image and imdecode works. So wondering why it do not work when the data comes in as byte arrays

推荐答案

所以这是我的解决方案.可以清除它的原因,但现在可以使用.

So heres is my solution. It can be cleaned up ofcause but it works now.

void CVService::Stiching::StichingClient::FindHomography(cli::array<unsigned char>^ pCBuf1, cli::array<unsigned char> ^ pCBuf2)
{

    pin_ptr<unsigned char> data1 = &pCBuf1[0];
    pin_ptr<unsigned char> data2 = &pCBuf2[0];  

    pin_ptr<System::Byte> p1 = &pCBuf1[0];
    unsigned char* pby1 = p1;
    Mat img_data1(pCBuf1->Length, 1, CV_8U, pby1);

    pin_ptr<System::Byte> p2 = &pCBuf2[0];
    unsigned char* pby2 = p2;
    Mat img_data2(pCBuf2->Length, 1, CV_8U, pby2);

    cv::Mat img_object = imdecode(img_data1, IMREAD_UNCHANGED);
    cv::Mat img_scene = imdecode(img_data2, IMREAD_UNCHANGED);

    if (!img_object.data || !img_scene.data)
    {
        return;
    }
    imwrite("img_object.jpg", img_object);
    imwrite("img_scene.jpg", img_scene);
}

我已经完成了转换从内存中的Azure Blob存储加载的两个图像的工作,现在可以对图像执行opencv的工作了.这里的测试只是将两个传递的图像写入文件系统.

I got this working with converting two images loaded from Azure Blob storage in memory and can now do my opencv stuff on the images. The test here just write the two passed images to the file system.

这篇关于如何通过使用c ++/cli从C#流到OpenCV Mat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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