How to properly convert Mat to Bitmap C++
Hello,
I'm a student and I'm learning OpenCV (C++) and I'm having a weird question. I'm using OpenCV in C++/CLI and a C# Windows Form to show the output.
This is my plan on how to do this:
- Load Mat Image in C++ 2.
- Convert Mat to Bitmap
- Return the Bitmap from C++ to C# 4.
- Show the Bitmap on Windows Form.
Now I thought this was a good strategy but I got an error saying: 'Access Violation Exception'. So I tried to look for an answer but couldn't find it on the Web. I have all my projects in x64. The weird thing is that if I put 'imshow()' function in this method, I get a weird looking picture, but no errors.
This is the .cpp file:
Bitmap^ OpenCvWrapper::ApplyFilter() {
Mat image = imread("C:/Users/Andries/Pictures/colored_squares.png");
returnImg = ConvertMatToBitmap(image)
return returnImg;
}
Bitmap^ OpenCvWrapper::ConvertMatToBitmap(cv::Mat matToConvert) {
imshow("Window", matToConvert);
Bitmap^ test = gcnew Bitmap(matToConvert.rows, matToConvert.cols, 4 * matToConvert.rows, System::Drawing::Imaging::PixelFormat::Format4bppIndexed, IntPtr(matToConvert.data));
return test;
}
And this is my .cs file:
public partial class Form1 : Form
{
private Bitmap f;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpencvDotNet.OpenCvWrapper obj = new OpencvDotNet.OpenCvWrapper();
f = obj.ApplyFilter();
pictureBox1.Image = f;
}
I can provide the header but I think that's not necessary as of now.
Now with this code I get the following output:
Please help.
Andries
Please read the FAQ!
Anyway, as a personal note, I don't recommend using C# with OpenCV...
Hi, thanks for replying but I'm not using any of these openCV wrappers. I'm just using C# as a GUI. Also, I don't use any extension libraries.
if you load an image using imread() (with no flags), it will be 24bit bgr.
so, your stride looks broken, and for sure it's not Format4bppIndexed (which is only a thing with bmp images)
(last, my 2 ct. : don't waste too much time on gui things. noone will see it anyway, your program won't work any better, you're only adding distraction to your code)
Hi berak, thanks for the comment. So if my stride is broken; how can I fix that? I thought 4 * image.rows is enough. I already tried every singe PixelFormat there was but I still get the same picture.
Well it's kind of a must to create a gui. People have to work with this gui.
stride is probably 3 x img.cols (if there is no padding required, idk)
and you need some pixelformat for 24bit
I already have my PixelFormat to 24 bit. I tried 3 * img.cols but that doesnt work. Now I tried saving my Bitmap right after I converted it from Mat to Bitmap and I received the following output: Image Please help.
Check the
Stride
property of theBitmapData
object. The lines in C# are aligned on a certain number of pixels, so the first pixel of lineN
isn't at positionN*bmpData.Width
butN*bmpData.Stride
.@kbarni I don't get that actually. I checked the
Stride
in the C++ convertedBitmap
object in the C# code and it says: 1024. But when I load theBitmap
image directly in C# it gives me aStride
of 1720. So I manually put the 1720 in the C++ conversion code, but I still get a weird image. I don't why.