0


山东大学数字图像处理实验(一) 计算机学院

本实验为计算机科学与技术学院计算机专业大四上限选课,2023-2024-1年度课程实验,较以往实验内容发生较大变化

代码中乱码系中文编码错误,可自行更换。本实验使用vs2019,c++语言,需要提前安装opencv,具体方法请自行搜索。

实验1:图像基本处理

实验1.1:图像合成

给定一张4通道透明图像a.png,从中提取alpha通道并显示,并用alpha混合,为a.png替换一张新的背景(背景图自选)

  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace cv;
  8. int main()
  9. {
  10. // ��ͼ
  11. Mat picture = imread("C:/Users/13441/Desktop/����ͼ��/a.png", -1);
  12. Mat picture_alpha(picture.rows, picture.cols, CV_8UC1);
  13. int i = 0, j = 0;
  14. while (i < picture.rows)
  15. {//�����ػ�ȡ�Աȶȣ��洢��һ��������
  16. j = 0;
  17. while (j < picture.cols)
  18. {
  19. picture_alpha.at<uchar>(i, j) = picture.at<Vec4b>(i, j)[3];
  20. j++;
  21. }
  22. i++;
  23. }
  24. imshow("1.png", picture_alpha); // ��ʾAlphaֵ
  25. waitKey();
  26. //alpha���
  27. Mat in = imread("C:/Users/13441/Desktop/����ͼ��/a.png", -1); //ԭʼͼƬ
  28. Mat background = imread("C:/Users/13441/Desktop/����ͼ��/back.png", -1); //����ͼƬ
  29. Mat output(in.rows, in.cols, CV_8UC4); //���ͼƬ
  30. //cout << in.rows << " " << in.cols << endl;
  31. // cout << background.rows << " " << background.cols << endl;
  32. for (int i = 0; i < in.rows; i++)//�����ظ��ݱ����Dz��ǰ�ɫ�����ı���
  33. {
  34. for (int j = 0; j < in.cols; j++)
  35. {
  36. //cout << "get " << i << " " << j << endl;
  37. double alpha = in.at<Vec4b>(i, j)[3] / 255.0; //����Աȶ����������Dz��ǰ�ɫ
  38. // cout << "r " << i << " " << j <<" "<<alpha<< endl;
  39. output.at<Vec4b>(i, j)[0] = (1 - alpha) * background.at<Vec4b>(i, j)[0] + alpha * in.at<Vec4b>(i, j)[0];
  40. // cout << "g " << i << " " << j << endl;
  41. output.at<Vec4b>(i, j)[1] = (1 - alpha) * background.at<Vec4b>(i, j)[1] + alpha * in.at<Vec4b>(i, j)[1];
  42. //cout << "b " << i << " " << j << endl;
  43. output.at<Vec4b>(i, j)[2] = (1 - alpha) * background.at<Vec4b>(i, j)[2] + alpha * in.at<Vec4b>(i, j)[2];
  44. // cout << "alpha " << i << " " << j << endl;
  45. output.at<Vec4b>(i, j)[3] = (1 - alpha) * background.at<Vec4b>(i, j)[3] + alpha * in.at<Vec4b>(i, j)[3];
  46. // cout << i << " " << j << endl;
  47. continue;
  48. }
  49. }
  50. imshow("new_picture", output);
  51. waitKey();
  52. return 0;
  53. }

实验1.2:对比度调整

设计一个Sigmoid函数,实现对象对比度调整,并使得调整幅度可通过参数控制。使用opencv窗口系统的slider控件,交互改变Sigmoid函数的参数,实现不同程度的对比度调整

  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. #include <time.h>
  7. using namespace std;
  8. using namespace cv;
  9. int alpha = 50;
  10. //ԭͼ����ͼ
  11. Mat picture;
  12. Mat new_picture;
  13. void change_alpha(int, void*)
  14. {
  15. for (int i = 0; i < picture.rows; i++)
  16. {
  17. for (int j = 0; j < picture.cols; j++)
  18. {
  19. for (int k = 0; k < 3; k++)
  20. {
  21. double tmp = 255.0 / (1 + exp(-(alpha * 0.001 * 0.4) * (picture.at<Vec3b>(i, j)[k] - 127.5)));//�Աȶȼ���
  22. new_picture.at<Vec3b>(i, j)[k] = saturate_cast<uchar>(tmp);
  23. }
  24. }
  25. }
  26. imshow("New Image", new_picture);
  27. return;
  28. }
  29. int main()
  30. {
  31. picture = imread("C:/Users/13441/Desktop/����ͼ��/b.png");
  32. new_picture = Mat::zeros(picture.size(), picture.type());
  33. namedWindow("Slider", WINDOW_AUTOSIZE);
  34. namedWindow("New Image", WINDOW_AUTOSIZE);
  35. createTrackbar("�Աȶȣ�", "Slider", &alpha, 100, change_alpha);
  36. change_alpha(alpha, 0);
  37. waitKey(0);
  38. return 0;
  39. }


本文转载自: https://blog.csdn.net/qq_56549521/article/details/141018791
版权归原作者 αα 所有, 如有侵权,请联系我们删除。

“山东大学数字图像处理实验(一) 计算机学院”的评论:

还没有评论