c++测试图片亮度,黑屏,清晰度

参考:
https://zhuanlan.zhihu.com/p/348761432
https://zhuanlan.zhihu.com/p/348588299
https://zhuanlan.zhihu.com/p/348601853

#include
using namespace std;
#include 
using namespace cv;//下面的所有cv相关类型不用加上前缀了 
cv::Ptr<BackgroundSubtractor> pMOG2 = cv::createBackgroundSubtractorMOG2(500, 16, true);


//liangdu_det;<1,zhengchang;>1,buchengchang
// void detect(Mat input_img)
// {
//     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(i, j) - 128); // 在计算过程中,考虑128为亮度均值点    
//             hist[gray_img.at(i, j)]++;       
//         }    
//     }    

//     float 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());   
//     float cast = abs(da) / abs(Ma);
//     cout<
// }

//heiping_det,dayu0.85heiping
// void detect(Mat input_img)
// {
//     Mat gray_img;  
//     cv::cvtColor(input_img, gray_img, COLOR_BGR2GRAY);

//     int dark_sum = 0;
//     for (int i = 0; i < gray_img.rows; i++)
//     {
//         for (int j = 0; j < gray_img.cols; j++) {
//             if (gray_img.at(i, j) < 20)
//             {
//                 dark_sum++;
//                 // cout<<"sum"<
//             }
                
//         }
//     }

//     float cast = dark_sum / float(gray_img.total());
//     cout<
// }


//clear <1.5,yichang
void detect(Mat input_img)
{
    Mat sobel_img;
    cv::Sobel(input_img, sobel_img, CV_16U, 1, 1);
    float cast = cv::mean(sobel_img)[0];
    cout<<cast<<endl;

}

void separation_foreground(cv::Mat input_img, cv::Mat &mask)
{	
    pMOG2->apply(input_img, mask);
}


int
main (int argc, char *argv[])
{
    
    Mat foreground_img;
    Mat input_img =imread(argv[1]);
    separation_foreground(input_img, foreground_img);
    
    detect(foreground_img);
    return 1;
}

精简修改版:

#include
 
using namespace std;
using namespace cv;
void BrightnessDetect(const cv::Mat &src, int &Refer, float &Mean, float &MeanDev)
{
	CV_Assert(!src.empty());
 
	cv::Mat gray;	
	if (3 == src.channels())
		cv::cvtColor(src, gray, COLOR_RGB2RGBA);
	else
		gray = src.clone();
 	
	cv::Scalar meanGrayS = cv::mean(gray);
	float meanGray = meanGrayS[0];
	
	Mean = meanGray - Refer;
	
	int nRows = gray.rows;
	int nCols = gray.cols;
	int sumTemp = 0;
	for (int j = 0; j < nRows; j++)
	{
		uchar *pGray = gray.ptr<uchar>(j);
		for (int i = 0; i < nCols; i++)
		{
			int diffTemp = pGray[i] - 128;
			int absTemp = abs(diffTemp - Mean);
			sumTemp += absTemp;	}
	}
	MeanDev = ((float)sumTemp / (nRows * nCols));
}


void BrightnessDetect2(Mat input_img, float &Mean2)
{
    Mat gray_img;  
    cv::cvtColor(input_img, gray_img, COLOR_BGR2GRAY);

    int dark_sum = 0;
    for (int i = 0; i < gray_img.rows; i++)
    {
        for (int j = 0; j < gray_img.cols; j++) {
            if (gray_img.at<uchar>(i, j) < 20)
            {
                dark_sum++;
                // cout<<"sum"<
            }
                
        }
    }

    Mean2 = dark_sum / float(gray_img.total());
}
int main(int argc, char *argv[])
{
	cv::Mat src = cv::imread(argv[1], 1);
	if (src.empty())
	{
		cout << "image is none" << endl;
		return -1;
	}
 
	int Refer = 128;
	float MeanDev = 0.0; 
	float Mean = 0.0;	
	//方法一
	BrightnessDetect(src, Refer, Mean, MeanDev);
	if (MeanDev < abs(Mean))	
	{
		if (Mean > 0)		
			cout << "method1 light" << endl;
		else if (Mean < 0)	
			cout << "method1 dark" << endl;
		else
			cout << "method1 nomal" << endl;
	}
	else
		cout << "method1 nomal" << endl;
 

	//方法二
	float Mean2 = 0.0;	
	BrightnessDetect2(src, Mean2);
	if(Mean2>0.85)
	{
		cout << "method2 dark" << endl;
	}



	
    // Mat imageGrey;
	// cvtColor(src, imageGrey, COLOR_BGR2GRAY);
	// Mat imageSobel;
 
	// Laplacian(imageGrey, imageSobel, CV_16U);
	
	// double meanValue = 0.0;
	// meanValue = mean(imageSobel)[0];
	// cout<<"Laplacian meanValue "<

	Mat sobel_img;
    cv::Sobel(src, sobel_img, CV_16U, 1, 1);
    float cast = cv::mean(sobel_img)[0];
	if (cast<1.5)
	{
		cout<<"unclear"<<endl;
	}
	else
	{
		cout<<"clear"<<endl;
	}
	return 0;
}



 



你可能感兴趣的:(c++,开发语言)