当前位置:   article > 正文

opencv 使较暗的物体变亮_opencv 黑夜变白天

opencv 黑夜变白天

测试图片:

 

code:

方案1:

  1. #include "cv.h"
  2. #include "highgui.h"
  3. #define HDIM 256 // bin of HIST, default = 256
  4. int main( int argc, char** argv )
  5. {
  6. IplImage *src = 0, *dst = 0;
  7. CvHistogram *hist = 0;
  8. int n = HDIM;
  9. double nn[HDIM];
  10. uchar T[HDIM];
  11. CvMat *T_mat;
  12. int x;
  13. int sum = 0; // sum of pixels of the source image 图像中象素点的总和
  14. double val = 0;
  15. src=cvLoadImage("1.jpg", 0);
  16. cvNamedWindow( "source", 1 );
  17. cvNamedWindow( "result", 1 );
  18. // calculate histgram 计算直方图
  19. hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 );
  20. cvCalcHist( &src, hist, 0, 0 );
  21. // Create Accumulative Distribute Function of histgram
  22. val = 0;
  23. for ( x = 0; x < n; x++)
  24. {
  25. val = val + cvGetReal1D (hist->bins, x);
  26. nn[x] = val;
  27. }
  28. // Compute intensity transformation 计算变换函数的离散形式
  29. sum = src->height * src->width;
  30. for( x = 0; x < n; x++ )
  31. {
  32. T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255]
  33. }
  34. // Do intensity transform for source image
  35. dst = cvCloneImage( src );
  36. T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
  37. cvSetData( T_mat, T, 0 );
  38. // directly use look-up-table function 直接调用内部函数完成 look-up-table 的过程
  39. cvLUT( src, dst, T_mat );
  40. int y;
  41. for(y=0;y<dst->height;y++)
  42. {
  43. for(x=0;x<dst->width;x++)
  44. {
  45. int dstV,srcV;
  46. dstV = cvGetReal2D(dst,y,x);
  47. srcV = cvGetReal2D(src,y,x);
  48. int newV ;
  49. newV = (srcV*0.7+dstV*0.4);
  50. newV = newV > 255 ? 255 : newV;
  51. cvSetReal2D(dst,y,x,newV);
  52. }
  53. }
  54. cvShowImage( "source", src );
  55. cvShowImage( "result", dst );
  56. cvWaitKey(0);
  57. cvDestroyWindow("source");
  58. cvDestroyWindow("result");
  59. cvReleaseImage( &src );
  60. cvReleaseImage( &dst );
  61. cvReleaseHist ( &hist );
  62. return 0;
  63. }

效果:

方案2:

  1. #include "stdafx.h"
  2. #include "cv.h"
  3. #include "cxcore.h"
  4. #include "highgui.h"
  5. int main(int argc, char* argv[])
  6. {
  7. IplImage* src;
  8. src = cvLoadImage("1.jpg");
  9. IplImage* srcSmooth;
  10. srcSmooth= cvCreateImage(cvGetSize(src),8,3);
  11. cvSmooth(src,srcSmooth,CV_GAUSSIAN,35,35,0,0);
  12. IplImage* srcGray=cvCreateImage(cvGetSize(src),8,1);
  13. cvCvtColor(srcSmooth,srcGray,CV_BGR2GRAY);
  14. CvMat* points = cvCreateMat(srcGray->height*srcGray->width , 1 , CV_32FC1);
  15. CvMat* clusters= cvCreateMat(srcGray->height*srcGray->width , 1 , CV_32SC1);
  16. uchar* image_data = (uchar*)srcGray->imageData;
  17. int width_step = srcGray->widthStep;
  18. int x,y;
  19. for(y=0;y<src->height;y++)
  20. {
  21. for(x=0;x<src->width;x++)
  22. {
  23. points->data.fl[y*srcGray->width+x] = (float)(*(image_data + y*width_step + x));
  24. }
  25. }
  26. cvKMeans2(points,3,clusters ,cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,10,1.0));
  27. float cluster_0_count=0,cluster_1_count=0;
  28. float sum_cluster_0 =0 , sum_cluster_1 = 0;
  29. for(y=0;y<srcGray->height;y++)
  30. {
  31. for(x=0;x<srcGray->width;x++)
  32. {
  33. int cluster_id = clusters->data.i[y*srcGray->width+x];
  34. if(cluster_id == 0 || cluster_id==2)
  35. {
  36. sum_cluster_0 = sum_cluster_0 + (float)(*(image_data + y*width_step + x));
  37. cluster_0_count = cluster_0_count + 1;
  38. *(image_data + y*width_step + x) = 255;
  39. }
  40. else if(cluster_id == 1 )
  41. {
  42. sum_cluster_1 = sum_cluster_1 + (float)(*(image_data + y*width_step + x));
  43. *(image_data + y*width_step + x) = (int)cvGetReal2D(srcGray,y,x);
  44. }
  45. else
  46. {
  47. }
  48. }
  49. }
  50. cvNamedWindow("src");
  51. cvShowImage("src",src);
  52. cvNamedWindow("s");
  53. cvShowImage("s",srcSmooth);
  54. cvNamedWindow("g");
  55. cvShowImage("g",srcGray);
  56. cvWaitKey(0);
  57. return 0;
  58. }

 

效果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/597877
推荐阅读
相关标签
  

闽ICP备14008679号