【OpenCV学习】BGR->RGBA色彩空间转换

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.RGBA色彩空间基本知识: RGBA stands for R ed G reen B lue A lpha.

作者: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 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
2月前
|
计算机视觉
OpenCV BGR
【10月更文挑战第09天】
36 1
|
2月前
|
计算机视觉 Python
Opencv学习笔记(七):如何根据opencv将BGR转换为HSV
使用OpenCV库在Python中将BGR图像转换为HSV颜色空间的两种方法:一种是直接使用cv2.cvtColor函数,另一种是手动实现RGB到HSV的转换。
44 0
Opencv学习笔记(七):如何根据opencv将BGR转换为HSV
|
4月前
|
计算机视觉 C++
基于VS2019和Opencv4,对hsv颜色空间的图像分割原理以及实现
这篇文章介绍了基于HSV颜色空间的图像分割原理,包括HSV模型的基本概念和如何在OpenCV中通过设置HSV的色彩范围来实现图像中特定颜色的物体分割,并通过示例代码展示了在静态图像和视频流中进行颜色分割的方法。
基于VS2019和Opencv4,对hsv颜色空间的图像分割原理以及实现
|
6月前
|
机器学习/深度学习 人工智能 自然语言处理
OpenCV与AI深度学习之常用AI名词解释学习
AGI:Artificial General Intelligence (通用人工智能):是指具备与人类同等或超越人类的智能,能够表现出正常人类所具有的所有智能行为。又被称为强人工智能。
137 2
|
6月前
|
计算机视觉
OpenCV图像色彩空间转换
OpenCV图像色彩空间转换
|
5月前
|
计算机视觉 Python
opencv 处理图像去噪的几种方法学习
OpenCV 提供了多种图像去噪的方法,以下是一些常见的去噪技术以及相应的 Python 代码示例: 均值滤波:使用像素邻域的灰度均值代替该像素的值。
70 0
|
6月前
|
机器学习/深度学习 开发框架 TensorFlow
### 如何系统化学习OpenCV4
### 如何系统化学习OpenCV4
43 0
|
7月前
|
算法 计算机视觉 Python
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
216 2
|
7月前
|
存储 计算机视觉
OpenCV—学习基本绘图
OpenCV—学习基本绘图
|
7月前
|
算法 C++ 计算机视觉
Opencv(C++)学习系列---Laplacian拉普拉斯边缘检测算法
Opencv(C++)学习系列---Laplacian拉普拉斯边缘检测算法
321 0