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
With four channels, it is often an alpha channel that specifies the opaque level of that pixel.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With