分享

【OpenCV学习笔记】三十、轮廓特征属性及应用(七)

 delphi_笔记 2019-09-15
matchShapes轮廓特征属性及应用(七)—位置关系及轮廓匹配

1.计算点与轮廓的距离及位置关系——pointPolygonTest()

2.矩的计算——moments()

3.形状匹配(比较两个形状或轮廓间的相似度)——matchShapes()

先上ppt:






代码:1.计算点到轮廓的距离与位置关系

  1. ///计算点到轮廓的距离与位置关系
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找轮廓前的预处理
  9. Mat srcImg = imread("00.png",CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg = srcImg.clone();
  11. cvtColor(srcImg,srcImg,CV_BGR2GRAY);
  12. threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh",srcImg);
  14. //2.查找轮廓
  15. vector<vector<Point>> contours;
  16. findContours(srcImg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//最外层轮廓
  17. drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
  18. //3.计算点到轮廓的距离与位置关系
  19. Point2f p1(20, 20);
  20. circle(copyImg,p1,3,Scalar(0,0,255),-1,8);
  21. double a0 = pointPolygonTest(contours[0], p1, true);//true表示点到轮廓的距离
  22. double b0 = pointPolygonTest(contours[0], p1, false);//false表示计算点与轮廓的位置关系
  23. cout << "a0=" << a0 << endl;
  24. cout << "b0=" << b0 << endl;
  25. imshow("contours",copyImg);
  26. waitKey(0);
  27. return 0;
  28. }

运行结果:


代码:2.轮廓矩的计算

  1. ///轮廓矩的计算
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找轮廓前的预处理
  9. Mat srcImg = imread("00.png", CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg = srcImg.clone();
  11. cvtColor(srcImg, srcImg, CV_BGR2GRAY);
  12. threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh", srcImg);
  14. //2.查找轮廓
  15. vector<vector<Point>> contours;
  16. findContours(srcImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  17. drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
  18. //3.轮廓矩的计算
  19. Moments moments0 = moments(contours[0],false);//计算轮廓矩
  20. cout << moments0.m00<< endl;//输出空间矩之一的m00
  21. imshow("contours", copyImg);
  22. waitKey(0);
  23. return 0;
  24. }

运行结果:


代码:3.形状匹配---比较两个形状或轮廓间的相似度

  1. ///形状匹配---比较两个形状或轮廓间的相似度
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找模版图像的轮廓
  9. Mat templateImg = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg1 = templateImg.clone();
  11. cvtColor(templateImg, templateImg, CV_BGR2GRAY);
  12. threshold(templateImg, templateImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh1", templateImg);
  14. vector<vector<Point>> contours1;
  15. findContours(templateImg, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  16. drawContours(copyImg1, contours1, -1, Scalar(0, 255, 0), 2, 8);
  17. //2.查找待测试图像的轮廓
  18. Mat testImg = imread("2.jpg", CV_LOAD_IMAGE_COLOR);
  19. Mat copyImg2 = testImg.clone();
  20. cvtColor(testImg, testImg, CV_BGR2GRAY);
  21. threshold(testImg, testImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  22. imshow("thresh2", testImg);
  23. vector<vector<Point>> contours2;
  24. findContours(testImg, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  25. //3.形状匹配---比较两个形状或轮廓间的相似度
  26. for (int i = 0; i < contours2.size();i++)//遍历待测试图像的轮廓
  27. {
  28. //返回此轮廓与模版轮廓之间的相似度,a0越小越相似
  29. double a0 = matchShapes(contours1[0],contours2[i],CV_CONTOURS_MATCH_I1,0);
  30. cout << "模版轮廓与待测试图像轮廓" << i << "的相似度:" << a0 << endl;//输出两个轮廓间的相似度
  31. if (a0<0.1)//如果此轮廓与模版轮廓的相似度小于0.1
  32. {
  33. drawContours(copyImg2, contours2, i, Scalar(0, 255, 0), 2, 8);//则在待测试图像上画出此轮廓
  34. }
  35. imshow("copyImg2", copyImg2);
  36. if (waitKey(0) == 27)//等待按键进行下一个轮廓,ESC则退出
  37. {
  38. cout << "ESC退出" << endl;
  39. break;
  40. }
  41. }
  42. waitKey(0);
  43. return 0;
  44. }

运行结果:


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多