Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 4 channel image to 3 Channel image

Tags:

opencv

I am using OpenCV 2.4.6. I am trying to convert a 4channel RGB IplImage to 4channel HSV image. Below is my code. Which is giving error "OpenCV Error: Assertion failed in unknown function". I think cvCvtColor supports 3channel images. Is there any way of converting 4channel RGB to HSV or 4channel RGB to 3channel RGB?

 IplImage*  mCVImageColor = cvCreateImageHeader(cvSize(640,480), IPL_DEPTH_8U, 4);
 /*Doing something*/
 IplImage* imgHSV = cvCreateImage(cvGetSize(mCVImageColor), IPL_DEPTH_8U, 4);
 cvCvtColor(mCVImageColor, imgHSV, CV_BGR2HSV); //This line throws exception
like image 941
Tonmoy Avatar asked Sep 26 '13 12:09

Tonmoy


People also ask

Why do some images have 4 channels?

With four channels, it is often an alpha channel that specifies the opaque level of that pixel.

How do I remove the 4th channel from a image in Python?

imdecode(arr,-1) # 'load it as it is' s = image. shape #check if third tuple of s is 4 #if it is 4 then remove the 4th channel and return the image.

Can an image have 4 channels?

A CMYK image has four channels: cyan, magenta, yellow, and key (black). CMYK is the standard for print, where subtractive coloring is used. A 32-bit CMYK image (the industry standard as of 2005) is made of four 8-bit channels, one for cyan, one for magenta, one for yellow, and one for key color (typically is black).

What is number of channels in image?

How many numbers we have per pixel is the number of channels that image has. A monochrome image that has one number per pixel has one channel. A more typical image that has three (R, G, B) numbers per pixel has three channels. Such images are called RGB images.


1 Answers

The common assumption is that the 4th channel is an alpha (A) channel. Thus, the correct conversion code is:

cvCvtColor(mCVImageColor, imgHSV, CV_BGRA2HSV);

Notice the A in BGRA.

Also, I guess from your syntax (mCVImage...) that you are using C++. Then, why not using the C++ API of OpenCV? If you choose to go C++, the documentation is still outdated, and you can find up-to-date color conversion codes for OpenCV 2.4.6 here.

For your case, the correct color conversion code (C++) is: cv::COLOR_BGRA2HSV. But if you are using C++ API, then you should use cv::Mat objects and call the funciton cv::cvtColor(...) instead of using IplaImage's and cv prefixed functions.

like image 65
sansuiso Avatar answered Sep 18 '22 12:09

sansuiso