您的位置:首页 > 其它

调节亮度、对比度及gamma值实现彩色图像的亮度变换

2010-10-31 09:45 585 查看
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
int ImageAdjust(IplImage* src, IplImage* dst,
double low, double high,   // X方向:low and high are the intensities of src
double bottom, double top, // Y方向:mapped to bottom and top of dst
double gamma )
{
double low2 = low*255;
double high2 = high*255;
double bottom2 = bottom*255;
double top2 = top*255;
double err_in = high2 - low2;
double err_out = top2 - bottom2;
int x,y;
double val;
if(low<0 && low>1 && high <0 && high>1&&
bottom<0 && bottom>1 && top<0 && top>1 && low>high)
return -1;
// intensity transform
for( y = 0; y < src->height; y++)
{
for (x = 0; x < src->width; x++)
{
val = ((uchar*)(src->imageData + src->widthStep*y))[x];
val=pow((val - low2)/err_in, gamma)*err_out+bottom2;
if(val>255)
val=255;
if(val<0)
val=0; // Make sure src is in the range [low,high]
((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val;
}
}
return 0;
}
void main()
{
int i;
IplImage *pImageChannel[4] = { 0, 0, 0, 0 };
IplImage* pSrcImage = cvLoadImage( "WeiShengZhiHuiZhongXin_Re-000000_00000000_00001465_00000119.jpg", 1 ) ;
IplImage *pImage = cvCreateImage(cvGetSize(pSrcImage), pSrcImage->depth, pSrcImage->nChannels);
if( pSrcImage )
{
for( i = 0; i < pSrcImage->nChannels; i++ )
{
pImageChannel[i] = cvCreateImage( cvGetSize(pSrcImage), pSrcImage->depth, 1 );
}
// 信道分离
cvSplit( pSrcImage, pImageChannel[0], pImageChannel[1],
pImageChannel[2], pImageChannel[3] );
for( i = 0; i < pImage->nChannels; i++ )
{
// 直方图均衡化
//cvEqualizeHist( pImageChannel[i], pImageChannel[i] );
// 输入参数 [0,0.5] 和 [0.5,1], gamma=1
ImageAdjust( pImageChannel[i], pImageChannel[i], 0, 0.8, 0, 1, 1.2);
}
// 信道组合
cvMerge( pImageChannel[0], pImageChannel[1], pImageChannel[2],
pImageChannel[3], pImage );
cvSaveImage("adjustImage.jpg", pImage);
// ……图像显示代码(略)
// 释放资源
for( i = 0; i < pSrcImage->nChannels; i++ )
{
if ( pImageChannel[i] )
{
cvReleaseImage( &pImageChannel[i] );
pImageChannel[i] = 0;
}
}
cvReleaseImage( &pImage );
pImage = 0;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: