提取的是颜色直方图 纹理灰度共生矩阵 形状是Hu7 先把代码在这里备份一个(代码风格挺舒服的) typedef struct RGB ... { double R,G,B;} RGB;typedef struct HSV ... { double H,S,V;} HSV;typedef struct HistogramVector ... {int vector[72];} HistogramVector;typedef struct GrayMatrixVector ... {double vector[8];} GrayMatrixVector;#include #include " cv.h " #include " highgui.h " #include " imageFeatureExtract.h " #include using namespace std; /**/ /*共现矩阵*/ int ** PMatrixRD; // 45度方向上的灰度共现矩阵 int ** PMatrixLD; // 135度方向上的灰度共现矩阵 int ** PMatrixV; // 90度方向上的灰度共现矩阵 int ** PMatrixH; // 0度方向上的灰度共现矩阵 /**/ /*颜色特征*/ double _max( double a, double b, double c) ... {if(a>=b && a>=c) return a;else if(b>=a && b>=c) return b;else return c;} double _min( double a, double b, double c) ... {if(a<=b && a<=c) return a;else if(b<=a && b<=c) return b;else return c;} int RGB2HSV(RGB * rgb,HSV * hsv) ... {double r,g,b; // temp hsv->V=_max(rgb->R,rgb->G,rgb->B);hsv->S=(hsv->V-_min(rgb->R,rgb->G,rgb->B))/hsv->V;r=(hsv->V-rgb->R)/(hsv->V-_min(rgb->R,rgb->G,rgb->B));g=(hsv->V-rgb->G)/(hsv->V-_min(rgb->R,rgb->G,rgb->B));b=(hsv->V-rgb->B)/(hsv->V-_min(rgb->R,rgb->G,rgb->B));if(rgb->R==_max(rgb->R,rgb->G,rgb->B) && rgb->G==_min(rgb->R,rgb->G,rgb->B))...{ hsv->H=5+b; }else if (rgb->R==_max(rgb->R,rgb->G,rgb->B) && rgb->G!=_min(rgb->R,rgb->G,rgb->B))...{ hsv->H=1-g; } else if (rgb->G==_max(rgb->R,rgb->G,rgb->B) && rgb->B==_min(rgb->R,rgb->G,rgb->B))...{ hsv->H=1+r;}else if (rgb->G==_max(rgb->R,rgb->G,rgb->B) && rgb->B!=_min(rgb->R,rgb->G,rgb->B))...{ hsv->H=3-b;}else if (rgb->B==_max(rgb->R,rgb->G,rgb->B) && rgb->R==_min(rgb->R,rgb->G,rgb->B))...{ hsv->H=3+g;}else ...{ hsv->H=5-r;}return 1;} int HSV2L(HSV * hsv) ... {double h,s,v;int H,S,V,L;h=hsv->H;s=hsv->S;v=hsv->V;if(h>0.2 && h<=0.4) H=1;else if(h>0.4 && h<=0.75) H=2;else if(h>0.75 && h<=1.55) H=3;else if(h>1.55 && h<=1.9) H=4;else if(h>1.9 && h<=2.7) H=5;else if(h>2.7 && h<=2.95) H=6;else if(h>2.95 && h<=3.15) H=7;else H=0;if(v>=0 && v<=0.2) V=0;else if(v>0.2 && v<=0.7) V=1;else V=2;if(s>=0 && s<=0.2) S=0;else if(s>0.2 && s<=0.7) S=1;else S=2;L=9*H+3*S+V;return L;} int colorFeatureExtract( char * imagePath,HistogramVector * histogramVector) ... {IplImage* pImg; //声明IplImage指针char *imageData;unsigned char pels[3];char *pelsIndex;int width;int height;int size;int step;int L;RGB rgb;HSV hsv; //载入图像 if((pImg = cvLoadImage(imagePath, 1)) != 0 ) ...{ width=pImg->width; height=pImg->height; size=pImg->imageSize; step=pImg->widthStep; imageData=pImg->imageData; pelsIndex=imageData; memset(histogramVector->vector, 0, sizeof(histogramVector->vector)); for(int row=0;row ...{ for(int line=0;line ...{ pels[0]=*pelsIndex; pels[1]=*(pelsIndex+1); pels[2]=*(pelsIndex+2); pelsIndex+=3; rgb.B=pels[0]/256.0; rgb.G=pels[1]/256.0; rgb.R=pels[2]/256.0; RGB2HSV(&rgb,&hsv); L=HSV2L(&hsv); histogramVector->vector[L]++; //printf("%d,%d,%f,%f,%f ",row,line,hsv.H,hsv.S,hsv.V); } pelsIndex=imageData+step*(row+1); } cvReleaseImage( &pImg ); //释放图像 return 1; }return 0;} /**/ /*形状特征*/ int shapeFeatureExtract( char * imagePath,CvHuMoments * hu) ... {IplImage* oriImg; IplImage* grayImg;CvMoments moments; if((oriImg = cvLoadImage(imagePath, 1)) != 0)...{ grayImg = cvCreateImage(cvSize(oriImg->width,oriImg->height),IPL_DEPTH_8U,1); cvCvtColor(oriImg,grayImg,CV_BGR2GRAY); cvMoments(grayImg,&moments,0); cvGetHuMoments(&moments, hu); cvReleaseImage( &oriImg ); cvReleaseImage( &grayImg ); return 1;}return 0;} /**/ /*纹理特征*/ // 计算均值 double countU( double array[], int length) ... {double sum =0 ;double u;for(int i=0; i...{ sum += array[i];}u=sum/length;return u;} // 计算标准差 double countSigma( double array[], int length) ... {double sum =0 ;double u;double sigma;u=countU(array,length);for(int i=0; i...{ sum += (array[i]-u)*(array[i]-u);}sigma=sqrt(sum/length);return sigma;} // 初始化同现矩阵 int initMatrix( int grayLayerNum) ... {PMatrixH = new int*[grayLayerNum];PMatrixLD= new int*[grayLayerNum];PMatrixRD= new int*[grayLayerNum];PMatrixV = new int*[grayLayerNum];for(int i=0; i...{ PMatrixH[i] = new int[grayLayerNum]; PMatrixLD[i]= new int[grayLayerNum]; PMatrixRD[i]= new int[grayLayerNum]; PMatrixV[i] = new int[grayLayerNum];}return 1;} // 销毁同现矩阵 int destroyMatrix( int grayLayerNum) ... {for(int i=0; i...{ delete[] PMatrixH[i]; delete[] PMatrixLD[i]; delete[] PMatrixRD[i]; delete[] PMatrixV[i];}delete[] PMatrixH; delete[] PMatrixLD;delete[] PMatrixRD;delete[] PMatrixV;return 1;} // 计算同现矩阵 int computeMatrix(IplImage * pImg, int grayLayerNum, int distance) ... {int i,j;unsigned char **NewImage;int width=pImg->width;int height=pImg->height;int step=pImg->widthStep;unsigned char * imageData=(unsigned char *)pImg->imageData;//初始化NewImageNewImage = new unsigned char *[height];for(i=0; i NewImage[i] = new unsigned char[width];for(i=0; i...{ for(j=0; j ...{ //分成GrayLayerNum个灰度级 NewImage[i][j] = imageData[(i*step+j)] / (256/grayLayerNum); }} //同现矩阵清0for(i=0; i...{ for(j=0; j ...{ PMatrixH[i][j] = 0; PMatrixLD[i][j] = 0; PMatrixRD[i][j] = 0; PMatrixV[i][j] = 0; }}//计算0度的灰度共现阵for(i=0; i...{ for(j=0; j ...{ PMatrixH[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[i][j+distance]] += 1; PMatrixH[(unsigned int)NewImage[i][j+distance]][(unsigned int)NewImage[i][j]] += 1; }}//计算90度的灰度共现阵for(i=0; i...{ for(j=0; j ...{ PMatrixV[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[i+distance][j]] += 1; PMatrixV[(unsigned int)NewImage[i+distance][j]][(unsigned int)NewImage[i][j]] += 1; }}//计算135度的灰度共现阵for(i=0; i...{ for(j=0; j ...{ int newi, newj; newi = i+distance; newj = j+distance; PMatrixLD[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[newi][newj]] += 1; PMatrixLD[(unsigned int)NewImage[newi][newj]][(unsigned int)NewImage[i][j]] += 1; }}//计算45度的灰度共现阵for(i=distance; i...{ for(j=0; j ...{ int newi, newj; newi = i-distance; newj = j+distance; PMatrixRD[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[newi][newj]] += 1; PMatrixRD[(unsigned int)NewImage[newi][newj]][(unsigned int)NewImage[i][j]] += 1; }}//销毁NewImagefor(i=0; i delete[] NewImage[i];delete[] NewImage;return 1;} // 计算特征参数 int computeFeature( double * featureCON, double * featureASM, double * featureNET, double * featureCOR, int ** pMatrix, int dim) ... {int i,j;double **pdMatrix;//初始化pdMatrixpdMatrix = new double*[dim];for(i=0; i pdMatrix[i] = new double[dim];int total = 0;for(i=0; i...{ for(j=0; j ...{ total += pMatrix[i][j]; }}for(i=0; i...{ for(j=0; j ...{ pdMatrix[i][j] = (double)pMatrix[i][j]/(double)total; }}*featureCON = 0.0;*featureASM = 0.0;*featureNET = 0.0;*featureCOR = 0.0;//计算反差、能量、熵for(i=0; i...{ for(j=0; j ...{ //反差 *featureCON += (i-j)*(i-j)*pdMatrix[i][j]*pdMatrix[i][j]; //能量 *featureASM += pdMatrix[i][j]*pdMatrix[i][j]; //熵 if(pdMatrix[i][j]>1e-12) ...{ *featureNET -= pdMatrix[i][j]*log(pdMatrix[i][j]); } }} /**//*//计算ux,sigmaxdouble ux = 0.0;double sigmax = 0.0;double * array = 0;array =new double[dim] ;memset(array,0,sizeof(double)*dim);for(i=0; i{ for(j=0; j { array[i] += pdMatrix[i][j]; }}ux=countU(array,dim);sigmax=countSigma(array,dim);//计算uy,sigmaydouble uy = 0.0;double sigmay = 0.0;memset(array,0,sizeof(double)*dim);for(j=0; j{ for(i=0; i { array[j] += pdMatrix[i][j]; }}uy=countU(array,dim);sigmay=countSigma(array,dim);//计算相关for(i=0; i{ for(j=0; j { *featureCOR += pdMatrix[i][j]*i*j; }}*featureCOR -=ux*uy;*featureCOR /= sigmax;*featureCOR /= sigmay;*///计算uxdouble ux = 0.0;double localtotal = 0.0;for(i=0; i...{ localtotal = 0.0; for(j=0; j ...{ localtotal += pdMatrix[i][j]; } ux += (double)i * localtotal;}//计算uydouble uy = 0.0;for(j=0; j...{ localtotal = 0.0; for(i=0; i ...{ localtotal += pdMatrix[i][j]; } uy += (double)j * localtotal;}//计算sigmaxdouble sigmax = 0.0;for(i=0; i...{ localtotal = 0.0; for(j=0; j ...{ localtotal += pdMatrix[i][j]; } sigmax += (double)(i-ux) * (double)(i-ux) * localtotal;}//计算sigmaydouble sigmay = 0.0;for(j=0; j...{ localtotal = 0.0; for(i=0; i ...{ localtotal += pdMatrix[i][j]; } sigmay += (double)(j-uy) * (double)(j-uy) * localtotal;}//计算相关for(i=0; i...{ for(j=0; j ...{ *featureCOR += (double)(i-ux) * (double)(j-uy) * pdMatrix[i][j]; }}*featureCOR /= sigmax;*featureCOR /= sigmay;//销毁pdMatrixfor(i=0; i delete[] pdMatrix[i];delete[] pdMatrix;return 1;} int computeFeatureVector(IplImage * pImg,GrayMatrixVector * grayMatrixVector) ... {double featureCON[4],featureASM[4],featureNET[4],featureCOR[4];memset(grayMatrixVector->vector, 0, sizeof(grayMatrixVector->vector));computeMatrix(pImg,64,1);computeFeature(&featureCON[0],&featureASM[0],&featureNET[0],&featureCOR[0],PMatrixH,64);computeFeature(&featureCON[1],&featureASM[1],&featureNET[1],&featureCOR[1],PMatrixV,64);computeFeature(&featureCON[2],&featureASM[2],&featureNET[2],&featureCOR[2],PMatrixLD,64);computeFeature(&featureCON[3],&featureASM[3],&featureNET[3],&featureCOR[3],PMatrixRD,64);grayMatrixVector->vector[0]=countU(featureCON,4); grayMatrixVector->vector[1]=countSigma(featureCON,4);grayMatrixVector->vector[2]=countU(featureASM,4);grayMatrixVector->vector[3]=countSigma(featureASM,4);grayMatrixVector->vector[4]=countU(featureNET,4);grayMatrixVector->vector[5]=countSigma(featureNET,4);grayMatrixVector->vector[6]=countU(featureCOR,4);grayMatrixVector->vector[7]=countSigma(featureCOR,4);return 1;} int textureFeatureExtract( char * imagePath,GrayMatrixVector * grayMatrixVector) ... {IplImage* oriImg; IplImage* grayImg; if((oriImg = cvLoadImage(imagePath, 1)) != 0)...{ grayImg = cvCreateImage(cvSize(oriImg->width,oriImg->height),IPL_DEPTH_8U,1); cvCvtColor(oriImg,grayImg,CV_BGR2GRAY); computeFeatureVector(grayImg,grayMatrixVector); cvReleaseImage( &oriImg ); cvReleaseImage( &grayImg ); return 1;}return 0;} int main( int argc, char ** argv ) ... { CvHuMoments hu;shapeFeatureExtract(argv[1],&hu);cout<<hu.hu1<<' '<<hu.hu2<<' '<<hu.hu3<<' '<<hu.hu4<<' '<<hu.hu5<<' '<<hu.hu6<<' '< /**//*HistogramVector histogramVector;colorFeatureExtract(argv[1],&histogramVector); for(int i=0;i<71;i++){ cout<}*//**//*GrayMatrixVector grayMatrixVector;initMatrix(64);textureFeatureExtract(argv[1],&grayMatrixVector);for(int i=0;i<8;i++){ cout<}destroyMatrix(64);*/ return 1;} collected by barenx