本实验为计算机科学与技术学院计算机专业大四上限选课,2023-2024-1年度课程实验,较以往实验内容发生较大变化
代码中乱码系中文编码错误,可自行更换。本实验使用vs2019,c++语言,需要提前安装opencv,具体方法请自行搜索。
实验1:图像基本处理
实验1.1:图像合成
给定一张4通道透明图像a.png,从中提取alpha通道并显示,并用alpha混合,为a.png替换一张新的背景(背景图自选)
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// ��ͼ
Mat picture = imread("C:/Users/13441/Desktop/����ͼ��/a.png", -1);
Mat picture_alpha(picture.rows, picture.cols, CV_8UC1);
int i = 0, j = 0;
while (i < picture.rows)
{//�����ػ�ȡ�Աȶȣ��洢��һ��������
j = 0;
while (j < picture.cols)
{
picture_alpha.at<uchar>(i, j) = picture.at<Vec4b>(i, j)[3];
j++;
}
i++;
}
imshow("1.png", picture_alpha); // ��ʾAlphaֵ
waitKey();
//alpha���
Mat in = imread("C:/Users/13441/Desktop/����ͼ��/a.png", -1); //ԭʼͼƬ
Mat background = imread("C:/Users/13441/Desktop/����ͼ��/back.png", -1); //����ͼƬ
Mat output(in.rows, in.cols, CV_8UC4); //���ͼƬ
//cout << in.rows << " " << in.cols << endl;
// cout << background.rows << " " << background.cols << endl;
for (int i = 0; i < in.rows; i++)//�����ظ��ݱ����Dz��ǰ�ɫ�����ı���
{
for (int j = 0; j < in.cols; j++)
{
//cout << "get " << i << " " << j << endl;
double alpha = in.at<Vec4b>(i, j)[3] / 255.0; //����Աȶ����������Dz��ǰ�ɫ
// cout << "r " << i << " " << j <<" "<<alpha<< endl;
output.at<Vec4b>(i, j)[0] = (1 - alpha) * background.at<Vec4b>(i, j)[0] + alpha * in.at<Vec4b>(i, j)[0];
// cout << "g " << i << " " << j << endl;
output.at<Vec4b>(i, j)[1] = (1 - alpha) * background.at<Vec4b>(i, j)[1] + alpha * in.at<Vec4b>(i, j)[1];
//cout << "b " << i << " " << j << endl;
output.at<Vec4b>(i, j)[2] = (1 - alpha) * background.at<Vec4b>(i, j)[2] + alpha * in.at<Vec4b>(i, j)[2];
// cout << "alpha " << i << " " << j << endl;
output.at<Vec4b>(i, j)[3] = (1 - alpha) * background.at<Vec4b>(i, j)[3] + alpha * in.at<Vec4b>(i, j)[3];
// cout << i << " " << j << endl;
continue;
}
}
imshow("new_picture", output);
waitKey();
return 0;
}
实验1.2:对比度调整
设计一个Sigmoid函数,实现对象对比度调整,并使得调整幅度可通过参数控制。使用opencv窗口系统的slider控件,交互改变Sigmoid函数的参数,实现不同程度的对比度调整
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <time.h>
using namespace std;
using namespace cv;
int alpha = 50;
//ԭͼ����ͼ
Mat picture;
Mat new_picture;
void change_alpha(int, void*)
{
for (int i = 0; i < picture.rows; i++)
{
for (int j = 0; j < picture.cols; j++)
{
for (int k = 0; k < 3; k++)
{
double tmp = 255.0 / (1 + exp(-(alpha * 0.001 * 0.4) * (picture.at<Vec3b>(i, j)[k] - 127.5)));//�Աȶȼ���
new_picture.at<Vec3b>(i, j)[k] = saturate_cast<uchar>(tmp);
}
}
}
imshow("New Image", new_picture);
return;
}
int main()
{
picture = imread("C:/Users/13441/Desktop/����ͼ��/b.png");
new_picture = Mat::zeros(picture.size(), picture.type());
namedWindow("Slider", WINDOW_AUTOSIZE);
namedWindow("New Image", WINDOW_AUTOSIZE);
createTrackbar("�Աȶȣ�", "Slider", &alpha, 100, change_alpha);
change_alpha(alpha, 0);
waitKey(0);
return 0;
}
本文转载自: https://blog.csdn.net/qq_56549521/article/details/141018791
版权归原作者 αα 所有, 如有侵权,请联系我们删除。
版权归原作者 αα 所有, 如有侵权,请联系我们删除。