1#include "cv.h" 2 3#include "highgui.h" 4 5#include <stdio.h> 6 7int ImageAdjust(IplImage *src,IplImage *dst, 8 9 double low,double high, //x direction 10 11 double bottom,double top, //y direction 12 13 double gamma); 14 15int main(void) 16 17{ 18 19 char *filename="lena.jpg"; 20 21 IplImage *dst,*src = /blog.armyourlife.info/cvLoadImage(filename,0); 22 23 if(!src) 24 25 { 26 27 printf("Couldn't seem to Open %s, sorry/n",filename); 28 29 return -1; 30 31 } 32 33 34 35 cvNamedWindow( "src", 1 ); 36 37 cvNamedWindow( "result", 1 ); 38 39 40 41 // Image adjust 42 43 dst = cvCloneImage(src); 44 45 // 输入参数 [0,0.5] 和 [0.5,1], gamma=1 46 47 if( ImageAdjust( src, dst, 0, 0.5, 0.5, 1, 1)!=0) 48 49 return -1; 50 51 52 53 cvShowImage( "src", src ); 54 55 cvShowImage( "result", dst ); 56 57 cvWaitKey(0); 58 59 cvDestroyWindow("src"); 60 61 cvDestroyWindow("result"); 62 63 cvReleaseImage( &src ); 64 65 cvReleaseImage( &dst ); 66 67 68 69 return 0; 70 71} 72 73int ImageAdjust(IplImage* src, IplImage* dst, 74 75 double low, double high, // X方向:low and high are the intensities of src 76 77 double bottom, double top, // Y方向:mapped to bottom and top of dst 78 79 double gamma ) 80 81{ 82 83 double low2 = low*255; 84 85 double high2 = high*255; 86 87 double bottom2 = bottom*255; 88 89 double top2 = top*255; 90 91 double err_in = high2 - low2; 92 93 double err_out = top2 - bottom2; 94 95 int x,y; 96 97 double val; 98 99 if(low<0 && low>1 && high <0 && high>1&& 100 101 bottom<0 && bottom>1 && top<0 && top>1 && low>high) 102 103 return -1; 104 105 // intensity transform 106 107 for( y = 0; y < src->height; y++) 108 109 { 110 111 for (x = 0; x < src->width; x++) 112 113 { 114 115 val = ((uchar*)(src->imageData + src->widthStep*y))[x]; 116 117 val=pow((val - low2)/err_in, gamma)*err_out+bottom2; 118 119 if(val>255) 120 121 val=255; 122 123 if(val<0) 124 125 val=0; // Make sure src is in the range [low,high] 126 127 ((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val; 128 129 } 130 131 } 132 133 return 0; 134 135}
OpenCV图像亮度、对比度调节
Wesley13
2021-10-11
1033 1 0
点赞
收藏
评论区
加载中...