Qt下图像生成、输入与输出

#include "widget.h"
#include 

#include 
#include 
#include 

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(i, j);
            rgba[0] = UCHAR_MAX;
            rgba[1] = saturate_cast( float((mat.cols)-j) / ((float) mat.cols) * UCHAR_MAX);
            rgba[2] = saturate_cast( float((mat.cols)-i) / ((float) mat.rows) * UCHAR_MAX);
            //rgba[3] = saturate_cast(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 compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);

    imwrite("./generatepng.png", mat, compression_params);

    return a.exec();
}

显示效果:

Qt下图像生成、输入与输出_第1张图片

你可能感兴趣的:(opencv,Qt,opencv学习,Qt,opencv)