`
javasogo
  • 浏览: 1775286 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

图像亮度、对比度调节

阅读更多
  1. #include"cv.h"
  2. #include"highgui.h"
  3. #include<stdio.h>
  4. intImageAdjust(IplImage*src,IplImage*dst,
  5. doublelow,doublehigh,//xdirection
  6. doublebottom,doubletop,//ydirection
  7. doublegamma);
  8. intmain(void)
  9. {
  10. char*filename="lena.jpg";
  11. IplImage*dst,*src=/blog.armyourlife.info/cvLoadImage(filename,0);
  12. if(!src)
  13. {
  14. printf("Couldn'tseemtoOpen%s,sorry\n",filename);
  15. return-1;
  16. }
  17. cvNamedWindow("src",1);
  18. cvNamedWindow("result",1);
  19. //Imageadjust
  20. dst=cvCloneImage(src);
  21. //输入参数[0,0.5]和[0.5,1],gamma=1
  22. if(ImageAdjust(src,dst,0,0.5,0.5,1,1)!=0)
  23. return-1;
  24. cvShowImage("src",src);
  25. cvShowImage("result",dst);
  26. cvWaitKey(0);
  27. cvDestroyWindow("src");
  28. cvDestroyWindow("result");
  29. cvReleaseImage(&src);
  30. cvReleaseImage(&dst);
  31. return0;
  32. }
  33. intImageAdjust(IplImage*src,IplImage*dst,
  34. doublelow,doublehigh,//X方向:lowandhigharetheintensitiesofsrc
  35. doublebottom,doubletop,//Y方向:mappedtobottomandtopofdst
  36. doublegamma)
  37. {
  38. doublelow2=low*255;
  39. doublehigh2=high*255;
  40. doublebottom2=bottom*255;
  41. doubletop2=top*255;
  42. doubleerr_in=high2-low2;
  43. doubleerr_out=top2-bottom2;
  44. intx,y;
  45. doubleval;
  46. if(low<0&&low>1&&high<0&&high>1&&
  47. bottom<0&&bottom>1&&top<0&&top>1&&low>high)
  48. return-1;
  49. //intensitytransform
  50. for(y=0;y<src->height;y++)
  51. {
  52. for(x=0;x<src->width;x++)
  53. {
  54. val=((uchar*)(src->imageData+src->widthStep*y))[x];
  55. val=pow((val-low2)/err_in,gamma)*err_out+bottom2;
  56. if(val>255)
  57. val=255;
  58. if(val<0)
  59. val=0;//Makesuresrcisintherange[low,high]
  60. ((uchar*)(dst->imageData+dst->widthStep*y))[x]=(uchar)val;
  61. }
  62. }
  63. return0;
  64. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics