opencv+色偏检测

// sepiancheck.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"






#include "cv.h"  
#include "highgui.h"  
#include "iostream"  
#include "math.h"  


using namespace cv;


/*********************************************************************************
*函数描述:  color     计算图像的色偏度以及色偏方向
*函数参数:  image    彩色 BGR 图片
*                     cast        计算出的偏差值,小于1表示比较正常,大于1表示存在色偏
*                     da          红/绿色偏估计值,da大于0,表示偏红;da小于0表示偏绿
*                     db          黄/蓝色偏估计值,db大于0,表示偏黄;db小于0表示偏蓝
**********************************************************************************/


void color(IplImage * image)
{
IplImage * lab = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
cvCvtColor(image, lab, CV_BGR2Lab);
double a = 0;
double b = 0;
int lista[256];
int listb[256];
for (int i = 0; i<256; i++)
{
lista[i] = 0;
listb[i] = 0;
}
CvScalar scalar;
for (int i = 0; iheight; i++)
{
for (int j = 0; jwidth; j++)
{
scalar = cvGet2D(lab, i, j);
a += (scalar.val[1] - 128);
b += (scalar.val[2] - 128);
int x = (int)scalar.val[1];
lista[x]++;
int y = (int)scalar.val[2];
listb[y]++;
}
}
double da = a / (lab->height * lab->width);
double db = b / (lab->height * lab->width);
double d = sqrt(da*da + db*db);
double ma = 0, mb = 0;
for (int i = 0; i<256; i++)
{
ma += abs(i - 128 - da)*lista[i];
mb += abs(i - 128 - db)*listb[i];
}
ma /= (lab->height * lab->width);
mb /= (lab->height * lab->width);
double m = sqrt(ma*ma + mb*mb);
double cast = d / m;
printf("cast2 : %f\n", cast);
return;
}
int main()
{
CvCapture * capture = cvCreateFileCapture("d:\\video\\bigbuckbunny_480x272.m2v");
IplImage * frame;
cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
int count = 0;
while (true)
{
count++;
frame = cvQueryFrame(capture);
if (!frame) break;
cvShowImage("video", frame);
if (!(count % 10))
{
printf("f : %d\n", count);
color(frame);
}
if (cvWaitKey(10) == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("video");


return 0;
}

你可能感兴趣的:(OPENCV)