You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.9 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <vector>
#include <iostream>
#include <exception>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <sys/time.h>
#include "ImageDetectConfig.h"
#include "log_manager.h"
using namespace std;
using namespace cv;
// cast 计算出的偏差值小于1.0表示比较正常大于1.0表示存在亮度异常;
// 当cast异常时da大于0表示过亮da小于0表示过暗。
void detect_brightness(cv::Mat input_img, float& cast, float& da)
{
cv::Mat gray_img;
cv::cvtColor(input_img, gray_img, COLOR_BGR2GRAY);
float a = 0, Ma = 0;
int hist[256] = { 0 };
for (int i = 0; i < gray_img.rows; i++)
{
for (int j = 0; j < gray_img.cols; j++) {
a += float(gray_img.at<uchar>(i, j) - 128); // 在计算过程中考虑128为亮度均值点
hist[gray_img.at<uchar>(i, j)]++;
}
}
da = a / float(gray_img.total());
for (int i = 0; i < 256; i++) {
Ma += abs(i - 128 - da) * hist[i];
}
Ma /= float(gray_img.total());
cast = abs(da) / abs(Ma);
}
void detect_saturation(cv::Mat input_img, float& satu)
{
Mat dstImage1;
cvtColor(input_img, dstImage1, COLOR_RGB2HSV);
vector<Mat> channels;
split(dstImage1, channels);//分离HSV
Scalar mean; //均值
Scalar stddev; //标准差
meanStdDev( channels[1], mean, stddev ); //计算均值和标准差
satu = mean.val[0];
}
int main(int argc, char *argv[])
{
Mat img = imread(argv[1]);
float cast, da, satu;
detect_brightness(img, cast, da);
detect_saturation(img, satu);
cout<<cast<<"||"<<da<<"||"<<satu;
return 0;
}