C# 中的Bitmap 和(c++)opencv之间的传递
@[toc]
1. C#传递bitmap给C++
- C++:bitmapTest.cpp 文件代码如下,需要编译成动态库bitmapTest.dll给C#调用
#include <iostream>
#include "opencv.hpp"
#define myExport extern "C" __declspec(dllexport)
void ShowImage(const cv::Mat &image, const std::string name, int waitKey=0)
{
if (image.empty())
return;
cv::namedWindow(name, 0);
cv::imshow(name, image);
cv::waitKey(waitKey);
}
myExport void APIGetBitmapFromCSharp(uchar * data, int width, int height, int stride)
{
//采用下面的方式初始化一个cv Mat对象后,对这个对象的修改也就是对C#中的bitmap的修改
//因为它们使用的数据的内存地址都是一样的
cv::Mat img = cv::Mat(cv::Size(width, height), CV_8UC3, data, stride);
//or
//cv::Mat img(cv::Size(width, height), CV_8UC3, data, stride);
//如果在这里转换颜色那么在C#中的图片的颜色也会被转换,如果不想改变原来的图就clone一个新图,在新图上处理
//cv::cvtColor(img, img, cv::COLOR_BGR2HSV);
ShowImage(img, "image");
}
-
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace CSharpBitmapAndCPPOpencv { class Program { [DllImport("bitmapTest.dll", CallingConvention = CallingConvention.Cdecl)] extern static void APIGetBitmapFromCSharp(IntPtr data, int width, int height, int stride); static public void SendBitmapToCPP() { Bitmap img = new Bitmap("K:\\trash\\ROI.bmp"); BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int width = imgData.Width; int height = imgData.Height; int stride = imgData.Stride; try { APIGetBitmapFromCSharp(imgData.Scan0, width, height, stride); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } img.UnlockBits(imgData); //img.Save("k:\\trash\\ROI_.bmp"); } static void Main(string[] args) { SendBitmapToCPP(); } } }
2. PixelFormat和opencv Mat类的对应关系
PixelFormat.Format24bppRgb时对应的opencv是CV_8UC3,颜色通道分别是B,G,R(虽然它标示的是24bppRgb但是在opencv里面用cv::split出来看就知道了它是B,G,R的)
PixelFormat.Format32bppPArgb对应CV_8UC4,
-
Format8bppIndexed对应CV_8UC1
一般只要opencv的Mat类的stride和C#中bitmap图的stride对应上就可以正确解析。
附注
C#中保存的Format8bppIndexed格式的图片并不是像opencv中的CV_8UC1一样是纯的单通道图,而一种类似伪彩图的模式。测试如下,在C#中创建一张全0的8bits的图,在C++中把中间部分填充为255,分别在C++和C#中保存图片
-
C#
static public void test8BitsImage() { int w = 100; int h = 100; int stride = w * 1; byte[] imgdata = new byte[stride * h]; IntPtr d = Marshal.AllocHGlobal(imgdata.Length); Marshal.Copy(imgdata, 0, d, imgdata.Length); Bitmap img_8bits = new Bitmap(w,h,stride,PixelFormat.Format8bppIndexed, d); Rectangle rect = new Rectangle(0, 0, img_8bits.Width, img_8bits.Height); BitmapData imgData = img_8bits.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); try { APIGetBack8BitsImage(imgData.Scan0, w, h, stride); } catch(Exception ex) { Console.WriteLine(ex.ToString()); } img_8bits.Save("k:\\trash\\gray.bmp"); }
-
C++
myExport void APIGetBack8BitsImage(uchar * data, int width, int height, int stride) { cv::Mat img = cv::Mat(cv::Size(width, height), CV_8UC1, data, stride); uchar * p = NULL; for (int row = img.rows / 4; row <3* img.rows/4; row++) { p = img.ptr<uchar>(row); for (int col = img.cols / 4; col < 3*img.cols/4; col++) *(p + col) = 255; } cv::imwrite("k:\\trash\\gray_opencv.bmp",img); }
gray.bmp是C#保存的图,gray_opencv.bmp是C++中用opencv保存的图
上面两个图存储时大小不一样,用imageJ打开也可以看到在C++中用opencv保存的是8bits的图,在C#中保存的就是一张伪彩图。
在使用上如果不需要保存的话不管什么格式的图片,只要C#中bitmap图的锁存格式和opencv的格式对上:24bpp-->CV_8UC3,32bpp-->CV_8UC4,8Indexed-->CV_8UC1,stride保持一致就不会有什么问题。