OpenCV轮廓vectorvector

时间:2023-03-08 17:05:53

OpenCV轮廓vectorvector,vector,vector,vector

https://blog.****.net/Ahuuua/article/details/80593388

轮廓周围绘制矩形框

vector<vector<Point>>,vector<V e c 4 i>,vector<R e c t>,vector<R o t a t e d R e c t>

刚开始学OpenCV没多久遇到这些个东西不知道是什么,搞得很不舒服。

通过给轮廓绘制矩形框弄明白了这些东西。

vector<vector<Point>>:

vector容器里面放了一个vector容器,子容器里放点

OpenCV轮廓vectorvector

vector<V e c 4 i>:放了4维int向量

OpenCV轮廓vectorvector

vector<R e ct>: 像素width * height   from  位置(x*y)

OpenCV轮廓vectorvector

vector<R o t a t e d R e c t>:如图三个成员

OpenCV轮廓vectorvector

上述结果的代码如下:

  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. using namespace cv;
  4. using namespace std;
  5. int main() {
  6. Mat src, gray_src, drawImg, bin_output;
  7. src = imread("F:/Temp_vs/pic/09.jpg");
  8. namedWindow("input", CV_WINDOW_AUTOSIZE);
  9. namedWindow("output", CV_WINDOW_AUTOSIZE);
  10. imshow("input", src);
  11. cvtColor(src, gray_src, CV_BGR2GRAY);
  12. blur(gray_src, gray_src, Size(10, 10), Point(-1, -1), BORDER_DEFAULT);
  13. //这些个类型
  14. vector<vector<Point>> contours;
  15. vector<Vec4i> hierarchy;
  16. threshold(gray_src, bin_output, 144, 255, 0); //二值化
  17. findContours(bin_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); //找轮廓
  18. //这些个类型
  19. vector<vector<Point>> contours_poly(contours.size());
  20. vector<Rect> poly_rects(contours.size());
  21. vector<RotatedRect> minRect(contours.size());
  22. //取点
  23. for (size_t i = 0; i < contours.size(); i++)
  24. {
  25. approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); //减少轮廓点数
  26. poly_rects[i]=boundingRect(contours_poly[i]);//获取绘制矩形数据
  27. if (contours_poly[i].size() > 5) {
  28. minRect[i] = minAreaRect(contours_poly[i]);//获取绘制旋转矩形数据
  29. }
  30. }
  31. //开始绘制
  32. src.copyTo(drawImg);
  33. Point2f pst[4];//储存单个旋转矩形的四个点
  34. cout << "----------Point2f pst[4]------------" << endl;
  35. for (size_t i = 0; i < contours.size(); i++)
  36. {
  37. rectangle(drawImg, poly_rects[i], Scalar(255, 0, 0), 2, 8);//绘制矩形框
  38. minRect[i].points(pst);//用线段画矩形,将RotatedRect类型转化为四个点
  39. for (size_t u = 0; u < 4; u++)
  40. {
  41. line(drawImg, pst[u], pst[(u + 1) % 4], Scalar(0, 255, 0), 2, 8);
  42. cout << pst[u]; //显示pst的数据
  43. }
  44. cout << endl;
  45. Rect brect = minRect[i].boundingRect(); //返回包含旋转矩形的最小矩形
  46. rectangle(drawImg, brect,Scalar(0, 0, 255));
  47. }
  48. cout << endl;
  49. imshow("output", drawImg);
  50. cout << "----------vector<vector<Point>> contours_poly------------" << endl;
  51. for (size_t i = 0; i < contours_poly.size(); i++)
  52. {
  53. cout << "第" << i << "行:";
  54. for (size_t j = 0; j < contours_poly[i].size(); j++)
  55. {
  56. cout<<contours_poly[i][j];
  57. }
  58. cout << endl;
  59. }
  60. cout << endl;
  61. cout << "----------vector<Vec4i> hierarchy------------" << endl;
  62. for (size_t i = 0; i < hierarchy.size(); i++)
  63. {
  64. cout << hierarchy[i] << endl;
  65. }
  66. cout << endl;
  67. cout << "----------vector<Rect> poly_rects------------" << endl;
  68. for (size_t i = 0; i < poly_rects.size(); i++)
  69. {
  70. cout << poly_rects[i]<<endl;
  71. }
  72. cout << endl;
  73. cout << "---------vector<RotatedRect> minRect------------" << endl;
  74. for (size_t i = 0; i <minRect.size(); i++) //显示一下点minRect
  75. {
  76. cout <<"angle:"<< minRect[i].angle<<" center:"<< minRect[i].center<<" size:"<< minRect[i].size<<endl;
  77. }
  78. cout << endl;
  79. waitKey(0);
  80. return 0;
  81. }

OpenCV轮廓vectorvector

OpenCV轮廓vectorvector