图片腐蚀

  1. #include <iostream>
  2. #include "opencv2/core.hpp"
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include<opencv2/imgproc/imgproc.hpp>
  6. using namespace std;
  7. using namespace cv;
  8. int main(int argc, char** argv)
  9. {
  10. //载入原图
  11. Mat srcImage = imread("E:\\opencv test image\\a.jpg");
  12. //显示原图
  13. imshow("原图", srcImage);
  14. //进行腐蚀操作
  15. Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
  16. Mat dstImage;
  17. erode(srcImage, dstImage, element);
  18. //显示效果
  19. imshow("腐蚀操作", dstImage);
  20. while (1) {
  21. waitKey(0);
  22. }
  23. return 0;
  24. }

图像模糊

  1. #include <iostream>
  2. #include "opencv2/core.hpp"
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include<opencv2/imgproc/imgproc.hpp>
  6. using namespace std;
  7. using namespace cv;
  8. int main(int argc, char** argv)
  9. {
  10. //载入原图
  11. Mat srcImage = imread("E:\\opencv test image\\a.jpg");
  12. //显示原图
  13. imshow("原图", srcImage);
  14. //进行滤波操作
  15. Mat dstImage;
  16. blur(srcImage, dstImage, Size(7, 7));
  17. //显示效果
  18. imshow("均值滤波", dstImage);
  19. while (1) {
  20. waitKey(0);
  21. }
  22. return 0;
  23. }

canny边缘检测

  1. #include <iostream>
  2. #include "opencv2/core.hpp"
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include<opencv2/imgproc/imgproc.hpp>
  6. using namespace std;
  7. using namespace cv;
  8. int main(int argc, char** argv)
  9. {
  10. //载入原图
  11. Mat srcImage = imread("E:\\opencv test image\\a.jpg");
  12. //显示原图
  13. imshow("原图", srcImage);
  14. Mat dstImage, edge, grayImage;
  15. //创建与src同类型和大小的矩阵
  16. dstImage.create(srcImage.size(), srcImage.type());
  17. //将原图像转换为灰度图像
  18. cvtColor(srcImage, grayImage, COLOR_BGR2GRAY);
  19. blur(grayImage, edge, Size(3, 3));
  20. Canny(edge, edge, 3, 9, 3);
  21. imshow("Canny边缘检测", edge);
  22. while (1) {
  23. waitKey(0);
  24. }
  25. return 0;
  26. }

视频读取

  1. #include <iostream>
  2. #include "opencv2/core.hpp"
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include<opencv2/opencv.hpp>
  6. #include<opencv2/imgproc/imgproc.hpp>
  7. using namespace std;
  8. using namespace cv;
  9. int main(int argc, char** argv)
  10. {
  11. //读取视频
  12. VideoCapture capture("1.avi");
  13. //循环显示每一帧
  14. while (1) {
  15. Mat frame;
  16. capture >> frame;
  17. imshow("读取视频", frame);
  18. waitKey(30);
  19. }
  20. while (1) {
  21. waitKey(0);
  22. }
  23. return 0;
  24. }

调用摄像头采集图像

  1. #include <iostream>
  2. #include "opencv2/core.hpp"
  3. #include <opencv2/imgcodecs.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include<opencv2/opencv.hpp>
  6. #include<opencv2/imgproc/imgproc.hpp>
  7. using namespace std;
  8. using namespace cv;
  9. int main(int argc, char** argv)
  10. {
  11. //从摄像头读入视频
  12. VideoCapture capture(0);
  13. Mat edges;
  14. //循环显示每一帧
  15. while (1) {
  16. Mat frame;
  17. capture >> frame;
  18. //转换为灰度图像
  19. cvtColor(frame, edges, CV_BGR2GRAY);
  20. //使用3*3内核降噪2*3+1=7
  21. blur(edges, edges, Size(7, 7));
  22. //进行canny边缘检测后并显示
  23. Canny(edges, edges, 0, 30, 3);
  24. imshow("canny后的视频", edges);
  25. waitKey(30);
  26. }
  27. while (1) {
  28. waitKey(0);
  29. }
  30. return 0;
  31. }