I am trying to do something that I thought was simple but am having a hard time doing it. I am using OpenCV 2.4.6 on a Windows 8 64 bit machine using VS2012 c++/cli. I just want to take a CV_8UC3 color image, convert it to grayscale and display it.
The first problem is that cvtColor is not outputting what I would expect. Based on the documentation, where OriginalMat is a CV_8UC3 image, both of the following should output a CV_8UC3 image:
cvtColor(OriginalMat,NewMat,CV_RGB2GRAY);
cvtColor(originalMat,NewMat,CV_RGB2GRAY,3);
Instead, both output a CV_8UC1 image.
Putting that aside, how can I display the output in a WinForms picturebox? I am using the following code which displays an image that is correct positionally but which is not grayscale and instead has weird colors:
System::Drawing::Graphics^ graphics = PBox->CreateGraphics();
System::IntPtr ptr(NewMat.ptr());
System::Drawing::Bitmap^ b;
b = gcnew System::Drawing::Bitmap(NewMat.cols,NewMat.rows,NewMat.step,System::Drawing::Imaging::PixelFormat::Format8bppIndexed,ptr);
System::Drawing::RectangleF rect(0,0,(float)PBox->Width,(float)PBox->Height);
graphics->DrawImage(b,rect);
I have tried a number of different approaches with no success. Any help appreciated. Thanks.