openCV学习笔记(七) —— 图像对比度和亮度调整、通道分离与合并

一、对比度和亮度调整

/*
	对比度亮度调整
*/

#include 
#include 

using namespace cv;
using namespace std;

#define WIN_NAME	"输出图像"

Mat src, dst;
int contrast = 100, bright = 100;

void onChange(int pos, void * data)
{
	for (int i = 0; i < src.rows; i++)
	{
		for (int j = 0; j < src.cols; j++)
		{
			dst.at(i, j)[0] = saturate_cast(src.at(i, j)[0] * contrast*0.01 + bright);
			dst.at(i, j)[1] = saturate_cast(src.at(i, j)[1] * contrast*0.01 + bright);
			dst.at(i, j)[2] = saturate_cast(src.at(i, j)[2] * contrast*0.01 + bright);
		}
	}

	imshow(WIN_NAME, dst);
}

int main()
{
	src = imread("test.jpg");
	if (src.empty())
	{
		printf("can't open picture\n");
		return -1;
	}
	imshow("原图", src);

	dst = Mat::zeros(src.size(), src.type());

	namedWindow(WIN_NAME, CV_WINDOW_AUTOSIZE);

	createTrackbar("对比度", WIN_NAME, &contrast, 300, onChange, 0);
	createTrackbar("亮 度", WIN_NAME, &bright, 200, onChange, 0);

	waitKey(0);
	destroyAllWindows();

	return 0;
}

 

openCV学习笔记(七) —— 图像对比度和亮度调整、通道分离与合并_第1张图片

 

二、通道分离与合并

#include
#include 

using namespace std;
using namespace cv;

int main() 
{
	Mat img = imread("test.jpg");
	Mat dst;
	vector channels;//定义Mat类型的向量

	split(img, channels);//通道分离
	Mat blue = channels.at(0);
	Mat green = channels.at(1);
	Mat red = channels.at(2);

	//threshold(blue, blue, 200, 255, THRESH_BINARY);//二值化:大于200的赋值255,小于200的赋值0
	//threshold(green, green, 200, 255, THRESH_BINARY);
	//threshold(red, red, 200, 255, THRESH_BINARY);

	merge(channels, dst);//通道合并

	imshow("原图", img);
	imshow("blue", blue);
	imshow("green", green);
	imshow("red", red);
	imshow("dst", dst);
	waitKey(0);

	destroyAllWindows();
}

openCV学习笔记(七) —— 图像对比度和亮度调整、通道分离与合并_第2张图片

你可能感兴趣的:(openCV)