opencv3/C++轮廓的提取与筛选方式

时间:2021-10-04 09:13:44

轮廓提取

findContours发现轮廓

  1. findContours(
  2. InputOutputArray binImg, //输入8bit图像,0值像素值不变,非0的像素看成1;(变为二值图像)
  3. OutputArrayOfArrays contours,//输出找到的轮廓对象
  4. OutputArray, hierachy// 图像的拓扑结构
  5. int mode, //轮廓返回的模式(RETR_TREE等)
  6. int method,//发现方法(CHAIN_APPROX_SIMPLE等)
  7. Point offset=Point()//轮廓像素的位移(默认没有位移(0, 0))
  8. )

【报错问题】

findContours()有时会报告“已触发了一个断点”等错误,尝试过y有效的解决方法有:

1.为vector提前申请一定的空间,如

  1. std::vector<std::vector<Point>> contours(500)

2.Debug版切换为Release版;

drawContours绘制轮廓

  1. drawContours(
  2. InputOutputArray binImg, // 输出图像
  3. OutputArrayOfArrays contours,//找到的全部轮廓对象
  4. Int contourIdx//轮廓索引号
  5. const Scalar & color,//绘制颜色
  6. int thickness,//绘制线宽
  7. int lineType ,//线的类型(默认8)
  8. InputArray hierarchy,//拓扑结构图
  9. int maxlevel,//最大层数(0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓)
  10. Point offset=Point()//轮廓位移
  11. )

示例:

  1. #include<opencv2/opencv.hpp>
  2. using namespace cv;
  3.  
  4. int main()
  5. {
  6. Mat src,dst;
  7. src = imread("E:/image/image/shape.jpg");
  8. if(src.empty())
  9. {
  10. printf("can not load image \n");
  11. return -1;
  12. }
  13. namedWindow("input", CV_WINDOW_AUTOSIZE);
  14. imshow("input", src);
  15. dst = Mat::zeros(src.size(), CV_8UC3);
  16.  
  17. blur(src,src,Size(3,3));
  18. cvtColor(src,src,COLOR_BGR2GRAY);
  19. Canny(src, src, 20, 80, 3, false);
  20. std::vector<std::vector<Point>> contours;
  21. std::vector<Vec4i> hierarchy;
  22. findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
  23.  
  24. RNG rng(0);
  25. for(int i = 0; i < contours.size(); i++)
  26. {
  27. Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  28. drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point(0,0));
  29. }
  30. namedWindow("output", CV_WINDOW_AUTOSIZE);
  31. imshow("output",dst);
  32. waitKey();
  33. return 0;
  34. }

opencv3/C++轮廓的提取与筛选方式

opencv3/C++轮廓的提取与筛选方式

使用opencv3时(测试用opencv3.1.0)发现,cv命名空间下没有了vector,而在opencv2中(测试用opencv2.4.10)还存在。后查看各自的头文件发现:

