Conversion from RGB to other color spaces
The color of an image may also be modified by changing the color space. In OpenCV, six color models are available and it is possible to convert from one to another by using the cvtColor
function.
Note
The default color format in OpenCV is often referred to as RGB but it is actually BGR (the channels are reversed).
The function void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
has the input and output images as the first and second parameters. The third parameter is the color space conversion code and the last parameter is the number of channels in the output image; if this parameter is 0, the number of channels is obtained automatically from the input image.
The following color_channels
example shows how to convert from RGB to HSV, Luv, Lab, YCrCb, and XYZ color spaces:
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv; using namespace std; int main( ){ Mat image, HSV, Luv, Lab,...