In machine vision, it is often necessary to separate a multi-channel image into several single-channel images or to synthesize several single-channel images into a multi-channel image for easy image processing, however. This blog is to deepen the understanding of these two concepts, the following will give some of the OPENCV to the single channel and multi-channel image conversion between the program code, and the results of the operation of observation and analysis.
OpenCV is commonly used in iplimage or Cvmat to store image matrices, while initialization functions for these two objects cvcreateimage (cvsize size, int depth, int channels) and cvcreatemat (int rows, int cols, int type), the last parameter channels (the number of channels can be 1, 2, 3, 4) and type contains the number of selection channels, generally used is the RGB color space, and R, G, b means three channels, is a three-channel image. In addition, the depth represents the depth, the type contains the choice of depth, the depth is selected to store the image matrix of the element data type (length), of course, the longer the length of the data (such as 8bit,16bit,32bit), the larger the range can be stored, the corresponding, the larger the pixel, the more clear the image.
The image channel in RGB color mode refers to the individual red R, Green G, blue part B. In other words, a complete image is made up of three channels of red, green and blue. They work together to produce a complete picture. Also in the HSV color middle finger is the hue h, saturation s, brightness v three channels. In addition to these two three-channel image mode, the following list also lists some of the image mode ( from Baidu Encyclopedia image mode )
Image mode:
1. Bitmap mode bitmap mode is a 1-bit depth image. It's just a black and white color. It can be generated by scanning or placing a vector line image of black, or it can be converted from grayscale or Duotone mode. Other image modes cannot be converted directly to bitmap mode. 2, Gray mode Gray mode is a 8-bit depth image mode. This is 28,28=256, which has 254 gray-level colors in all black and white to depict images in grayscale mode. Images in all modes can be converted to grayscale, and even bitmaps can be transformed into grayscale mode. Almost all of Photoshop's features support grayscale mode. 3, Duotone mode Duotone mode is not a single image mode, but a classification. It is simply a generic term for mono, duotone, three tones, and four tones. The Duotone mode has only one channel. The dual-tone mode is the same as bitmap mode, and only grayscale mode can be converted. 4, RGB mode RGB mode is the most important mode of digital image, Photoshop all the features support it, because Photoshop is based on its development. The color displayed on the display is RGB mode, the TV screen is also RGB mode, the different it is not digital but the use of flat to describe. Both scanners and digital cameras capture RGB image information. The RGB mode is the additive mode, when the values of R, G, and B reach the maximum, the tri-color synthesis becomes white. The RGB mode is a 24-bit color depth. It has a total of three channels, each with a 8-bit depth. Three channels are synthesized together to produce 16.77 million colors, which we also call "True Color". 5, CMYK mode CMYK mode is used for printing or printing mode, it is the mode of subtraction, when C, M, y three values reach the maximum, in theory should be black, but in fact, because of the relationship between pigments, is not black, but dark brown. In order to compensate for this problem, the black K was added. Because of the addition of black, CMYK a total of four channels, because of this, for the same image file, CMYK mode than the RGB mode of information is One-fourth greater. However, RGB mode has a larger color gamut than CMYK mode. Because the printing pigment cannot reproduce the RGB color in the printing process. CMY and RGB are complementary colors. C-Cyan: synthesized by G-Green and B-blue, in which there is no r-Red ingredient; M-Magenta: Synthesized by R-Red and B-blue, where there is no G-green ingredient; y-yellow: Synthesized by R-Green and G-red, which has no B-blue component; CMYK mode cannot be converted to indexed mode. Most Photoshop features do not support CMYK mode 6, Lab Mode Lab mode is a 24-bit color depth image mode with three channels. The L channel is the Luminance Channel (lightness), A and b two are color channels. It is characterized by: (1) His color gamut is the widest, and its relationship with the RGB and CMYK model is as follows: In terms of gamut range lab>rgb>cMyk (2) The image in this mode is independent of the device, and its color is not changed by different printing devices, displays and operating platforms. Since it has a bit more, when Photoshop converts RGB mode and CMYK mode to each other, it becomes the middle mode, and the color information will not be lost due to the different gamut of the above two modes. A component is from green to red over. The B component is a transition from blue to yellow. Lab mode cannot be converted to indexed mode. Most features of Photoshop do not support lab mode. 7. Indexed color mode index color mode is a 8-bit color depth mode, which can only have up to 256 colors. (1) Each image has a color table, and the color table differs depending on the image. This is of paramount importance. (2) Its information is small, but also can brake painting, so its image and animation is widely used in Web page production. (3) It can be made into transparent images, used in Web pages. When converting, only grayscale and RGB two modes, cannot be converted into indexed color mode. Only two choices are practical when converting: (with sample) when the number of image colors is greater than 256, Photoshop will customize the color table by using this option. In fact, when the image color is less than 256, use this option to make a color table with the actual color. Photoshop does not support indexed color mode at all. 8, multi-channel mode multi-channel mode is to divide the image containing the channel into a single channel. When CMYK mode is converted to multichannel mode, the resulting channel is four channels cyan, magenta, yellow, and black. When lab mode is converted to multichannel mode, three alpha channels are generated. 9, 8 bits per channel, and 16 bits per channel in grayscale, RGB, and CMYK can replace 8-bit depth with 16-bit depth per channel. Then, the number of colors per channel is increased from 256 to 65536 colors, resulting in better color detail. Currently, 16-bit/channel images cannot be printed or printed due to unsupported devices. In addition to these, as well as the HSV color is also more important, I in another blog briefly explored the HSV color, interested can refer to learning.
Click
transformation and separation of image patternsIn OpenCV, the common
cvcvtcolor (const cvarr* SRC, cvarr* dst, int code)The function converts an image of one pattern to another pattern. Where the third parameter of the function code selects the target image mode to be converted, and the Opencvchm document describes the function all possible color spaces are transformed into each other: rgb<=>xyz (cv_bgr2xyz, CV_RGB2XYZ, CV_XYZ2BGR , Cv_xyz2rgb) RGB<=>YCRCB (CV_BGR2YCRCB, CV_RGB2YCRCB, CV_YCRCB2BGR, Cv_ycrcb2rgb) RGB=>HSV (CV_BGR2HSV,CV_ RGB2HSV) Rgb=>lab (Cv_bgr2lab, Cv_rgb2lab) above the conversion formula can refer to http://www.cica.indiana.edu/cica/faq/color_spaces/ Color.spaces.html
Bayer=>rgb (CV_BAYERBG2BGR, CV_BAYERGB2BGR, CV_BAYERRG2BGR, CV_BAYERGR2BGR, Cv_bayerbg2rgb, CV_BayerRG2BGR, CV_ Bayergb2rgb, CV_BAYERGR2BGR, Cv_bayerrg2rgb, CV_BAYERBG2BGR, Cv_bayergr2rgb, CV_BAYERGB2BGR) The following lists the macros in the OpenCV library to define some of the source code, All can refer to http://artu819.i.sohu.com/blog/view/89684052.htm: (There are more than 60)
/* Constants for color conversion */#define Cv_bgr2bgra 0#define Cv_rgb2rgba cv_bgr2bgra#define cv_bgra2bgr 1//... Most omit # define cv_luv2rgb 59#define cv_hls2bgr 60#define cv_hls2rgb #define Cv_colorcvt_max 100
Cvsplit (const cvarr* SRC, cvarr* dst0,
cvarr* dst1,arr* Dst2, cvarr* dst3); The function separates multiple channels of a color space. With CVMerge (
Const cvarr* SRC0, const cvarr* src1,const cvarr* src2, const cvarr* SRC3, cvarr* DST); The function combines several single-channel images into one multichannel image.
Program DemoThe following is a simple demonstration of using these two functions to convert the color space (RGB->HSV), and then separate into a single-channel image, in the synthesis of two single-channel images of a two-channel image, you can observe the lack of a channel after the image changes. With image representation, you can understand more deeply
channel and Image modes。 Code:
#include "cv.h" #include "cxcore.h" #include "highgui.h" using namespace Std;int main (int Argc,char **argv) {if (argc>=2 {Iplimage *image,*change,*h,*s,*v,*r,*b,*g,*two,*zero;//creates an image display window Cvnamedwindow ("image", cv_window_autosize); Cvnamedwindow ("R", cv_window_autosize); Cvnamedwindow ("G", cv_window_autosize); Cvnamedwindow ("B", CV_WINDOW_ AUTOSIZE) Cvnamedwindow ("HS", Cv_window_autosize) Cvnamedwindow ("HV", cv_window_autosize); Cvnamedwindow ("VS", CV_ Window_autosize), Cvnamedwindow ("RG", Cv_window_autosize), Cvnamedwindow ("RB", cv_window_autosize); CvNamedWindow (" BG ", cv_window_autosize); Cvnamedwindow (" Zero ", cv_window_autosize); Image=cvloadimage (argv[1]);//load image// Allocate image Space Change=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,3); R=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); G=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); B=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); H=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); S=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); V=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); Two=cvcreateimage (CvSize (image-> width,image->height), ipl_depth_8u,3); Zero=cvcreateimage (Cvsize (image->width,image->height), ipl_depth_8u,1); Cvzero (Zero);//in merging two channels Yes, no third channel , instead, the channel is set to blank, that is, full 0cout<< "ipl_depth_8u =" <<IPL_DEPTH_8U<<endl;cout<< "CV_RGB2HSV =" <<CV _rgb2hsv<<endl;cout<< "Cv_hsv2rgb =" <<cv_hsv2rgb<<endl;cvcvtcolor (image,change,CV_ RGB2HSV); Convert RGB color to HSV color Cvsplit (image,r,g,b,0),//separate multichannel cvsplit (change,h,s,v,0),//separate multi-channel//display RGB Chart single channel diagram Cvshowimage ("image", image); Cvshowimage ("zero", Zero), Cvshowimage ("R", R), Cvshowimage ("G", g); Cvshowimage ("B", b); Cvmerge (r,g,zero,0, ); Merge two image space Cvshowimage ("RG", two),//Show Image Cvmerge (zero,g,b,0,two) when B channel is blank, Cvshowimage ("BG", two); Cvmerge (r,zero,b,0 , Cvshowimage ("RB", "n"); Cvmerge (h,s,zero,0,two); Cvcvtcolor (Two,change,cv_hsv2RGB),//cvshowimage is in accordance with the RGB color display image, it is to display the change effect of the HSV color through RGB colors cvshowimage ("HS", changes); Cvmerge (Zero,s,v,0,two); Cvcvtcolor (TWO,CHANGE,CV_HSV2RGB),//cvshowimage is in accordance with the RGB color display image, it is to display the change effect of the HSV color through the RGB colors cvshowimage ("VS", changes); Cvmerge (H,zero,v,0,two); Cvcvtcolor (TWO,CHANGE,CV_HSV2RGB);//cvshowimage is displayed in RGB color, Therefore, through the RGB color display to the HSV color changes effect cvshowimage ("HV", change); Cvwaitkey (0); Cvdestroyallwindows (); Cvreleaseimage (&image); Cvreleaseimage (&change);} System ("pause"); return 1;}
(Note: The Cvshowimage display image is displayed by RGB, so to display the HSV color image, you need to change it to RGB color before the display.) A three-channel image, such as an image of an RGB space, "missing" a channel that cannot be a channel that is not NULL, but the element value of the channel is 0, where the zero single-channel image space is added and emptied with Cvzero. )
Operation Effect: Original:1> single channel can be observed, three images are black and white, look no different (in fact, look closely, there are many different places)
2>rgb A channel blank This is a bit peculiar, blue channel blank image is blue, green channel blank image is red, red channel blank image is yellow, 3>HSV a channel blank is obviously v brightness is empty, the image is definitely black. Reference: Http://artu819.i.sohu.com/blog/view/89684052.htm http://blog.sina.com.cn/s/blog_520a9c1d0100b 8hr.html
Multi-channel (multichannel) single-channel (Singlechannel) image concept carding