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);

這樣我們得到的就是帶有透明的圖像了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章