目录
实现原理
1)读取识别图像的原图,并转灰度图,再归一化。
// 生成灰度图
Mat gray = Mat::zeros(input.size(), CV_32FC1);
Mat f = input.clone();
f.convertTo(f, CV_32FC3);
vector<Mat> pics;
split(f, pics);
gray = 0.299f*pics[2] + 0.587*pics[2] + 0.114*pics[0];
gray = gray / 255.f;
2)确定高光区。因为我们要识别高光,所以thresh通过gray*gray,得到的图像中原本亮的地方则为亮,取平均值当阈值,进行二值化得到掩膜mask。
// 确定高光区
Mat thresh = Mat::zeros(gray.size(), gray.type());
thresh = gray.mul(gray);
// 取平均值作为阈值
Scalar t = mean(thresh);
Mat mask = Mat::zeros(gray.size(), CV_8UC1);
mask.setTo(255, thresh >= t[0]);
3)对掩膜区边缘进行平滑过渡。假设light为50,那么midrate的掩膜区值为1.5,黑色区为1,过渡区为1~1.5;bright的掩膜区为0.125,黑色区为0,过渡区为0~0.125。
// 参数设置
int max = 4;
float bright = light / 100.0f / max;
float mid = 1.0f + max * bright;
// 边缘平滑过渡
Mat midrate = Mat::zeros(input.size(), CV_32FC1);
Mat brightrate = Mat::zeros(input.size(), CV_32FC1);
for (int i = 0; i < input.rows; ++i)
{
uchar *m = mask.ptr<uchar>(i);
float *th = thresh.ptr<float>(i);
float *mi = midrate.ptr<float>(i);
float *br = brightrate.ptr<float>(i);
for (int j = 0; j < input.cols; ++j)
{
if (m[j] == 255)
{
mi[j] = mid;
br[j] = bright;
}
else {
mi[j] = (mid - 1.0f) / t[0] * th[j] + 1.0f;
br[j] = (1.0f / t[0] * th[j])*bright;
}
}
}
4)根据midrate和brightrate,进行高光区提亮。对非高光区而言,midrate都为1,brightrate都为0,即没有变化;对高光区而言,midrate都为1.5,brightrate都为0.125,所以色彩数值均有所增加,带来了提亮效果;对边缘地区,midrate和brightrate起到了很好的过渡作用。
// 高光提亮,获取结果图
Mat result = Mat::zeros(input.size(), input.type());
for (int i = 0; i < input.rows; ++i)
{
float *mi = midrate.ptr<float>(i);
float *br = brightrate.ptr<float>(i);
uchar *in = input.ptr<uchar>(i);
uchar *r = result.ptr<uchar>(i);
for (int j = 0; j < input.cols; ++j)
{
for (int k = 0; k < 3; ++k)
{
float temp = pow(float(in[3 * j + k]) / 255.f, 1.0f / mi[j])*(1.0 / (1 - br[j]));
if (temp > 1.0f)
temp = 1.0f;
if (temp < 0.0f)
temp = 0.0f;
uchar utemp = uchar(255*temp);
r[3 * j + k] = utemp;
}
}
}
代码实现
function.h
#pragma once
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat HighLight(Mat input, int light);
function.cpp
#include "function.h"
// 图像高光选取
Mat HighLight(Mat input, int light)
{
// 生成灰度图
Mat gray = Mat::zeros(input.size(), CV_32FC1);
Mat f = input.clone();
f.convertTo(f, CV_32FC3);
vector<Mat> pics;
split(f, pics);
gray = 0.299f * pics[2] + 0.587 * pics[2] + 0.114 * pics[0];
gray = gray / 255.f;
// 确定高光区
Mat thresh = Mat::zeros(gray.size(), gray.type());
thresh = gray.mul(gray);
// 取平均值作为阈值
Scalar t = mean(thresh);
Mat mask = Mat::zeros(gray.size(), CV_8UC1);
mask.setTo(255, thresh >= t[0]);
// 参数设置
int max = 4;
float bright = light / 100.0f / max;
float mid = 1.0f + max * bright;
// 边缘平滑过渡
Mat midrate = Mat::zeros(input.size(), CV_32FC1);
Mat brightrate = Mat::zeros(input.size(), CV_32FC1);
for (int i = 0; i < input.rows; ++i)
{
uchar* m = mask.ptr<uchar>(i);
float* th = thresh.ptr<float>(i);
float* mi = midrate.ptr<float>(i);
float* br = brightrate.ptr<float>(i);
for (int j = 0; j < input.cols; ++j)
{
if (m[j] == 255)
{
mi[j] = mid;
br[j] = bright;
}
else {
mi[j] = (mid - 1.0f) / t[0] * th[j] + 1.0f;
br[j] = (1.0f / t[0] * th[j]) * bright;
}
}
}
// 高光提亮,获取结果图
Mat result = Mat::zeros(input.size(), input.type());
for (int i = 0; i < input.rows; ++i)
{
float* mi = midrate.ptr<float>(i);
float* br = brightrate.ptr<float>(i);
uchar* in = input.ptr<uchar>(i);
uchar* r = result.ptr<uchar>(i);
for (int j = 0; j < input.cols; ++j)
{
for (int k = 0; k < 3; ++k)
{
float temp = pow(float(in[3 * j + k]) / 255.f, 1.0f / mi[j]) * (1.0 / (1 - br[j]));
if (temp > 1.0f)
temp = 1.0f;
if (temp < 0.0f)
temp = 0.0f;
uchar utemp = uchar(255 * temp);
r[3 * j + k] = utemp;
}
}
}
return result;
}
main.cpp
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include "function.h"
using namespace cv;
using namespace std;
int main()
{
Mat scr = imread("F:\\picture\\江.jpg");
Mat res = HighLight(scr, -50);
imshow("原图", scr);
imshow("处理后", res);
waitKey(0);
return 0;
}
结果
如果以上内容对你有所帮助,不妨点赞关注一下,感谢观看!