本文主要是介绍opencv 手动调整照片颜色小工具 对比度 亮度 BGR 各通道,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、运行界面
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <string.h>
#define INITBGR 30//-----------------------------------【命名空间声明部分】---------------------------------------
// 描述:包含程序所使用的命名空间
//-----------------------------------------------------------------------------------------------
using namespace std;
using namespace cv; //-----------------------------------【全局函数声明部分】--------------------------------------
// 描述:全局函数声明
//-----------------------------------------------------------------------------------------------
static void ContrastAndBright(int, void *);
static void ContrastAndBrightb(int, void *);
static void ContrastAndBrightg(int, void *);
static void ContrastAndBrightr(int, void *); //-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量声明
//-----------------------------------------------------------------------------------------------
int g_nContrastValue; //对比度值
int g_nBrightValue; //亮度值
int g_b;
int g_g;
int g_r;
Mat g_srcImage,g_dstImage;
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{ if (2 != argc) {cout << "Please enter the image list!" <<endl;return -1;}vector<string> file_names;FILE *file_list = fopen(argv[1],"r");char buf[255];memset(&buf,0,sizeof(buf));while(fgets(buf,255,file_list)){if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';file_names.push_back(string(buf));}fclose(file_list);int count = file_names.size();for(int i=0; i<count; i++){string img_nm = file_names[i];string img_new = "new" + img_nm;int pos = img_nm.rfind('.');string img_fmt = img_nm.substr(pos+1);if("jpg" != img_fmt){cout << "Unknown format: " << img_fmt << endl;continue;}cout << "read a image" << endl;//改变控制台前景色和背景色 system("color5F"); //读入用户提供的图像 g_srcImage= imread(img_nm); if(!g_srcImage.data ) { printf("Oh,no,读取g_srcImage图片错误~!\n"); return false;} g_dstImage= Mat::zeros( g_srcImage.size(), g_srcImage.type() ); //设定对比度和亮度的初值 g_nContrastValue=80; g_nBrightValue=50; g_b = 0;g_g = 0;g_r = 0;//创建窗口 namedWindow("【效果图窗口】", 0); //创建轨迹条 createTrackbar("对比度:", "【效果图窗口】",&g_nContrastValue,300,ContrastAndBright ); createTrackbar("亮 度:","【效果图窗口】",&g_nBrightValue,100,ContrastAndBright ); createTrackbar("B:","【效果图窗口】",&g_b,200,ContrastAndBrightb ); createTrackbar("G:","【效果图窗口】",&g_g,200,ContrastAndBrightg); createTrackbar("R:","【效果图窗口】",&g_r,200,ContrastAndBrightr ); //调用回调函数 ContrastAndBright(g_nContrastValue,0); ContrastAndBright(g_nBrightValue,0); ContrastAndBrightb(g_b,0); ContrastAndBrightg(g_g,0); ContrastAndBrightr(g_r,0); //输出一些帮助信息 cout<<endl<<"Press the q button, the image exits and save,and next will start now!!" <<endl; //按下“q”键时,程序退出 while(char(waitKey(1)) != 'q') {} imwrite(img_new,g_dstImage);}return 0;
} //-----------------------------【ContrastAndBright( )函数】------------------------------------
// 描述:改变图像对比度和亮度值的回调函数
//-----------------------------------------------------------------------------------------------
static void ContrastAndBright(int, void *)
{ //创建窗口 namedWindow("【原始图窗口】", 0); //三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b for(int y = 0; y < g_srcImage.rows; y++ ) { for(int x = 0; x < g_srcImage.cols; x++ ) { g_dstImage.at<Vec3b>(y,x)[0]= saturate_cast<uchar>( (g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y,x)[0] ) + g_nBrightValue+g_b); g_dstImage.at<Vec3b>(y,x)[1]= saturate_cast<uchar>( (g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y,x)[1] ) + g_nBrightValue+g_g); g_dstImage.at<Vec3b>(y,x)[2]= saturate_cast<uchar>( (g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y,x)[2] ) + g_nBrightValue+g_r); } } //显示图像 imshow("【原始图窗口】", g_srcImage); imshow("【效果图窗口】", g_dstImage);
} static void ContrastAndBrightb(int, void *)
{ //创建窗口 namedWindow("【原始图窗口】", 0); g_b=g_b-INITBGR;//三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b for(int y = 0; y < g_srcImage.rows; y++ ) { for(int x = 0; x < g_srcImage.cols; x++ ) { g_dstImage.at<Vec3b>(y,x)[0]= saturate_cast<uchar>( (g_nContrastValue*0.01)*g_srcImage.at<Vec3b>(y,x)[0] + g_nBrightValue + g_b); } } //显示图像 imshow("【原始图窗口】", g_srcImage); imshow("【效果图窗口】", g_dstImage);
} static void ContrastAndBrightg(int, void *)
{ //创建窗口 namedWindow("【原始图窗口】", 0); g_g=g_g-INITBGR;//三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b for(int y = 0; y < g_srcImage.rows; y++ ) { for(int x = 0; x < g_srcImage.cols; x++ ) { g_dstImage.at<Vec3b>(y,x)[1]= saturate_cast<uchar>( (g_nContrastValue*0.01)*g_srcImage.at<Vec3b>(y,x)[1] + g_nBrightValue + g_g); } } //显示图像 imshow("【原始图窗口】", g_srcImage); imshow("【效果图窗口】", g_dstImage);
} static void ContrastAndBrightr(int, void *)
{ //创建窗口 namedWindow("【原始图窗口】", 0); g_r=g_r-INITBGR;//三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b for(int y = 0; y < g_srcImage.rows; y++ ) { for(int x = 0; x < g_srcImage.cols; x++ ) { g_dstImage.at<Vec3b>(y,x)[2]= saturate_cast<uchar>( (g_nContrastValue*0.01)*g_srcImage.at<Vec3b>(y,x)[2] + g_nBrightValue + g_r); } } //显示图像 imshow("【原始图窗口】", g_srcImage); imshow("【效果图窗口】", g_dstImage);
}
说明:
输入是一个包含照片名字的文本文档,调整完后按q自动加载另一张照片以及保存打开的照片。
程序运行速度比较慢,有待提高。
这篇关于opencv 手动调整照片颜色小工具 对比度 亮度 BGR 各通道的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!