#include "ImgProcession.h" /** * @brief mat数值累加 * @param src:矩阵mat */ double ImgProcession::matsum(Mat src) { //初始化 double total; //翻写numpy的sum函数,可以用来检查图像的数据 //快速写法 Scalar ss = sum(src); for (int i = 0; i < ss.rows; i++) { total = total + ss[i]; } //Eigen写法 需要-O3的编译优化状态下,速度提升10倍 /*vectormat_split; split(src, mat_split); Map */ return total; } // 注意float类型跟python一样 float value_cast(float value) { if (value > 255){ value = 255; } else if (value < 0){ value = 0; } return value; } /** * @brief 亮度增强 * @param src:图像流 * @param arr:调整参数,一般取1.15 * @return dst:新图像 */ Mat ImgProcession::BrightEnhance(Mat src, double arr) { Mat src_T = src.clone(); int row = src.rows; int col = src.cols; vector> map_b(mat_split[0].data, mat_split[0].rows, mat_split[0].cols), map_g(mat_split[1].data, mat_split[1].rows, mat_split[1].cols), map_r(mat_split[2].data, mat_split[2].rows, mat_split[2].cols); total = map_b.cast ().sum() + map_g.cast ().sum() + map_r.cast ().sum(); bgr_split, hsv_split; split(src_T, bgr_split); Map char, Dynamic, Dynamic, RowMajor>> map_b(bgr_split[0].data, row, col), map_g(bgr_split[1].data, row, col), map_r(bgr_split[2].data, row, col); Mat src_R = src.clone(); Mat src_hsv; cvtColor(src_R, src_hsv, COLOR_BGR2HSV); split(src_hsv, hsv_split); Map char, Dynamic, Dynamic, RowMajor>> map_v(hsv_split[2].data, row, col); // 计算亮度均值 double mean_v = map_v.cast<double>().mean(); double rate_bri = 200 / mean_v; // 配置参数 if (rate_bri > arr) { rate_bri = 1.15; } // 融合图像 map_b = (map_b.cast<double>() * rate_bri).unaryExpr(ptr_fun(value_cast)).cast char>(), map_g = (map_g.cast<double>() * rate_bri).unaryExpr(ptr_fun(value_cast)).cast char>(), map_r = (map_r.cast<double>() * rate_bri).unaryExpr(ptr_fun(value_cast)).cast char>(); Mat mat_dst_b(row, col, CV_8UC1, map_b.data()), mat_dst_g(row, col, CV_8UC1, map_g.data()), mat_dst_r(row, col, CV_8UC1, map_r.data()); vector nm; nm.emplace_back(move(mat_dst_b)); nm.emplace_back(move(mat_dst_g)); nm.emplace_back(move(mat_dst_r)); Mat dst; merge(nm, dst); return dst; } /** * @brief 色度增强 * @param src:图像流 * @param arr:调整参数,一般取1.5 * @return dst:新图像 */ Mat ImgProcession::ColorEnhance(Mat src, double arr) { Mat src_T = src.clone(); int row = src.rows; int col = src.cols; vector mat_split; split(src_T, mat_split); Map char, Dynamic, Dynamic, RowMajor>> map_b(mat_split[0].data, row, col), map_g(mat_split[1].data, row, col), map_r(mat_split[2].data, row, col); Matrix<int, Dynamic, Dynamic, RowMajor> map_f = (map_r.cast<double>() * 0.299 + map_g.cast<double>() * 0.587 + map_b.cast<double>() * 0.114).cast<int>(); // 融合图像 map_b = ((map_f.cast<double>() * (1.0 - arr)).array() + (map_b.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(), map_g = ((map_f.cast<double>() * (1.0 - arr)).array() + (map_g.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(), map_r = ((map_f.cast<double>() * (1.0 - arr)).array() + (map_r.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(); Mat mat_dst_b(row, col, CV_8UC1, map_b.data()), mat_dst_g(row, col, CV_8UC1, map_g.data()), mat_dst_r(row, col, CV_8UC1, map_r.data()); vector nm; nm.emplace_back(move(mat_dst_b)); nm.emplace_back(move(mat_dst_g)); nm.emplace_back(move(mat_dst_r)); Mat dst; merge(nm, dst); return dst; } /** * @brief 对比度增强 * @param src:图像流 * @param arr:调整参数,一般取1.5 * @return dst:新图像 */ Mat ImgProcession::ContrastEnhance(Mat src, double arr) { Mat src_T = src.clone(); int row = src.rows; int col = src.cols; vector mat_split; split(src_T, mat_split); Map char, Dynamic, Dynamic, RowMajor>> map_b(mat_split[0].data, row, col), map_g(mat_split[1].data, row, col), map_r(mat_split[2].data, row, col); int mean = (map_r.cast<double>() * 0.299 + map_g.cast<double>() * 0.587 + map_b.cast<double>() * 0.114).cast<int>().cast<double>().mean() + 0.5; // 融合图像 map_b = (mean * (1.0 - arr) + (map_b.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(), map_g = (mean * (1.0 - arr) + (map_g.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(), map_r = (mean * (1.0 - arr) + (map_r.cast<double>() * arr).array()).unaryExpr(ptr_fun(value_cast)).cast char>(); Mat mat_dst_b(row, col, CV_8UC1, map_b.data()), mat_dst_g(row, col, CV_8UC1, map_g.data()), mat_dst_r(row, col, CV_8UC1, map_r.data()); vector nm; nm.emplace_back(move(mat_dst_b)); nm.emplace_back(move(mat_dst_g)); nm.emplace_back(move(mat_dst_r)); Mat dst; merge(nm, dst); return dst; } /** * 同时增强对比对颜色 * @param src * @param arr * @return */ Mat ImgProcession::ContrastAndColorEnhance(Mat src, double con, double co){ Mat dst = ContrastEnhance(src, con); dst = ColorEnhance(dst, co); return dst; }
#include#include #include #include #include #include using namespace std; using namespace cv; using namespace Eigen; class ImgProcession { public: static double matsum(Mat src); static Mat BrightEnhance(Mat src, double arr=1.15); static Mat ColorEnhance(Mat src, double arr=1.5); static Mat ContrastEnhance(Mat src, double arr=1.5); static Mat ContrastAndColorEnhance(Mat src, double con, double col); };