EmguCV - 从非托管数据创建图像 [英] EmguCV - Create image from unmanaged data

查看:42
本文介绍了EmguCV - 从非托管数据创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 EmguCV,我有一个大小为 640x480 的字节类型灰度矩阵,矩阵的每个元素都是 255 或 0.现在我需要从中制作一个图像.我找到的最好的构造函数是这样的:

I'm using EmguCV and i've got a byte type Grayscale Matrix with the size of 640x480 and Every element of the matrix is either 255 or 0. Now I need to make an Image from this. The best constructor I found looked like this:

Image<Gray,byte>(int width, int height, int stride, IntPtr scan0)

我从矩阵中获得了第一个、第二个和第四个参数,并为步幅发送了 640.但它给了我一个

I got the first, second and fourth parameters from the matrix and I sent 640 for the stride. But it gives me an

Access violation exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

我将不胜感激.

推荐答案

您很可能传递了错误的指针作为指针.错误的步幅只会导致图像内容失真.在您的情况下,640 将是正确的值(只需再次使用宽度),因为步幅始终表示内存中一个图像行的长度(在您的情况下为 640 字节).我只能猜测但不要尝试使用矩阵的 _ptr 指针.这听起来可能合乎逻辑,但它会指向整个矩阵对象,而不仅仅是数据(您想要的).现在不检查,我很确定您可以使用矩阵的 copyTo() 将内容复制到图像(如果这足以满足您的用例).

You most likely passed something wrong as the pointer. A wrong stride would just result in the image contents appearing distorted. 640 would be the correct value in your case (just use width again) as the stride always represents the length of one image line in memory (in your case 640 bytes). I can just guess but don't try to use the matrix's _ptr pointer. This might sound logical but that one will point to the whole matrix object and not just the data (you want). Without checking right now, I'm rather sure you're able to use the matrix's copyTo() to copy the contents to an image (if that's sufficient for your use case).

这篇关于EmguCV - 从非托管数据创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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