1.读图像imread()

  1. #include<opencv2\opencv.hpp>
  2. using namespace cv;
  3. int main(int argc,char* argv[])
  4. {
  5. Mat img;
  6. //-- 1 --双右斜线法
  7. //string imgpath = "C:\\Users\\bingbuyu\\Pictures\\photo\\miao1.jpg";
  8. //-- 2 --双左斜线法
  9. //string imgpath = "C://Users//bingbuyu//Pictures//photo//miao1.jpg";
  10. //-- 3 --单左斜线法
  11. //string imgpath = "C:/Users/bingbuyu/Pictures/photo/miao1.jpg";
  12. //-- 4 --以上三种混合法
  13. //string imgpath = "C:/Users//bingbuyu\\Pictures//photo//miao1.jpg";
  14. //-- 5 --相对路径法
  15. //string imgpath = "miao.jpg";
  16. //-- 6 --命令行参数法
  17. string imgpath = argv[1];
  18. img = imread(imgpath, 1);
  19. imshow("img", img);
  20. waitKey(0);
  21. return 0;
  22. }

2.显示图像imshow()

  1. void cv::imshow (const String& winname,
  2. InputArray mat
  3. )
  4. Python:
  5. None = cv.imshow(winname, mat)

3.保存图像imwirte()

  1. #include <iostream>
  2. #include <opencv2\opencv.hpp>
  3. using namespace std;
  4. using namespace cv;
  5. void AlphaMat(Mat &mat)
  6. {
  7. CV_Assert(mat.channels() == 4);
  8. for (int i = 0; i < mat.rows; ++i)
  9. {
  10. for (int j = 0; j < mat.cols; ++j)
  11. {
  12. Vec4b& bgra = mat.at<Vec4b>(i, j);
  13. bgra[0] = UCHAR_MAX; // 蓝色通道
  14. bgra[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // 绿色通道
  15. bgra[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // 红色通道
  16. bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha通道
  17. }
  18. }
  19. }
  20. int main(int agrc, char** agrv)
  21. {
  22. // Create mat with alpha channel
  23. Mat mat(480, 640, CV_8UC4);
  24. AlphaMat(mat);
  25. vector<int> compression_params;
  26. compression_params.push_back(IMWRITE_PNG_COMPRESSION); //PNG格式图像压缩标志
  27. compression_params.push_back(9); //设置最高压缩质量
  28. bool result = imwrite("alpha.png", mat, compression_params);
  29. if (!result)
  30. {
  31. cout << "保存成PNG格式图像失败" << endl;
  32. return -1;
  33. }
  34. cout << "保存成功" << endl;
  35. return 0;
  36. }

4.读视频VideoCapture()

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace cv;
  5. int main()
  6. {
  7. system("color F0"); //更改输出界面颜色
  8. VideoCapture video("cup.mp4");
  9. if (video.isOpened())
  10. {
  11. cout << "视频中图像的宽度=" << video.get(CAP_PROP_FRAME_WIDTH) << endl;
  12. cout << "视频中图像的高度=" << video.get(CAP_PROP_FRAME_HEIGHT) << endl;
  13. cout << "视频帧率=" << video.get(CAP_PROP_FPS) << endl;
  14. cout << "视频的总帧数=" << video.get(CAP_PROP_FRAME_COUNT);
  15. }
  16. else
  17. {
  18. cout << "请确认视频文件名称是否正确" << endl;
  19. return -1;
  20. }
  21. while (1)
  22. {
  23. Mat frame;
  24. video >> frame;
  25. if (frame.empty())
  26. {
  27. break;
  28. }
  29. imshow("video", frame);
  30. waitKey(1000 / video.get(CAP_PROP_FPS));
  31. }
  32. waitKey();
  33. return 0;
  34. }

5.保存视频VideoWriter()

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. using namespace cv;
  4. using namespace std;
  5. int main()
  6. {
  7. Mat img;
  8. VideoCapture video(0); //使用某个摄像头
  9. //读取视频
  10. //VideoCapture video;
  11. //video.open("cup.mp4");
  12. if (!video.isOpened()) // 判断是否调用成功
  13. {
  14. cout << "打开摄像头失败,请确实摄像头是否安装成功";
  15. return -1;
  16. }
  17. video >> img; //获取图像
  18. //检测是否成功获取图像
  19. if (img.empty()) //判断有没有读取图像成功
  20. {
  21. cout << "没有获取到图像" << endl;
  22. return -1;
  23. }
  24. bool isColor = (img.type() == CV_8UC3); //判断相机(视频)类型是否为彩色
  25. VideoWriter writer;
  26. int codec = VideoWriter::fourcc('M', 'J', 'P', 'G'); // 选择编码格式
  27. //OpenCV 4.0版本设置编码格式
  28. //int codec = CV_FOURCC('M', 'J', 'P', 'G');
  29. double fps = 25.0; //设置视频帧率
  30. string filename = "live.avi"; //保存的视频文件名称
  31. writer.open(filename, codec, fps, img.size(), isColor); //创建保存视频文件的视频流
  32. if (!writer.isOpened()) //判断视频流是否创建成功
  33. {
  34. cout << "打开视频文件失败,请确实是否为合法输入" << endl;
  35. return -1;
  36. }
  37. while (1)
  38. {
  39. //检测是否执行完毕
  40. if (!video.read(img)) //判断能都继续从摄像头或者视频文件中读出一帧图像
  41. {
  42. cout << "摄像头断开连接或者视频读取完成" << endl;
  43. break;
  44. }
  45. writer.write(img); //把图像写入视频流
  46. //writer << img;
  47. imshow("Live", img); //显示图像
  48. char c = waitKey(50);
  49. if (c == 27) //按ESC案件退出视频保存
  50. {
  51. break;
  52. }
  53. }
  54. // 退出程序时刻自动关闭视频流
  55. //video.release();
  56. //writer.release();
  57. return 0;
  58. }

6.调用摄像头