OpenCV Jpeg编解码

源码来自:https://code.google.com/p/opencvjp-sample/source/browse/trunk/cpp/encode_decode_test.cpp?r=63

描述:描述读取一张图片进行jpg和png压缩,并解压显示。

主要函数:imencode和imdecode。

环境:windows7+vs2010+opencv2.4.8。

工程地址:http://download.csdn.net/detail/chengkun183/7609491

源码:

 
  1. // JPEGDecodeEncode.cpp : 定义控制台应用程序的入口点。

  2.  
  3. #include "stdafx.h"

  4. #include

  5. #include

  6. #include

  7. #include

  8. using namespace std;

  9. using namespace cv;

  10.  
  11. double getPSNR(Mat& src1, Mat& src2, int bb=0);

  12.  
  13. int main(int argc, char** argv)

  14. {

  15. Mat src = imread("lena.png");

  16. cout<<"origin image size: "<

  17. cout<<"height: "<

  18. cout<<"height*width*depth: "<

  19. //(1) jpeg compression

  20. vector buff;//buffer for coding

  21. vector param = vector(2);

  22. param[0]=CV_IMWRITE_JPEG_QUALITY;

  23. param[1]=95;//default(95) 0-100

  24.  
  25. imencode(".jpg",src,buff,param);

  26. cout<<"coded file size(jpg): "<

  27. Mat jpegimage = imdecode(Mat(buff),CV_LOAD_IMAGE_COLOR);

  28.  
  29. //(2) png compression

  30. param[0]=CV_IMWRITE_PNG_COMPRESSION;

  31. param[1]=3;//default(3) 0-9.

  32. imencode(".png",src,buff,param);

  33. cout<<"coded file size(png): "<

  34. Mat pngimage = imdecode(Mat(buff),CV_LOAD_IMAGE_COLOR);

  35.  
  36. //(3) intaractive jpeg compression

  37. char name[64];

  38. namedWindow("jpg");

  39. int q=95;

  40. createTrackbar("quality","jpg",&q,100);

  41. int key = 0;

  42. while(key!='q')

  43. {

  44. param[0]=CV_IMWRITE_JPEG_QUALITY;

  45. param[1]=q;

  46. imencode(".jpg",src,buff,param);

  47. Mat show = imdecode(Mat(buff),CV_LOAD_IMAGE_COLOR);

  48.  
  49. double psnr = getPSNR(src,show);//get PSNR

  50. double bpp = 8.0*buff.size()/(show.size().area());//bit/pixe;

  51. sprintf(name,"quality:%03d, %.1fdB, %.2fbpp",q,psnr,bpp);

  52. putText(show,name,Point(15,50), FONT_HERSHEY_SIMPLEX,1,CV_RGB(255,255,255),2);

  53. imshow("jpg",show);

  54. key = waitKey(33);

  55.  
  56. if(key =='s')

  57. {

  58. //(4) data writing

  59. sprintf(name,"q%03d_%.2fbpp.png",q,bpp);

  60. imwrite(name,show);

  61.  
  62. sprintf(name,"q%03d_%.2fbpp.jpg",q,bpp);

  63. param[0]=CV_IMWRITE_JPEG_QUALITY;

  64. param[1]=q;

  65. imwrite(name,src,param);;

  66. }

  67. }

  68. }

  69. double getPSNR(Mat& src1, Mat& src2, int bb)

  70. {

  71. int i,j;

  72. double sse,mse,psnr;

  73. sse = 0.0;

  74.  
  75. Mat s1,s2;

  76. cvtColor(src1,s1,CV_BGR2GRAY);

  77. cvtColor(src2,s2,CV_BGR2GRAY);

  78.  
  79. int count=0;

  80. for(j=bb;j

  81. {

  82. uchar* d=s1.ptr(j);

  83. uchar* s=s2.ptr(j);

  84.  
  85. for(i=bb;i

  86. {

  87. sse += ((d[i] - s[i])*(d[i] - s[i]));

  88. count++;

  89. }

  90. }

  91. if(sse == 0.0 || count==0)

  92. {

  93. return 0;

  94. }

  95. else

  96. {

  97. mse =sse /(double)(count);

  98. psnr = 10.0*log10((255*255)/mse);

  99. return psnr;

  100. }

  101. }

使用的图片为lena.png:

 

OpenCV Jpeg编解码_第1张图片

 

输出的信息:

OpenCV Jpeg编解码_第2张图片

你可能感兴趣的:(opencv)