32 #include <opencv2/imgproc/imgproc.hpp>    56       cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
    58       if(mat.type() == CV_8UC1)
    60         a = 255.0 / std::max(max - min, DBL_EPSILON);
    62         mat.convertTo(scaled, CV_8U, a, b);
    65         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
    67         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
    70       else if(mat.type() == CV_32FC1)
    72         a = 255.0 / std::max(max - min, DBL_EPSILON);
    74         mat.convertTo(scaled, CV_8U, a, b);
    77         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
    79         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
    82       else if(mat.type() == CV_32FC3)
    84         a = 255.0 / std::max(max - min, DBL_EPSILON);
    86         mat.convertTo(scaled, CV_8UC3, a, b);
    92           cv::cvtColor(scaled, color, CV_RGB2BGRA);
    96           cv::cvtColor(scaled, color, CV_BGR2BGRA);
   100         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   103       else if(mat.type() == CV_8UC3)
   105         a = 255.0 / std::max(max - min, DBL_EPSILON);
   107         mat.convertTo(scaled, CV_8UC3, a, b);
   113           cv::cvtColor(scaled, color, CV_RGB2BGRA);
   117           cv::cvtColor(scaled, color, CV_BGR2BGRA);
   121         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   124       else if(mat.type() == CV_8UC4)
   126         a = 255.0 / std::max(max - min, DBL_EPSILON);
   128         mat.convertTo(scaled, CV_8UC4, a, b);
   134           cv::cvtColor(scaled, color, CV_RGBA2BGRA);
   144           color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   152       if(mat.type() == CV_8UC3)
   154         mat.convertTo(scaled, CV_8UC3, a, b);
   159           cv::cvtColor(scaled, color, CV_RGB2BGRA);
   163           cv::cvtColor(scaled, color, CV_BGR2BGRA);
   167         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   170       else if(mat.type() == CV_8UC4)
   172         mat.convertTo(scaled, CV_8UC4, a, b);
   177           cv::cvtColor(scaled, color, CV_RGBA2BGRA);
   185         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   190         mat.convertTo(scaled, CV_8U, a, b);
   193         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
   195         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
   205     if (mat.type() == CV_8UC4)
   207       for (
int r = 0; r < mat.rows; r++)
   209         for (
int c = 0; c < mat.cols; c++)
   211           mat.at<cv::Vec4b>(r, c)[3] = alpha;
 
void SetAlpha(cv::Mat &mat, uint8_t alpha)
cv::Mat ToBgra8(const cv::Mat &mat, const cv::Mat &mask=cv::Mat(), bool is_rgb=false, double a=0.0, double b=0.0)