OpenCV Jpeg编解码

opencv 专栏收录该内容
2 篇文章 0 订阅

源码来自: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 <iostream>

  5. #include <fstream>

  6. #include <cv.h>

  7. #include <highgui.h>

  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: "<<src.dataend-src.datastart<<endl;

  17. cout<<"height: "<<src.rows<<endl<<"width: "<<src.cols<<endl<<"depth: "<<src.channels()<<endl;

  18. cout<<"height*width*depth: "<<src.rows*src.cols*src.channels()<<endl<<endl;

  19. //(1) jpeg compression

  20. vector<uchar> buff;//buffer for coding

  21. vector<int> param = vector<int>(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): "<<buff.size()<<endl;//fit buff size automatically.

  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): "<<buff.size()<<endl;

  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<s1.rows-bb;j++)

  81. {

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

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

  84.  
  85. for(i=bb;i<s1.cols-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:

 

 

输出的信息:

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值