OpenCV learning: basic image operation (3): contrast and brightness adjustment, image fusion

Picture contrast and brightness

In the image pixel formula g(x)=a*f(x)+b:

  • The parameter f(x) represents the source image pixel.
  • The parameter g(x) represents the output image pixel.
  • The parameter a (need to satisfy a>0) is called gain and is often used to control the contrast of the image.
  • The parameter b is often called bias and is often used to control the brightness of the image.

Picture fusion

Significance: Associate the information in multiple pictures so that a single picture contains more information elements to facilitate subsequent control and processing.

Method: Based on deep learning: use GAN network generation, use codec network generation, use feature map fusion technology, etc.

           Based on image processing: pixel value calculation, RANSAC stitching, etc.

This article mainly introduces the pixel-based method. The most intuitive idea is to associate the fused image pixels with the pixel values ​​of the two pictures . The simple way is to perform calculations on the pixel values ​​of the two images, such as multiplying, adding, etc. .

API introduction

addWeighted(src1, alpha, src2, beta, gamma, dst) implements the following operations at the pixel level:

D (x, y) = S_1 (x, y) * \ alpha + S_2 (x, y) * \ beta + \ gamma

In addition, there are common operations add, mutiply, sub, etc.;

add(src1,src2,dst,mask,data_type)

Among them, mask is the area where the operation is performed

Code practice

Contrast and brightness

#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include"opencv2/imgproc/imgproc.hpp"
#include <iostream>



using namespace cv;

static void ContrastAndBright(int, void*);

int g_nContrastValue; //对比度值
int g_nBrightValue;  //亮度值
Mat g_srcImage, g_dstImage;


int main()

{

	//读入用户提供的图像

	g_srcImage = imread("src.jpg");
	if (!g_srcImage.data) { printf("读取图片失败!\n"); return false
	g_dstImage = Mat::zeros(g_srcImage.size(), g_srcImage.type());



	//设定对比度和亮度的初值
	g_nContrastValue = 80;
	g_nBrightValue = 80;



	//创建窗口
	namedWindow("结果", 1);



	//创建轨迹条
	createTrackbar("对比度:", "结果", &g_nContrastValue, 300, ContrastAndBright);
	createTrackbar("亮   度:", "结果", &g_nBrightValue, 200, ContrastAndBright);



	//调用回调函数

	ContrastAndBright(g_nContrastValue, 0);
	ContrastAndBright(g_nBrightValue, 0);



	//按下“q”键时,程序
	while (char(waitKey(1)) != 'q') {}
	return 0;

}


static void ContrastAndBright(int, void*)
{



	//创建窗口

	namedWindow("原图", 1);



	//三个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++)
		{ 
			for (int c = 0; c < 3; c++)
			{
				g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue * 0.01) * (g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
			}
		}
	}



	//显示图像
	imshow("原图", g_srcImage);
	imshow("结果", g_dstImage);

}

Image fusion

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{

	Mat src = imread("src.jpg");
	if (src.empty())
	{
		cout << "could not open image ..." << endl;
		return -1;
	}

	Mat back = imread("back.jpg");
	if (!back.data) //用data来判断MAT是否为空
	{
		cout << "could not open image ..." << endl;
		return -1;
	}

	resize(back, back, src.size());
	

	float alpha = 0.5;

	Mat dst ;
	if (src.size() == back.size() && src.type() == back.type())
	{
		addWeighted(src, alpha, back, 0.3, 10.0, dst);	
		//add(src, back, dst);
		//multiply(src, back, dst);
		imwrite("add.jpg", dst);
	}
	return 0;
}

                                              Multiply add

                                               Weight addition

Guess you like

Origin blog.csdn.net/fan1102958151/article/details/106965381