关于opencv的小作业存档
以下是实现opencv两个小例子的报告(本程序基于vs2017以及opencv4.0):
(1)调整图像对比度和亮度
#include "opencv2/highgui.hpp"
#include"opencv2/imgcodecs.hpp"
#include <iostream>
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;
using namespace cv;
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, "{@input | lena.jpg | input image}");
Mat image = imread(samples::findFile(parser.get<String>("@input"))); //从opencv安装路径(D:\opencv\opencv\sources\samples\data)用findfile命令读取sample下data文件夹目录名字为lena.jpg的图片,也可以imread(“绝对文件路径”)例如imread("C:/Users/Administrator/Desktop/name.jpg")
if (image.empty())
{
cout << "Could not open or find the image!\n" << endl; //判断读取的图形是否为空,为空则返回-1;即退出。
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
Mat new_image = Mat::zeros(image.size(), image.type()); //定义新容器以存储变更亮度的图片
double alpha = 1.0; //定义对比度系数
int beta = 255; //定义灰度系数
cout << " 请输入基本线性变换参数 " << endl;
cout << "-------------------------" << endl;
cout << "* 输入alpha的值 [1.0-3.0]: "; cin >> alpha;
cout << "* 输入beta的值 [0-100]: "; cin >> beta;
for (int y = 0; y < image.rows; y++) {
for (int x = 0; x < image.cols; x++) {
for (int c = 0; c < image.channels(); c++) {
new_image.at<Vec3b>(y, x)[c] =
saturate_cast<uchar>(alpha*image.at<Vec3b>(y, x)[c] + beta); //三个for循环依次对图片的行,列,频道像素进行变换,
} // saturate_cast<uchar>的作用是防止像素溢出,原理如下:
/* if (data < 0)
data = 0;
else if(data > 255)
data = 255; */
}
}
imshow("Original Image", image);
imshow("New Image", new_image); //显示新旧对比图像。
waitKey();
return 0;
}
效果如下:
(2)把两张图片按权重混合为一(两张图片务必大小相等,长宽像素点相同) blend two image to one
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
double alpha = 0.5; double beta; double input; //定义参数
Mat src1, src2, dst; //定义三个存储图片的容器
cout << " Simple Linear Blender " << endl;
cout << "-----------------------" << endl;
cout << "* Enter alpha [0.0-1.0]: ";
cin >> input;
// We use the alpha provided by the user if it is between 0 and 1
if (input >= 0 && input <= 1)
{
alpha = input; //输入权重
}
src1 = imread(samples::findFile("LinuxLogo.jpg")); //从sample/data目录下加载linuxlogo.jpg文件
src2 = imread(samples::findFile("WindowsLogo.jpg"));
if (src1.empty()) { cout << "Error loading src1" << endl; return EXIT_FAILURE; } //判断两者为空否
if (src2.empty()) { cout << "Error loading src2" << endl; return EXIT_FAILURE; }
beta = (1.0 - alpha);
addWeighted(src1, alpha, src2, beta, 0.0, dst); //调用addweighted函数对两张图片进行混合,addWeighted()函数是将两张相同大小,相同类型的图片融合的函数
/*API详解:void cvAddWeighted( const CvArr* src1, double alpha,const CvArr* src2, double beta,double gamma, CvArr* dst );
参数1:src1,第一个原数组. 并且铺在上层
参数2:alpha,第一个数组元素权重
参数3:src2第二个原数组
参数4:beta,第二个数组元素权重
参数5:gamma,图1与图2作和后添加的数值。不要太大,不然图片一片白。总和等于255以上就是纯白色了。
参数6:dst,输出图片*/
imshow("windows", src1);
imshow("linux", src2);
imshow("Linear Blend", dst); //输出新图片
waitKey(0);
return 0;
}
效果图如下: