zoukankan      html  css  js  c++  java
  • Opencv 图像矩

    #include <iostream>
    #include <opencv2/opencv.hpp>

    using namespace std;
    using namespace cv;

    Mat img1, img2, img3, img4, img_result, img_gray1, img_gray2, img_gray3, img_canny1;

    char win1[] = "window1";
    char win2[] = "window2";
    char win3[] = "window3";
    char win4[] = "window4";
    char win5[] = "window5";

    int thread_value = 100;
    int max_value = 255;
    RNG rng1(12345);

    int Demo_Moments();
    void Demo_1(int, void*);

    //图像矩
    int Demo_Moments()
    {
      namedWindow(win1, CV_WINDOW_AUTOSIZE);
      namedWindow(win2, CV_WINDOW_AUTOSIZE);
      //namedWindow(win3, CV_WINDOW_AUTOSIZE);

      img1 = imread("D://images//19.png");
      //img2 = imread("D://images//1//p5_1.jpg");
      if (img1.empty())
      {
        cout << "could not load image..." << endl;
        return 0;
      }

      imshow(win1, img1);
      //img4 = Mat::zeros(img1.size(), CV_8UC3);

      //转灰度图
      cvtColor(img1, img_gray1, CV_BGR2GRAY);
      //模糊处理
      //blur(img_gray1, img2, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
      GaussianBlur(img1, img2, Size(3, 3), 0, 0);

      createTrackbar("track", win1, &thread_value, max_value, Demo_1);
      Demo_1(0, 0);

      return 0;
    }

    void Demo_1(int, void*)
    {
      vector<vector<Point>> vec_p;
      vector<Vec4i> vec_4i;
      img4 = Mat::zeros(img1.size(), CV_8UC3);

      //threshold(img2, img3, thread_value, max_value, THRESH_BINARY);
      Canny(img2, img3, thread_value, thread_value * 2, 3, false);
      findContours(img3, vec_p, vec_4i, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

      //vector<vector<Point>> convexs(vec_p.size());
      vector<Moments> contours_moments(vec_p.size());
      vector<Point2f> ccs(vec_p.size());
      for (size_t i = 0; i<vec_p.size(); i++)
      {
        contours_moments[i] = moments(vec_p[i]);
        ccs[i] = Point(static_cast<float>(contours_moments[i].m10/contours_moments[i].m00),static_cast<float>(contours_moments[i].m01/contours_moments[i].m00));
      }

      for (size_t j = 0; j<vec_p.size(); j++)
      {
        if (vec_p[j].size() <100)
        {
          continue;
        }
        Scalar color_1 = Scalar(rng1.uniform(0, 255), rng1.uniform(0, 255), rng1.uniform(0, 255));
        
        printf("center point x : %.2f y : %.2f ", ccs[j].x, ccs[j].y);
        printf("contours %d area : %.2f arc length : %.2f ", j, contourArea(vec_p[j]), arcLength(vec_p[j], true));

        drawContours(img4, vec_p, j, color_1, 2, LINE_8, vec_4i, 0, Point(0, 0));
        circle(img4,ccs[j],2,color_1,2,8);
      }
      imshow(win2, img4);
    }

    int main()
    {
      Demo_Moments();

      waitKey(0);
      return 0;
    }

     --------------------------------------------------------------------------

    center point x : 627.00 y : 607.00
    contours 5 area : 68.00 arc length : 498.84
    center point x : 534.00 y : 609.00
    contours 7 area : 41.50 arc length : 300.15
    center point x : 691.00 y : 611.00
    contours 8 area : 20.00 arc length : 207.97
    center point x : 896.00 y : 570.00
    contours 10 area : 56.00 arc length : 478.90
    center point x : 880.00 y : 479.00
    contours 11 area : 103.00 arc length : 691.15
    center point x : 721.00 y : 427.00
    contours 12 area : 26.50 arc length : 320.84
    center point x : 411.00 y : 364.00
    contours 16 area : 32.50 arc length : 333.81
    center point x : 282.00 y : 362.00
    contours 18 area : 53.50 arc length : 543.83
    center point x : 334.00 y : 439.00
    contours 19 area : 150.50 arc length : 1344.58
    center point x : 606.00 y : 397.00
    contours 21 area : 38.50 arc length : 389.95
    center point x : 1017.00 y : 425.00
    contours 23 area : 88.50 arc length : 742.42
    center point x : 1088.00 y : 352.00
    contours 27 area : 46.50 arc length : 380.23
    center point x : 723.00 y : 416.00
    contours 37 area : 119.50 arc length : 1088.67
    center point x : 851.00 y : 268.00
    contours 40 area : 26.50 arc length : 361.46
    center point x : 96.00 y : 336.00
    contours 43 area : 63.00 arc length : 541.73
    center point x : 739.00 y : 255.00
    contours 47 area : 46.00 arc length : 432.82
    center point x : 806.00 y : 253.00
    contours 53 area : 56.50 arc length : 310.58
    center point x : 556.00 y : 318.00
    contours 56 area : 58.50 arc length : 492.17
    center point x : 100.00 y : 186.00
    contours 63 area : 12.50 arc length : 163.78
    center point x : 552.00 y : 193.00
    contours 65 area : 32.50 arc length : 318.49
    center point x : 420.00 y : 199.00
    contours 66 area : 44.50 arc length : 372.09
    center point x : 400.00 y : 242.00
    contours 67 area : 61.00 arc length : 519.73
    center point x : 735.00 y : 187.00
    contours 71 area : 50.50 arc length : 246.63
    center point x : 350.00 y : 114.00
    contours 75 area : 31.50 arc length : 267.52
    center point x : 202.00 y : 184.00
    contours 77 area : 83.50 arc length : 604.34
    center point x : 299.00 y : 153.00
    contours 79 area : 139.50 arc length : 1052.33
    center point x : 761.00 y : 126.00
    contours 83 area : 15.50 arc length : 298.55
    center point x : 983.00 y : 66.00
    contours 85 area : 51.50 arc length : 433.06
    center point x : 533.00 y : 129.00
    contours 86 area : 58.00 arc length : 515.19
    center point x : 533.00 y : 143.00
    contours 88 area : 79.50 arc length : 3004.34

    --------------------------------------------------------------------------

  • 相关阅读:
    rand()和srand()
    advanced regression to predict housing prices
    数学建模
    python的对数
    八月总结——人工智能初学者
    看到的不错的项目
    学习笔记(七): Logistic Regression
    学习笔记(六): Regularization for Simplicity
    An Intuitive Explanation of Convolutional Neural Networks
    CentOS7的mysql5.7-rpm.bundle方式安装
  • 原文地址:https://www.cnblogs.com/herd/p/9737878.html
Copyright © 2011-2022 走看看