Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Error on cvtColor/Display PixelFormat/PictureBox

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.

click to hide/show revision 2
pulled out description of a display problem I separately fixed

Error on cvtColor/Display PixelFormat/PictureBox

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 Perhaps the documentation should be changed to reflect the fact that aside, how can I display when working with grayscale, 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 will always be single channel 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.any hard coded channel count will be ignored.

Error on cvtColor/Display PixelFormat/PictureBoxcvtColor output unexpected channel count

I am using OpenCV 2.4.6 on a Windows 8 64 bit machine using VS2012 c++/cli. I want to take a CV_8UC3 color image, convert it to grayscale and display it.

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.

Perhaps the documentation should be changed to reflect the fact that when working with grayscale, the output will always be single channel and any hard coded channel count will be ignored.