OpenCV如何实现透明(alpha channel)图像的读取和写入

最近在做一个曲线匹配的东西,用的是OpenCV,需要根据一定的轨迹将原图上的某一块区域切割下来,需要空白的地方透明。

比如:

切割下来的图片

在OpenCV里,正常是读取图像和写入图像默认都是忽略透明通道的,官网上只是简单的提了一下,所以在这里翻译一下官网的内容,并说一下自己的使用体验。

读取透明图像

官网链接-Load and Display an Image
原文:
Now we call the imread function which loads the image name specified by the first argument (argv[1]). The second argument specifies the format in what we want the image. This may be:

  • CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
  • CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one
  • CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format

解读
如果想要读取原图像中的透明通道,则在使用imread()函数时,后面的参数要使用CV_LOAD_IMAGE_UNCHANGED参数。比如:

Mat inimg = imread("demo_after_pre_.bmp", CV_LOAD_IMAGE_UNCHANGED); // 读取透明通道
// 输出RGBA数值
cout << (int)inimg.at<Vec4b>(0,0)[0] << endl
     << (int)inimg.at<Vec4b>(0,0)[1] << endl
     << (int)inimg.at<Vec4b>(0,0)[2] << endl
     << (int)inimg.at<Vec4b>(0,0)[3] << endl;

在这里需要注意的时,使用PS生成的局部透明图像,只有透明的部分有第四通道,正常图像部分是没有第四通道的。

写入透明图像

原文-Reading and Writing Images and Video
It is possible to store PNG images with an alpha channel using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535. The sample below shows how to create such a BGRA image and store to PNG file. It also demonstrates how to set custom compression parameters

#include <vector>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void createAlphaMat(Mat &mat)
{
    for (int i = 0; i < mat.rows; ++i) {
        for (int j = 0; j < mat.cols; ++j) {
            Vec4b& rgba = mat.at<Vec4b>(i, j);
            rgba[0] = UCHAR_MAX;
            rgba[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX);
            rgba[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX);
            rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
        }
    }
}

int main(int argv, char **argc)
{
    // Create mat with alpha channel
    Mat mat(480, 640, CV_8UC4);
    createAlphaMat(mat);

    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);

    try {
        imwrite("alpha.png", mat, compression_params);
    }
    catch (runtime_error& ex) {
        fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
        return 1;
    }

    fprintf(stdout, "Saved PNG file with alpha data.\n");
    return 0;
}

解读
1. 首先要申请图像时要申请CV_8UC4type的mat。
2. 然后按照正常RGB值给前三个通道赋值。第四(透明)通道是值的范围是0-255(因为是uchar类型的),0代表透明,255代表不透明。
3. 最后在保存图像的时候需要以特定type保存

vector<int> compression_params;   compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
imwrite("alpha.png", mat, compression_params);

这样我们得到的就是带有透明的图像了。

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值