OpenCV从B / W转换为RGB

时间:2012-05-18 20:29:27

标签: c++ image-processing opencv

我已经将图像从RGB转换为B / W然后我想将其转换回RGB但我有一个问题:

我的代码:

             int width=zoomedImage->width;
             int height=zoomedImage->height;
             TempImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
             cvCvtColor(zoomedImage, TempImage,CV_RGB2GRAY);
             cvThreshold( TempImage, TempImage,128,256,CV_THRESH_BINARY);
             cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
             this->pictureBox1->Image=(gcnew 
System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep, 
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData)); 

这里我将zoomedImage显示为B / W图像,在另一个动作中我想将zoomedImage显示为RGB图像这里的主要问题是我无法更改将作为我的另一部分绘制的图像代码依赖于这个序列,我在另一个动作中写道:

  cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
this->pictureBox1->Image=(gcnew 
System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep, 
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData));

但是zoomedImage仍然显示为B / W,我听说当真彩色图像转换为灰色时,它不能再作为真彩色图像返回,那么CV_GRAY2RGB会做什么?

2 个答案:

答案 0 :(得分:5)

将RGB图像转换为灰度图像时,颜色信息会丢失,并且无法再从灰度图像中恢复此信息。

当您尝试将黑白图像转换为RGB时,您只能制作3通道图像,但所有通道都包含相同的强度数据。因此,您将获得具有3个通道的灰度图像。没什么。

答案 1 :(得分:0)

我已经解决了我的问题如下:

将原始图像转换为B / W

             int width=zoomedImage->width;
             int height=zoomedImage->height;
             ColorSaver=cvCreateImage(cvSize(width,height),zoomedImage->depth,zoomedImage->nChannels);
             ColorSaver=cvCloneImage(zoomedImage);
             TempImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
             cvCvtColor(zoomedImage, TempImage,CV_RGB2GRAY);
             cvThreshold( TempImage, TempImage,128,256,CV_THRESH_BINARY);
             cvCvtColor( TempImage,zoomedImage,CV_GRAY2RGB);
             this->pictureBox1->Image=(gcnew 

系统::绘图::位图(zoomedImage->宽度,zoomedImage->高度,zoomedImage-> widthStep, 系统::绘图::成像::的PixelFormat :: Format24bppRgb,(系统:: IntPtr的)zoomedImage->的imageData));

将原始图像返回RGB:

zoomedImage=cvCloneImage(ColorSaver);
this->pictureBox1->Image=(gcnew 
System::Drawing::Bitmap( zoomedImage->width, zoomedImage->height, zoomedImage->widthStep, 
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) zoomedImage->imageData));