int bgr2hsi2(Mat &image,Mat &hsi)
{
if(!image.data){
//cout<<"Miss Data"<<endl;
return -1;
}
int nl = image.rows;
int nc = image.cols;
if(image.isContinuous()){
nc = nc*nl;
nl = 1;
}
Mat(image.size(), CV_32FC3).copyTo(hsi);
for(int i = 0;i < nl;i++){
uchar *src = image.ptr<uchar>(i);
float *dst = hsi.ptr<float>(i);
for(int j = 0;j < nc;j++){
//float b = src[j*3]/255.0;
//float g = src[j*3+1]/255.0;
//float r = src[j*3+2]/255.0;
float b = src[j*3];
float g = src[j*3+1];
float r = src[j*3+2];
float num = (float)(0.5*((r-g)+(r-b)));
float den = (float)sqrt((r-g)*(r-g)+(r-b)*(g-b));
float H,S,I;
if(den == 0){ //分母不能为0
H = 0;
}
else{
double theta = acos(num/den);
H = theta;
//if(b <= g)
// H = theta/(CV_PI*2);
//else
// H = (2*CV_PI - theta)/(2*CV_PI);
}
double minRGB = min(min(r,g),b);
den = r+g+b;
if(den == 0) //分母不能为0
S = 0;
else
S = 1 - 3*minRGB/den;
I = den/3.0;
//将S分量和H分量都扩充到[0,255]区间以便于显示;
//一般H分量在[0,2pi]之间,S在[0,1]之间
dst[3*j] = H;
dst[3*j+1] = S;
dst[3*j+2] = I;
}
}
//vector<Mat> vecMat;
//split(hsi, vecMat);
//vector<double> vecMean;
//vector<double> vecDev;
//meanStdDev(vecMat[0], vecMean, vecDev);
//printf("%lf, %lf\n", vecMean[0], vecDev[0]);
//meanStdDev(vecMat[1], vecMean, vecDev);
//printf("%lf, %lf\n", vecMean[0], vecDev[0]);
//meanStdDev(vecMat[2], vecMean, vecDev);
//printf("%lf, %lf\n", vecMean[0], vecDev[0]);
return 0;
}