psnr

http://blog.csdn.net/huzia/archive/2008/09/01/2861877.aspx

 


#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"
#include "stdio.h"

void CvtColor(IplImage* color,IplImage* grey){
 for (int i=0;i<color->width;i++) for (int j=0;j<color->height;j++){
  float total = 0.0;
  for (int z=0;z<color->nChannels;z++){
   total += (unsigned char)(color->imageData)[j*color->widthStep+i*color->nChannels+z];
  }
  total /= 3.0;
  for (int z=0;z<color->nChannels;z++){
   if (total>=255) total = 255; if (total<=0) total = 0;
   grey->imageData[j*grey->widthStep+i*grey->nChannels+z] = total;
  }
 }
}

double PSNRCompute(CvArr *src,CvArr *dst)
{
 double psnr=0;
 double totalValue=0;
 IplImage *srcImage = (IplImage *)src;
 IplImage *dstImage = (IplImage *)dst;
 int NR(srcImage->height),NC(srcImage->width);
 if (srcImage->width!=dstImage->width||srcImage->height!=dstImage->height)
 {
  printf("usage: Two images must have same size!/n");
 }else if (srcImage->nChannels!=dstImage->nChannels)
 {
  printf("usage:Two images must have same channels/n 1 dimension/n 3 dimension/n");
 }
 else if (srcImage->nChannels==1||dstImage->nChannels==1)
 {
  for (int i=0;i<srcImage->width;i++)
  {
   for (int j=0;j<srcImage->height;j++)
   {
    double s1 = srcImage->imageData[j*srcImage->widthStep+i*srcImage->nChannels+0];
    double s2 = dstImage->imageData[j*dstImage->widthStep+i*dstImage->nChannels+0];
    double sub = s1-s2;
    totalValue +=pow(sub,2);
   }
  }

 }
 else if(srcImage->nChannels==3||dstImage->nChannels==3)
 {
  IplImage *srcData = 0, *dstData= 0;
  srcData = cvCloneImage( srcImage );
  dstData = cvCloneImage( dstImage );

  CvtColor(srcImage,srcData);
  CvtColor(dstImage,dstData);
  cvNamedWindow("ab");
  cvShowImage("ab",srcData);
  cvWaitKey(0);

  for (int i=0;i<NR;i++)
  {
   for (int j=0;j<NC;j++)
   {
    double s1 = srcData->imageData[j*srcData->widthStep+i*srcData->nChannels+0];
    double s2 = dstData->imageData[j*dstData->widthStep+i*dstData->nChannels+0];
    double sub = s1-s2;
    totalValue +=pow(sub,2);
   }
  }

 }
 if (fabs(totalValue) < 1e-6)
 {
  psnr = 0;
 }
 else
 {
  double meanValue = totalValue/(NR*NC);
  psnr = 10*log((255*255)/meanValue);
 }
 return psnr;
}

void main(){
 float res ;
 IplImage* src = cvLoadImage("hb00.jpg");
 IplImage* dst = cvLoadImage("hb001.jpg");
 res = PSNRCompute(src,dst);
 printf("%f",res);
 getchar();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章