- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 MVC
应用程序,其中我的一个 Controller 接收上传的文件(图像)作为 HttpPostedFileBase
对象。
我正在尝试使用 EmguCV
处理图像,但是我很难将我的 HttpPostedFileBase
转换为 EmguCV
矩阵对象 Emgu.CV.Mat
(它只是 cv::Mat
对象的 C#
实现)。
Mat
有一个构造函数,如下所示:
public Mat(int rows, int cols, DepthType type, int channels, IntPtr data, int step);
但我不确定如何从开始的HttpPostedFileBase
中获取type
、data
和step
> 对象。这可能吗?
我看到 here ,我可以将 HttpPostedFileBase
转换为 Image
对象(我认为它在 System.Drawing
命名空间中),这让我可以看到高度和宽度。 但是我如何使用这些信息来获取发送 Mat()
构造函数所需的其余参数?
最佳答案
根据 Emgu 规范,这些参数意味着:
/// <param name="type">Mat element type
/// <param name="channels">Number of channels
/// <param name="data">
/// Pointer to the user data. Matrix constructors that take data and step parameters do not
/// allocate matrix data. Instead, they just initialize the matrix header that points to the
/// specified data, which means that no data is copied. This operation is very efficient and
/// can be used to process external data using OpenCV functions. The external data is not
/// automatically deallocated, so you should take care of it.
/// <param name="step">
/// Number of bytes each matrix row occupies.
/// The value should include the padding bytes at the end of each row, if any.
type
是CvEnum.DepthType
类型,也就是图像的深度,可以通过CvEnum.DepthType.Cv32F
表示 32 位深度图像,其他可能的值的形式为 CvEnum.DepthType.Cv{x}{t}
,其中 {x} 是集合 {8,16, 32,64} 和 {t} 可以是 S
表示 Single
或 F
表示 Float
。
channels
,取决于图像的类型,但我认为您可以使用 ARGB 中的 4
。
对于其他2个参数,如果你不需要优化部分,你可以只使用Mat
类的这个构造函数:
public Mat(int rows, int cols, DepthType type, int channels)
如果你真的想使用优化版本,那么(继续):
data
,你可以通过 Bitmap.GetHbitmap() 返回一个 IntPtr 给用户数据。
step
,对于这个人,我会给你一个有根据的猜测,如果每个像素有 4 个 channel ,并且每个 channel 的范围从 0 到 255(8 位),8*4 = 32
,所以对于单位的每个宽度,你需要 32 位。假设这是正确的,每一行都有 32*width
位,将其转换为字节 ((8*4)*width)/8 = 4*width
,这是 channel 数,乘以图像宽度。
更新
获取数据
和步骤
的其他方法来自BitmapData类,像这样(摘自 MSDN 资源):
Bitmap bmp = new Bitmap(Image.FromStream(httpPostedFileBase.InputStream, true, true));
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// data = scan0 is a pointer to our memory block.
IntPtr data = bmpData.Scan0;
// step = stride = amount of bytes for a single line of the image
int step = bmpData.Stride;
// So you can try to get you Mat instance like this:
Mat mat = new Mat(bmp.Height, bmp.Width, CvEnum.DepthType.Cv32F, 4, data, step);
// Unlock the bits.
bmp.UnlockBits(bmpData);
尚未测试此解决方案,但您可以试一试。我的回答是基于 Emgu 代码 here ., 位图 IntPtr here还有这个post这也帮助我对此有了进一步的了解。
我也见过其他方法,除非你真的需要调用那个完整的构造函数,否则我会尝试这种方法,看起来更干净:
HttpPostedFileBase file //your file must be available is this context.
if (file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
// your so wanted Mat!
Mat img = imread(filename, CV_LOAD_IMAGE_COLOR);
}
注意
OpenCV documentation 中有很棒的教程.只需查看核心模块的可用教程即可。特别是这个 one .
关于C#/EmguCV - 将上传的 HttpPostedFileBase 转换为 Emgu.CV.Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24050813/
我使用的是 Emgu 2(使用 opencv 2.4.10.1 的那个),它运行非常稳定,不会崩溃。我现在已经升级到 Emgu 3.1.0.1,我的应用程序有时会在几小时或一天内崩溃并出现 Acces
您好,我想做的是将 Emgu.CV.Mat 分配给 Emgu.CV.UI.ImageBox 但我一直收到 CvInvokeCore.cs 文件中的 System.AccessViolationExce
我在 C# 中使用 EmguCV,当我想从网络摄像头抓取帧时遇到问题,语句中出现红色下划线: imgOrg = capturecam.QueryFrame(); error: Cannot impli
我能够将 emgu 图像格式转换为字节字符串,这确实可以使用此代码保存在 MySQL 数据库上,但图像以 Windows 图像查看器无法识别的格式保存 string myCon
我有点碰壁我可能只是对如何在 .net(c# express 2010)中设置项目缺乏一些基本的了解,但我似乎无法让这个程序工作 基本上,我正在尝试设置一个简单的程序,该程序将获取网络摄像头馈送并检查
我正在使用 EmguCV 3.1 开发人脸识别应用程序。我正在使用 EigenFaceRecognizer 作为识别算法。我尝试使用以下代码训练图像。 List> trainingImages
我正在开发一个使用 OpenCV 库的项目,它使用 Emgu.CV.Image 作为从相机捕获的图像。 我正在尝试将代码移动到 Microsoft Azure 辅助角色中以处理云中的图像处理。 我当前
我从以下位置下载 sift 实现:https://sites.google.com/site/btabibian/projects/3d-reconstruction/code但我收到错误消息:命名空
我正在尝试使用EmguCV检测字符标记,并且已经从OpenCV网站复制并重写了example。 我的代码如下所示: Mat cameraMatrix = new Mat(new Size(3, 3),
我正在使用 Emgu CV 库用 Visual Basic 语言编写一些代码。 我需要知道如何使用函数 AdaptiveThreshold Dim inputImage As Emgu.CV.Inpu
我仍在学习 Emgu CV,我需要从包含 PNG32 数据的字节数组中加载图像。我正在按如下方式加载图像(这是工作示例): FileStream fs; Bitmap bitmap; Image im
我正在做一个项目,需要从表面的红外激光中识别点。我用的是带红外滤镜的相机 一些输入图像: 也可以有几个点。因此,我尝试从网络摄像头中锐化此图像,然后使用Emgu CV的FindContours方法。
我正在使用 emgu/opencv 来查找一些扁平 Blob 的位置。我目前可以以像素为单位找到它们的位置,并希望将其转换为世界坐标(in/mm)。我看过 emgu 的相机校准 example ,但我
我正在使用 Emgu 在 Visual Studio 上使用 C#。 我正在对一张大图像进行一些图像处理。我想到了将图像分成两半,并行进行操作,然后合并图像。 为了实现这一目标,我发现了一些关于获取图
我无法使用 Emgu CV 播放视频 显示错误 Unable to create capture from 184.avi 代码如下: public partial class Form1 : For
我正在从事一个实验项目,其中的挑战是识别和提取用户点击/触摸的图标或控件的图像。我正在尝试的方法如下(我需要一些帮助来完成第 3 步): 1) 当用户点击/触摸屏幕时截屏: 2) 应用边缘检测: 3)
我发现了一个类似的问题:creating histogram using emgu cv c# 当我传递灰度图像时效果很好,但是当我使用 Matrix 时,程序会抛出异常。我的代码: Matrix m
有人可以指导我介绍各向异性扩散的一些现有实现方式,最好是 perona-malik扩散吗? 最佳答案 翻译以下MATLAB代码: % pm2.m - Anisotropic Diffusion rou
我在项目中使用emgu。 我得到这个异常(exception): 这里是异常的细节: opencv_core242在文件夹项目中,并且平台目标设置为x86。 任何想法可能导致异常的原因以及如何解决?
今天是个好日子, 我正在尝试为视频构建实时帧插值程序。 因为我一直使用C#,所以我选择将OpenCV与Emgu 3.2.0(可用的最新版本)一起使用。 就是这样: 首先,我有2张分别名为frame1和
我是一名优秀的程序员,十分优秀!