我需要将位图传递给使用opencv在C++中创建的dll。在dll中,我使用Mat对象来处理图像。我想知道如何将Bitmap对象更改为Mat对象。我尝试使用IntPtr,但我不知道如何构建Mat对象,因为Mat构造函数不支持IntPtr。有谁知道我该怎么做?如果你能用一段代码来帮助我,那将是最好的。谢谢。如何将位图对象转换为Mat对象(opencv)?
0
A
回答
0
一个简单的方法来做到这一点是:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace System;
using namespace System::Drawing;
int main(array<System::String ^> ^args) {
Bitmap^ img = gcnew Bitmap(10, 10, System::Drawing::Imaging::PixelFormat::Format24bppRgb);
// or: Bitmap^ img = gcnew Bitmap("input_image_file_name");
System::Drawing::Rectangle blank = System::Drawing::Rectangle(0, 0, img->Width, img->Height);
System::Drawing::Imaging::BitmapData^ bmpdata = img->LockBits(blank, System::Drawing::Imaging::ImageLockMode::ReadWrite, System::Drawing::Imaging::PixelFormat::Format24bppRgb);
cv::Mat cv_img(cv::Size(img->Width, img->Height), CV_8UC3, bmpdata->Scan0.ToPointer(), cv::Mat::AUTO_STEP);
img->UnlockBits(bmpdata);
cv::imwrite("image.png", cv_img);
return 0;
}
顺便说一句,这是值得的,你是用C++/CLI工作的问题就更不用说了。
1
谢谢你的帮助! 我找到了另一种方法来做到这一点。检查我的代码: C#:
[DllImport("addborders.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int main(IntPtr pointer, uint height,uint width);
unsafe
{
fixed (byte* p = ImageToByte(img))
{
var pct = (IntPtr) p;
x = main(pct, (uint)img.Height, (uint)img.Width);
}
textBox1.Text = x.ToString();
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
C++
extern "C"
{
__declspec(dllexport)
int main(unsigned char* image,unsigned int height,unsigned int width)
{
cv::Mat img = cv::Mat(height, width, CV_8UC1, image);
}
}
相关问题
- 1. 将OpenCV Mat对象转换为BufferedImage
- 2. 通过opencv将Mat转换为位图
- 3. 将图形对象转换为位图对象
- 4. 如何在保持颜色的同时将Mat对象转换为位图?
- 5. 如何将对象的对象转换为对象数组?
- 6. 将BufferedImage转换为OpenCv Mat
- 7. 将32位对象转换为64位对象
- 8. 如何将QImage转换为opencv Mat
- 9. 尝试使用OpenCv中Mat对象的转换IplImage对象时出现错误
- 10. 将uchar Mat转换为OpenCV中的Mat?
- 11. 将jcas对象转换为json对象
- 12. 将javascript对象转换为json对象
- 13. 将对象转换为jquery对象
- 14. 将Image对象转换为BufferedImage对象
- 15. 将TS对象转换为data.frame对象
- 16. 将Image对象转换为FormFile对象
- 17. 转换对象为对象
- 18. 如何将int数组转换为位图对象?
- 19. android,如何将位图转换为文件对象
- 20. 如何包含数组对象转换为对象的对象
- 21. 如何将Python对象转换为XML?
- 22. 如何将Scripting.Dictionary转换为JavaScript对象?
- 23. 如何将对象转换为数组?
- 24. 如何将JSON转换为对象?
- 25. 如何将对象转换为int?
- 26. 如何将pd.to_timedelta()转换为time()对象?
- 27. 如何将JSON转换为对象
- 28. 如何将JSONString转换为Javascript对象
- 29. 如何将i18n.map转换为json对象
- 30. 如何将json转换为对象?
你见过[这](http://docs.opencv.org/java/2.4.9/org/opencv/android/ Utils.html) –