How to Display Image In Picturebox in VC++ from Iplimage and Mat

本文介绍如何将OpenCV的iplimage和Mat类型的数据展示到Windows Form应用中的PictureBox控件上,包括了从图像数据到PictureBox的转换过程。

Introduction

This tip/trick will be useful to OpenCV programmers, who wish to use Windows Form application under Visual C++. This tip helps programmers to use Windows function with OpenCV library. The following tip shows how to assigniplimage and mat variable to picturebox image.

Assign Iplimage to Picturebox Image

In this part, it shows how to assign iplimage to picturebox image. First of all, declare the frame pointer.Iplimage type.

Declare Iplimage variable:

IplImage* frame; 

Here. Create new Bitmap image from frame properties like widthheightwidthstep. Set image data from frameimagedata.

Process the PictureBox.

pictureBox1->Image  = gcnew System::Drawing::Bitmap
(frame->width,frame->height,frame->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
pictureBox1->Refresh(); 

Assign Mat to Picturebox Image

In this part, it shows how to assign Mat to picturebox image. First of all, declare the frameDetected variable,Mat type.

Declare Mat variable.

Mat frameDetected; 

Here. Create graphics of picturebox. Then create new Bitmap image from frameDetected properties like cols, rows, step. Then create Rectangle with and with value and width and height with picturebox height,width respectively. Then rectangle assign to previous initialized graphics object.

Process the PictureBox:

System::Drawing::Graphics^ graphics2 = pictureBox3->CreateGraphics();
System::IntPtr ptr2(frameDetected.ptr());
System::Drawing::Bitmap^ b2  = gcnew System::Drawing::Bitmap(frameDetected.cols,
frameDetected.rows,frameDetected.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr2);
System::Drawing::RectangleF rect2(0,0, pictureBox3->Width,pictureBox3->Height);
graphics2->DrawImage(b2,rect2);  

Points of Interest

  • Learn how to assign iplimage to picturebox image
  • Learn how to assign Mat to picturebox image

References

  1. http://opencv.org/
  2. http://en.wikipedia.org/wiki/OpenCV
  3. http://docs.opencv.org/


转自:http://www.codeproject.com/Tips/741107/How-to-Display-Image-In-Picturebox-in-VCplusplus-f

你遇到的错误是: ``` Mat mat = pictureBox2.Image; ``` 这行代码在 C# 中是**无法直接编译通过**的,因为 `pictureBox2.Image` 是一个 `System.Drawing.Image` 类型,而 OpenCvSharp 的 `Mat` 是一个封装了 OpenCV 图像数据的类,两者之间**不能直接赋值**。 --- ## ✅ 正确做法:将 `PictureBox.Image` 转换为 `Mat` 你需要将 `Image` 转换成 `Mat`,通常可以通过以下方式实现: ### ✅ 方法 1:使用 `Cv2.ImDecode`(推荐,适用于所有图像来源) ```csharp using System.Drawing; using System.Drawing.Imaging; using OpenCvSharp; public Mat ImageToMat(Image image) { using (var ms = new MemoryStream()) { image.Save(ms, ImageFormat.Png); // 或者 ImageFormat.Bmp/Jpeg 等 var data = ms.ToArray(); return Cv2.ImDecode(data, ImreadModes.Color); } } ``` ### ✅ 使用方式: ```csharp Mat mat = ImageToMat(pictureBox2.Image); ``` --- ### ✅ 方法 2:使用 `BitmapConverter`(适用于 `Bitmap` 类型) 如果你确定 `pictureBox2.Image` 是 `Bitmap` 类型,可以直接转换: ```csharp public Mat BitmapToMat(Bitmap bitmap) { using (var ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); var data = ms.ToArray(); return Cv2.ImDecode(data, ImreadModes.Color); } } ``` ### ✅ 使用方式: ```csharp if (pictureBox2.Image is Bitmap bitmap) { Mat mat = BitmapToMat(bitmap); } ``` --- ## ✅ 方法 3:使用 `BitmapConverter`(OpenCvSharp 自带扩展方法) OpenCvSharp 提供了一个 `BitmapConverter`,可以使用 `ToMat()` 扩展方法(需要引用 `OpenCvSharp.Extensions`): ```csharp using OpenCvSharp.Extensions; Mat mat = pictureBox2.Image.ToMat(); ``` > ⚠️ 注意: > - 需要添加引用 `OpenCvSharp.Extensions.dll` > - `Image.ToMat()` 是 `Bitmap` 的扩展方法,所以 `Image` 必须是 `Bitmap` 类型 --- ## ✅ 补充:将 `Mat` 再转回 `Image` 显示在 `PictureBox` ```csharp using OpenCvSharp.Extensions; pictureBox2.Image = mat.ToBitmap(); ``` --- ## ✅ 完整示例代码 ```csharp using System.Drawing; using System.IO; using OpenCvSharp; using OpenCvSharp.Extensions; private void button1_Click(object sender, System.EventArgs e) { // 假设 pictureBox2 已加载图像 if (pictureBox2.Image != null) { // 转为 Mat Mat mat = pictureBox2.Image.ToMat(); // 示例操作:灰度化 Mat grayMat = new Mat(); Cv2.CvtColor(mat, grayMat, ColorConversionCodes.BGR2GRAY); // 转回 Bitmap 显示 pictureBox2.Image = grayMat.ToBitmap(); } } ``` --- ## ✅ 总结 | 操作 | 方法 | |------|------| | ImageMat | `Image.ToMat()`(需 `OpenCvSharp.Extensions`) | | Bitmap 转 Mat | `Bitmap.ToMat()` | | MatImage | `Mat.ToBitmap()` | | 处理大图像 | 缩放、分块、使用 64 位程序 | --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值