Eslam Hamdy
Eslam Hamdy

Reputation: 7396

OpenCV convert from B/W to RGB

i have converted an image from RGB to B/W then i want to convert it back to RGB but i have a problem on that:

my code:

             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)); 

here i'm displaying zoomedImage as a B/W image,in another action i want to display zoomedImage as an RGB image the major problem here is that i can't change the image that will be draw as another parts of my code is depending on this sequence, i wrote that in the other action:

  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));

but zoomedImage still dispalyed as B/W, i heared that when a true color image is converted to gray it can't be returned again as a true color image, so what does CV_GRAY2RGB do?

Upvotes: 0

Views: 2438

Answers (2)

Eslam Hamdy
Eslam Hamdy

Reputation: 7396

i have solved my problem as following:

Convert Original image to 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 

System::Drawing::Bitmap(zoomedImage->width,zoomedImage->height,zoomedImage->widthStep, System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)zoomedImage->imageData));

return back the original image to 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));

Upvotes: 0

Hakan Serce
Hakan Serce

Reputation: 11266

When you convert an RGB image to a gray level image, color information is lost, and this information cannot be recovered fom the gray level image again.

When you try to convert B/W image to RGB you only make a 3 channel image, but all channels contain the same intensity data. Hence you get a gray level image with 3 channels. Nothing more.

Upvotes: 5

Related Questions