opencv.hpp头文件中包含着core.hpp(#include “opencv2/core.hpp”);

而在opencv2的core.hpp中包含有

  1. ........
  2. #include <map>
  3. #include <new>
  4. #include <string>
  5. #include <vector>
  6. .......

等头文件,但在opencv3的core.hpp中删去这些包含项。

因此在使用opencv3时cv命名空间下没有了vector。

使用opencv2.4.10时可以写:

  1. #include<opencv2/opencv.hpp>
  2. using namespace cv;
  3.  
  4. int main()
  5. {
  6. Mat src,dst;
  7. src = imread("E:/image/image/shape.jpg");
  8. if(src.empty())
  9. {
  10. printf("can not load image \n");
  11. return -1;
  12. }
  13. namedWindow("input", CV_WINDOW_AUTOSIZE);
  14. imshow("input", src);
  15. dst = Mat::zeros(src.size(), CV_8UC3);
  16. blur(src,src,Size(5,5));
  17. Canny(src, src, 20, 80, 3, false);
  18. vector<vector<Point>>contours;
  19. vector<Vec4i> hierarchy;
  20. findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
  21.  
  22. RNG rng(0);
  23. for(int i = 0; i < contours.size(); i++)
  24. {
  25. Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  26. drawContours(dst, contours, i, color, 2, 8, hierarchy, 0, Point(0,0));
  27. }
  28. namedWindow("output", CV_WINDOW_AUTOSIZE);
  29. imshow("output",dst);
  30. waitKey();
  31. return 0;
  32. }

绘制轮廓外矩形框

常用绘制轮廓外形状的函数:

cv::boundingRect(InputArray points)绘制一个矩形(轮廓周围最小矩形左上角点和右下角点)

cv::minAreaRect(InputArray points)绘制轮廓周围最小旋转矩形

cv::minEnclosingCircle(InputArray points, Point2f& center, float& radius)//绘制轮廓周围最小圆形

cv::fitEllipse(InputArray points)绘制轮廓周围最小椭圆

绘制轮廓外矩形框:

  1. #include<opencv2/opencv.hpp>
  2. using namespace cv;
  3.  
  4. int main()
  5. {
  6. Mat src,dst;
  7. src = imread("E:/image/shape.jpg");
  8. if(src.empty())
  9. {
  10. printf("can not load image \n");
  11. return -1;
  12. }
  13. namedWindow("input", CV_WINDOW_AUTOSIZE);
  14. imshow("input", src);
  15. dst = Mat::zeros(src.size(), CV_8UC3);
  16. std::vector<std::vector<Point>>contours;
  17. std::vector<Vec4i> hierarchy;
  18. blur(src,src,Size(3,3));
  19. cvtColor(src,src,COLOR_BGR2GRAY);
  20. Canny(src, src, 20, 80, 3, false);
  21. findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
  22.  
  23. RNG rng(0);
  24. std::vector<std::vector<Point>> contoursPloy(contours.size());
  25. std::vector<RotatedRect> minRects(contours.size());
  26.  
  27. for(int i = 0; i < contours.size(); i++)
  28. {
  29. minRects[i] = minAreaRect(Mat(contours[i]));
  30. Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  31. drawContours(dst, contoursPloy, i, color, 1,8,std::vector<Vec4i>(), 0, Point(0, 0));
  32. Point2f rectPoints[4];
  33. minRects[i].points(rectPoints);
  34. for (int j = 0; j < 4; j++)
  35. {
  36. line(dst, rectPoints[j], rectPoints[(j+1)%4], color, 1, 8, 0);
  37. }
  38. }
  39.  
  40. namedWindow("output", CV_WINDOW_AUTOSIZE);
  41. imshow("output",dst);
  42. waitKey();
  43. return 0;
  44. }

opencv3/C++轮廓的提取与筛选方式

opencv3/C++轮廓的提取与筛选方式

轮廓筛选

  1. moments(
  2. InputArray array,//输入数据
  3. bool binaryImage=false //是否为二值图像
  4. )
  5. contourArea(
  6. InputArray contour,//输入轮廓数据
  7. bool oriented//返回绝对值(默认false)
  8. )
  9.  
  10. arcLength(
  11. InputArray curve,//输入轮廓
  12. bool closed// 轮廓否是封闭曲线
  13. )

轮廓筛选示例:

使用轮廓的面积和长度特征对轮廓进行筛选后用外接矩形将筛选后的轮廓框选出来。

  1. #include<opencv2/opencv.hpp>
  2. using namespace cv;
  3.  
  4. void trackBar(int,void*);
  5.  
  6. Mat src,dst;
  7. std::vector<std::vector<Point>>contours;
  8. int area = 0, length = 0;
  9. int main()
  10. {
  11.  
  12. src = imread("E:/image/shape.jpg");
  13. if(src.empty())
  14. {
  15. printf("can not load image \n");
  16. return -1;
  17. }
  18. namedWindow("input", CV_WINDOW_AUTOSIZE);
  19. imshow("input", src);
  20. dst = Mat::zeros(src.size(), CV_8UC3);
  21.  
  22. std::vector<Vec4i> hierarchy;
  23. blur(src,dst,Size(3,3));
  24. cvtColor(dst,dst,COLOR_BGR2GRAY);
  25. Canny(dst, dst, 20, 80, 3, false);
  26. findContours(dst, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
  27. namedWindow("output", CV_WINDOW_AUTOSIZE);
  28.  
  29. createTrackbar("area:", "output", &area,150,trackBar);
  30. createTrackbar("length:", "output", &length,150,trackBar);
  31.  
  32. waitKey();
  33. return 0;
  34. }
  35.  
  36. void trackBar(int,void*)
  37. {
  38. Mat src1 = src.clone();
  39. RNG rng(0);
  40. std::vector<std::vector<Point>> contoursPloy(contours.size());
  41. std::vector<RotatedRect> minRects(contours.size());
  42.  
  43. for(int i = 0; i < contours.size(); i++)
  44. {
  45. if(contourArea(contours[i]) > area && arcLength(contours[i], false) > length)
  46. {
  47. minRects[i] = minAreaRect(Mat(contours[i]));
  48. Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255));
  49. //drawContours(dst, contoursPloy, i, color, 1,8,vector<Vec4i>(), 0, Point(0, 0));
  50. Point2f rectPoints[4];
  51. minRects[i].points(rectPoints);
  52. for (int j = 0; j < 4; j++)
  53. {
  54. line(src1, rectPoints[j], rectPoints[(j+1)%4], color, 2, 8, 0);
  55. }
  56. }
  57. }
  58. imshow("output",src1);
  59. src1 = src;
  60. }

opencv3/C++轮廓的提取与筛选方式

opencv3/C++轮廓的提取与筛选方式

以上这篇opencv3/C++轮廓的提取与筛选方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/akadiao/article/details/78843773