天天看点

归一化相关系数

对两幅图像进行相似度的衡量,除了用眼睛观察的方法外,我们可以更加精确地用数据来客观的评估归一化,归一化的相关系数(NC)提供了度量工具。其计算公式如下:

MATLAB代码如下所示:

function dNC = nc(ImageA,ImageB)

if (size(ImageA,1) ~= size(ImageB,1)) | (size(ImageA,2) ~= size(ImageB,2))

error('ImageA <> ImageB');

dNC = 0;

return ;

end

ImageA=double(ImageA);

ImageB=double(ImageB);

M = size(ImageA,1);

N = size(ImageA,2);

d1=0;

d2=0;

d3=0;

for i = 1:M

    for j = 1:N

        d1=d1+ImageA(i,j)*ImageB(i,j) ;

        d2=d2+ImageA(i,j)*ImageA(i,j) ;

        d3=d3+ImageB(i,j)*ImageB(i,j) ;

    end

end

dNC=d1/(sqrt(d2)*sqrt(d3));

VC代码则根据自己所用库进行相应的修改,下面附上我自己所用的代码片段:

 int imgA_width;

 int imgA_height;

 int imgB_width;

 int imgB_height;

 imgA_width = m_img1_file.GetWidth();

 imgA_height = m_img1_file.GetHeight();

 imgB_width = m_img2_file.GetWidth();

 imgB_height = m_img2_file.GetHeight();

 if((imgA_width != imgB_width) || (imgA_height != imgB_height))

 {

  AfxMessageBox(_T("输入图像大小不相等!"));

  return;

 }

 double d1=0.0;

 double d2=0.0;

 double d3=0.0;

 COLORREF colorA;

 COLORREF colorB;

 BYTE byteA;

 BYTE byteB;

 //相关系数计算

 for(int i=0;i<imgA_height;i++)

 {

  for(int j=0;j<imgA_width;j++)

  {

   colorA = m_img1_file.GetPixel(j,i);

   colorB = m_img2_file.GetPixel(j,i);

   byteA = GetRValue(colorA);

   byteB = GetRValue(colorB);

   d1 = d1+byteA*byteB;

   d2 = d2+byteA*byteA;

   d3 = d3+byteB*byteB;

  }

 }

 m_ctNC = d1/(sqrt(d2)*sqrt(d3));

 this->UpdateData(false);

继续阅读