作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
1.RGBA色彩空间基本知识:
RGBA stands for R ed G reen B lue A lpha.
While it is sometimes described as a color space, it is actually simply
a use of the RGB color model, with extra information. The color is RGB , and may belong to any RGB color space, but an integral alpha value as invented by Catmull and Smith between 1971 and 1972 enables alpha blending and alpha compositing . The inventors named alpha after the Greek letter in the classic linear interpolation formula αA + (1-α)B.
The alpha channel is normally used as an opacity channel. If a pixel has a value of 0% in its alpha channel, it is fullytransparent
(and, thus, invisible), whereas a value of 100% in the alpha channel
gives a fully opaque pixel (traditional digital images). Values between
0% and 100% make it possible for pixels to show through a background
like a glass (translucency), an effect not possible with simple binary
(transparent or opaque) transparency. It allows easy image compositing . Alpha channel values can be expressed as a percentage, integer, or real number between 0 and 1 like RGB parameters.
Sometimes this is referred as ARGB (like RGBA, but first datum is alpha), which is used, for example, in Macromedia
products terminology. For example, 0x80FFFF00 is 50%-transparent
yellow, because all parameters are expressed on a 0-to-255 scale. 0x80
is 128, approximately half 255 (alpha 50%); 0xFF is 255, the greatest
value a parameter can have (pure red); the second 0xFF is like the
previous, but for green; and 0x00 is 0 in decimal (no blue). Red,
green, and half transparency mixture are 50% transparent yellow.
PNG is an image format that uses RGBA.
说白了就是一个像素透明度的位。
2.实例:
#include <iostream> #include <cv.h> #include <highgui.h> char *filename="sample.png"; IplImage *cvLoadImageBGRA(const char *filename,int iscolor = 1); IplImage *GetMaskFromBGR(IplImage *BGRImg); //取得图像掩码 int main(int argc, char **argv) { IplImage *imgBGRA = cvLoadImageBGRA(filename,CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if(imgBGRA == NULL ) { std::cerr << "Can't Load Image/n"; return -1; } cvNamedWindow("RGBA",CV_WINDOW_AUTOSIZE); cvShowImage("RGBA",imgBGRA); cvReleaseImage( & imgBGRA); cvWaitKey(0); cvDestroyAllWindows(); return EXIT_SUCCESS; } IplImage *cvLoadImageBGRA(const char *filename,int iscolor) { //RGB IplImage *ImgBGR = cvLoadImage(filename,iscolor); if(ImgBGR == NULL) { return NULL; } //创建图像掩码 IplImage *Mask = GetMaskFromBGR(ImgBGR); if(Mask == NULL) { cvReleaseImage(&ImgBGR); return NULL; } IplImage *BGRA = cvCreateImage(cvGetSize(ImgBGR),IPL_DEPTH_8U,4); if(BGRA == NULL) { cvReleaseImage(&ImgBGR); cvReleaseImage(&Mask); return NULL; } const CvArr *InputPlane[2]={ImgBGR,Mask}; int from_to[] = { 0, 2, 1, 1, 2, 0, 3, 3 };//设定对应的BGRA cvMixChannels(InputPlane,2,(CvArr**)&BGRA,1,from_to,4); cvReleaseImage(&Mask); cvReleaseImage(&ImgBGR); return BGRA; } IplImage *GetMaskFromBGR(IplImage *BGRImg) { //灰度单通道 IplImage *gray = cvCreateImage(cvGetSize(BGRImg),IPL_DEPTH_8U,1); if(gray == NULL){return NULL;} //创建灰度图像 cvCvtColor(BGRImg, gray, CV_BGR2GRAY); //创建图像掩码 cvThreshold(gray,gray,254,255,CV_THRESH_BINARY); cvNot(gray,gray); return gray; }
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。