图像旋转,对比度变化,亮度变化

实现将图像旋转、图像对比度变化、图像亮度变化归一到同一个接口下,可以通过函数替换,来决定对图像变换操作(使用了OPENCV2.1)

#include "cv.h"
#include "highgui.h"
#include "math.h"
#include 
   
   
    
    

//图像旋转
void ImageRotate(IplImage *src, IplImage * dest, float angle){
	assert(src != NULL);
	assert(dest != NULL);
	float m[6];
	CvMat M = cvMat(2, 3, CV_32F, m);
	int w = src->width;
	int h = src->height;
	m[0] = (float)(cos(-angle * CV_PI / 180.));
	m[1] = (float)(sin(-angle * CV_PI / 180.));
	m[3] = -m[1];
	m[4] = m[0];
	// 将旋转中心移至图像中
	m[2] = w*0.5f;
	m[5] = h*0.5f;
	cvGetQuadrangleSubPix(src, dest, &M);
}

//图像亮度变化
void ImageBrightAdjust(IplImage *src, IplImage *dest, float beta){
	assert(src != NULL);
	assert(dest != NULL);
	int x, y, i;
	if (src->nChannels == 3)
	{
		for (i = 0; i < 3; i++)//彩色图像需要处理3个通道,灰度图像这里可以删掉   
		for (y = 0; y < src->height; y++)
		for (x = 0; x < src->width; x++)
		{
			float val = ((uchar*)(src->imageData + src->widthStep*y))[x * 3 + i];
			val += beta;
			if (val<0)    val = 0;
			else if (val>255) val = 255;
			((uchar*)(dest->imageData + dest->widthStep*y))[x * 3 + i] = (uchar)val;
		}
	}
	else
	{
		for (y = 0; y < src->height; y++)
		for (x = 0; x < src->width; x++)
		{
			float val = ((uchar*)(src->imageData + src->widthStep*y))[x];
			val += beta;
			if (val<0)    val = 0;
			else if (val>255) val = 255;
			((uchar*)(dest->imageData + dest->widthStep*y))[x] = (uchar)val;
		}
	}
}

//图像对比度变化
void ImageContrastAdjust(IplImage *src, IplImage *dest, float alpha){
	assert(src != NULL);
	assert(dest != NULL);
	int x, y, i;
	if (src->nChannels == 3)
	{
		for (i = 0; i < 3; i++)//彩色图像需要处理3个通道,灰度图像这里可以删掉   
		for (y = 0; y < src->height; y++)
		for (x = 0; x < src->width; x++)
		{
			float val = ((uchar*)(src->imageData + src->widthStep*y))[x * 3 + i];
			//val += alpha;
			val *= alpha;
			if (val<0)    val = 0;
			else if (val>255) val = 255;
			((uchar*)(dest->imageData + dest->widthStep*y))[x * 3 + i] = (uchar)val;
		}
	}
	else
	{
		for (y = 0; y < src->height; y++)
		for (x = 0; x < src->width; x++)
		{
			float val = ((uchar*)(src->imageData + src->widthStep*y))[x];
			val *= alpha;
			if (val<0)    val = 0;
			else if (val>255) val = 255;
			((uchar*)(dest->imageData + dest->widthStep*y))[x] = (uchar)val;
		}
	}
}

void DoNothing(IplImage *src,IplImage *dest, float t){

}

void Traverse(const char * floderName, void(*f)(IplImage *src, IplImage *dest, float t), float argument){
	IplImage *src = cvLoadImage(floderName, CV_LOAD_IMAGE_COLOR);
	IplImage *dest = cvCloneImage(src);
	f(src, dest, argument);
	cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
	cvShowImage("test", dest);
	cvReleaseImage(&src);
	cvReleaseImage(&dest);
}

static void help(){
	printf("need a image path as argument!");
}

int main(int argc, char * argv[]){
	if (argc != 2){
		help();
		return 0;
	}

	for (int i = -30; i < 30; ++i)
	{
		Traverse(argv[1], ImageRotate, static_cast
    
    
     
     (i));
		if (cvWaitKey(30) == 27)
			break;
	}

	for (float i = -150; i < 150; ++i)
	{
		Traverse(argv[1], ImageBrightAdjust, i);
		if (cvWaitKey(30) == 27)
			break;
	}

	for (float i = 0; i < 3.0; i += 0.01f)
	{
		Traverse(argv[1], ImageContrastAdjust, i);
		if (cvWaitKey(30) == 27)
			break;
	}

	return 0;
}

    
    
   
   

运行效果

原图:

旋转:

亮度变化:

对比度变化:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值