mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
7 years ago
|
#include <opencv2/opencv.hpp>
|
||
|
|
||
|
using namespace cv;
|
||
|
using namespace std;
|
||
|
|
||
|
static void createAlphaMat(Mat &mat)
|
||
|
{
|
||
|
CV_Assert(mat.channels() == 4);
|
||
|
for (int i = 0; i < mat.rows; ++i)
|
||
|
{
|
||
|
for (int j = 0; j < mat.cols; ++j)
|
||
|
{
|
||
|
Vec4b& bgra = mat.at<Vec4b>(i, j);
|
||
|
bgra[0] = UCHAR_MAX; // Blue
|
||
|
bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
|
||
|
bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
|
||
|
bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// Create mat with alpha channel
|
||
|
Mat mat(480, 640, CV_8UC4);
|
||
|
createAlphaMat(mat);
|
||
|
vector<int> compression_params;
|
||
|
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
|
||
|
compression_params.push_back(9);
|
||
|
|
||
|
bool result = false;
|
||
|
try
|
||
|
{
|
||
|
result = imwrite("alpha.png", mat, compression_params);
|
||
|
}
|
||
|
catch (const cv::Exception& ex)
|
||
|
{
|
||
|
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
|
||
|
}
|
||
|
if (result)
|
||
|
printf("Saved PNG file with alpha data.\n");
|
||
|
else
|
||
|
printf("ERROR: Can't save PNG file.\n");
|
||
|
return result ? 0 : 1;
|
||
|
}
|