Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1.  
  2. #include "opencv2/calib3d/calib3d.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include "opencv2/contrib/contrib.hpp"
  6.  
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include <fstream>
  10.  
  11.  
  12. using namespace cv;
  13.  
  14. int main(int argc, char** argv)
  15. {
  16.     Mat img = imread("pileczka_L.png");
  17.  
  18.     Mat edges = img.clone();
  19.     Canny(img, edges, 150, 450);
  20.     imwrite("MiI-Kontur.png", edges);
  21.  
  22.     Mat filled = edges.clone();
  23.     cvtColor(edges, filled, CV_GRAY2RGB);
  24.     floodFill(filled, Point(700, 400), CV_RGB(255, 255, 255));
  25.     floodFill(filled, Point(0, 0), CV_RGB(0, 255, 0));
  26.     imwrite("MiI-Obszar.png", filled);
  27.  
  28.     Mat mapa = imread("MiI-MapaRozbieznosci-Skalibrowana.png");
  29.  
  30.     Mat depth = 4800.0 / mapa;
  31.     imwrite("MiI-MapaGlebi.png", depth);
  32.  
  33.  
  34.     Mat dist = imread("MiI-MapaGlebi.png");
  35.     Mat ball = imread("MiI-Obszar.png");
  36.  
  37.     double avgDist = 0;
  38.     int nDist = 0;
  39.  
  40.     for (int i = 0; i < ball.rows; i++) {
  41.         for (int j = 0; j < ball.cols; j++) {
  42.             Vec3b ballPx = ball.at<Vec3b>(i, j);
  43.             uchar distPx = dist.at<uchar>(i, j);
  44.  
  45.             // Skip pixels not in ball.
  46.             if (ballPx != Vec3b(255, 255, 255))
  47.                 continue;
  48.  
  49.             avgDist += distPx;
  50.             nDist++;
  51.         }
  52.     }
  53.  
  54.     avgDist /= nDist;
  55.  
  56.     std::ofstream out("MiI-SredniaOdleglosc.txt");
  57.     out << avgDist;
  58.     out.close();
  59.  
  60.     int nBallPixels = 0;
  61.     for (int i = 0; i < ball.rows; i++) {
  62.         for (int j = 0; j < ball.cols; j++) {
  63.             if (ball.at<Vec3b>(i, j) == Vec3b(255, 255, 255))
  64.                 nBallPixels++;
  65.         }
  66.     }
  67.  
  68.     double diameterPx = 2 * sqrt((double)nBallPixels / 3.141592);
  69.     double diameterCm = 0.001 * avgDist * diameterPx;
  70.  
  71.     std::cout << diameterCm << std::endl;
  72.  
  73.     system("pause");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement