vc++实现基于内容的图像检索系统(二)
基于颜色特征的检索——颜色矩
(一)颜色空间转换:由RGB 空间结构转换为HSV空间;
(二)计算颜色矩:变量m_graph记录值的变化;
(三)保存颜色矩:保存在feature_color。
具体方法在centerM()函数中实现:
void CImagetrievalDlg::centerM(CString pathfile,int mode)
{
CBitmap hbmp;
HBITMAP hbitmap;
//装载资源
hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),pathfile,IMAGE_BITMAP,
0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
hbmp.Attach(hbitmap);
//获取图像格式
BITMAP bm;
hbmp.GetBitmap(&bm);
//创建临时的内存DC对象
CDC dcMem;
dcMem.CreateCompatibleDC(GetDC());
dcMem.SelectObject(hbmp);
int width=bm.bmWidth;
int height=bm.bmHeight;
int totalnum= height *width;
long m_graph[3][12];
float m_graphf[3][12];;
for(int i=0;i<3;i++)
for(int j=0;j<12;j++)
{
m_graph[i][j]=0;
m_graphf[i][j]=0.0;
if(mode==1)
feature_color[i][j]=0.0;
else
feature_color_1[i][j]=0.0;
}
COLORREF color;
double h=0,s=0,v=0;
for(long y=0;y<height;y++)
{
for(long x=0;x<width;x++)
{
color=dcMem.GetPixel(x,y);
RGBToHSV(GetRValue(color),GetGValue(color),GetBValue(color),&h,&s,&v);
int result_h=(int)(6*h/PI);
if( result_h ==12)
m_graph[0][11]++;
else
m_graph[0][result_h]++;
int result_s=(int)(s*12);
if( result_s ==12)
m_graph[1][11]++;
else
m_graph[1][result_s]++;
int result_v=(int)(v*12);
if( result_v ==12)
m_graph[2][11]++;
else
m_graph[2][result_v]++;
}
}
for(int i=0;i<3;i++)
for(int j=0;j<12;j++)
m_graphf[i][j]=((float)m_graph[i][j])/((float)totalnum);
float m1[3],m2[3],m3[3];
for(int i=0;i<3;i++)
{
m1[i] = 0.0;
m2[i] = 0.0;
m3[i] = 0.0;
}
for(int i=0;i<3;i++)
for(int j=0;j<12;j++)
m1[i] +=m_graphf[i][j]/12;
for(int i=0;i<3;i++)
for(int j=0;j<12;j++)
{
m2[i] +=((m_graphf[i][j] - m1[i]) * (m_graphf[i][j] - m1[i]))/12;
m3[i] +=((m_graphf[i][j] - m1[i]) * (m_graphf[i][j] - m1[i])
* (m_graphf[i][j] - m1[i]))/12;
}
float zz=1/3;
for(int i=0;i<3;i++)
{
m2[i] = sqrtf(m2[i]);
m3[i] = (float)pow( m3[i