<pre name="code" class="cpp">//
// main.cpp
// hw01
// use OPENCV IplImage type to load and bright image.
//
// Created by 李大龙 on 15/3/16.
// Copyright (c) 2015年 李大龙. All rights reserved.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <fstream>
using namespace std;
//图片色彩明亮化:创建颜色查询列表
int a[256];
void brightness(int brightness_number){
for (int i = 0; i<256; i++) {
if (i + brightness_number < 256) {
a[i] = i + 50;
}else a[i] = 255;
}
}
int main(int argc, const char * argv[])
{
//get the image from the directed path
IplImage* img = cvLoadImage("20131007102558_yYSNe.jpg", 1);
brightness(70);
IplImage *imgBuf = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3); //通道为3
for (int i = 0; i < img->height; i++)
{
for(int j = 0; j < img->width; j++)
{
int b = ((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 0]; //改变该像素B的颜色分量
int g = ((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 1]; //改变该像素G的颜色分量
int r = ((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 2]; //改变该像素R的颜色分量
((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 0] = a[b];
((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 1] = a[g];
((uchar *)(img->imageData + i * img->widthStep ))[j * img->nChannels + 2] = a[r];
}
}
cvSaveImage("test1.jpg", img);
cvNamedWindow("picture",1);
cvShowImage("picture", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("picture");
return 0;
}