Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Displaying Grayscale in PictureBox/PixelFormat

I am using OpenCV 2.4.6 on a Windows 8 64 bit machine with VS2012 C++/cli and trying to make a WinForms application that displays frames captured from a webcam in a picturebox. So far, this has been working for color images but when I try and display a image converted to grayscale, the displayed image seems positionally correct but kaleidoscopic color wise rather than grayscale.

void DrawCVImageDetect(System::Windows::Forms::PictureBox^ PBox, cv::Mat& colorImage)
{
    System::Drawing::Graphics^ graphics = PBox->CreateGraphics();
    System::IntPtr ptr(colorImage.ptr());
    System::Drawing::Bitmap^ b;
    switch(colorImage.type())
    {
        case CV_8UC3: // non-grayscale images are correctly displayed here
            b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
            break;
        case CV_8UC1: // grayscale images are incorrectly displayed here 
            b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format8bppIndexed,ptr);
            break;
        default:
            // error message
            break;
    }
    System::Drawing::RectangleF rect(0,0,(float)PBox->Width,(float)PBox->Height);
    graphics->DrawImage(b,rect);
}

When I call this with a regular Mat captured from the webcam, it works fine. When I convert that same Mat to a grayscale, I get the weird colors. I am converting to grayscale using cvtColor(OriginalMat,OriginalMat,RGB2GRAY). The output from this does not appear to be the same channel type as the input (i.e., a CV_8UC3 going in appears to come out as a CV_8UC1). I have also forced the output to 3 channels using cvtColor(OriginalMat,OriginalMat,RGB2GRAY,3). The fact that just the colors are off makes me think that there is something with the color indexing/premultiplication but I have tried many of the the different pixelformat types and nothing seems to work. Thanks in advance for any help.

Displaying Grayscale in PictureBox/PixelFormat

I am using OpenCV 2.4.6 on a Windows 8 64 bit machine with VS2012 C++/cli and trying to make a WinForms application that displays frames captured from a webcam in a picturebox. So far, this has been working for color images but when I try and display a image converted to grayscale, the displayed image seems positionally correct but kaleidoscopic color wise rather than grayscale.

void DrawCVImageDetect(System::Windows::Forms::PictureBox^ PBox, cv::Mat& colorImage)
{
    System::Drawing::Graphics^ graphics = PBox->CreateGraphics();
    System::IntPtr ptr(colorImage.ptr());
    System::Drawing::Bitmap^ b;
    switch(colorImage.type())
    {
        case CV_8UC3: // non-grayscale images are correctly displayed here
            b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,
                System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
            break;
        case CV_8UC1: // grayscale images are incorrectly displayed here 
            b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format8bppIndexed,ptr);
System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,
                System::Drawing::Imaging::PixelFormat::Format8bppIndexed,ptr);
            break;
        default:
            // error message
            break;
    }
    System::Drawing::RectangleF rect(0,0,(float)PBox->Width,(float)PBox->Height);
    graphics->DrawImage(b,rect);
}

When I call this with a regular Mat captured from the webcam, it works fine. When I convert that same Mat to a grayscale, I get the weird colors. I am converting to grayscale using cvtColor(OriginalMat,OriginalMat,RGB2GRAY). The output from this does not appear to be the same channel type as the input (i.e., a CV_8UC3 going in appears to come out as a CV_8UC1). I have also forced the output to 3 channels using cvtColor(OriginalMat,OriginalMat,RGB2GRAY,3). The fact that just the colors are off makes me think that there is something with the color indexing/premultiplication but I have tried many of the the different pixelformat types and nothing seems to work. Thanks in advance for any help.