#include "widget.h"
#include <QApplication>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <QDebug>
using namespace cv;
void CreateAlphaMat(cv::Mat& mat)
{
for(int i=0; i < mat.rows; i++)
{
for (int j=0; j < mat.cols; j++)
{
cv::Vec4b& rgba = mat.at<cv::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.cols)-i) / ((float) mat.rows) * UCHAR_MAX);
//rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
rgba[3] = 0;
}
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Widget w;
//w.show();
//this mat
cv::Mat mat(480, 640, CV_8UC4);
CreateAlphaMat(mat);
namedWindow("Display Image", CV_WINDOW_AUTOSIZE );
imshow("Display Image", mat);
//PARAM for imwrite
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
imwrite("./generatepng.png", mat, compression_params);
return a.exec();
}
显示效果: