Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/calib3d/calib3d.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/contrib/contrib.hpp"
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- using namespace cv;
- int main(int argc, char** argv)
- {
- Mat img = imread("pileczka_L.png");
- Mat edges = img.clone();
- Canny(img, edges, 150, 450);
- imwrite("MiI-Kontur.png", edges);
- Mat filled = edges.clone();
- cvtColor(edges, filled, CV_GRAY2RGB);
- floodFill(filled, Point(700, 400), CV_RGB(255, 255, 255));
- floodFill(filled, Point(0, 0), CV_RGB(0, 255, 0));
- imwrite("MiI-Obszar.png", filled);
- Mat mapa = imread("MiI-MapaRozbieznosci-Skalibrowana.png");
- Mat depth = 4800.0 / mapa;
- imwrite("MiI-MapaGlebi.png", depth);
- Mat dist = imread("MiI-MapaGlebi.png");
- Mat ball = imread("MiI-Obszar.png");
- double avgDist = 0;
- int nDist = 0;
- for (int i = 0; i < ball.rows; i++) {
- for (int j = 0; j < ball.cols; j++) {
- Vec3b ballPx = ball.at<Vec3b>(i, j);
- uchar distPx = dist.at<uchar>(i, j);
- // Skip pixels not in ball.
- if (ballPx != Vec3b(255, 255, 255))
- continue;
- avgDist += distPx;
- nDist++;
- }
- }
- avgDist /= nDist;
- std::ofstream out("MiI-SredniaOdleglosc.txt");
- out << avgDist;
- out.close();
- int nBallPixels = 0;
- for (int i = 0; i < ball.rows; i++) {
- for (int j = 0; j < ball.cols; j++) {
- if (ball.at<Vec3b>(i, j) == Vec3b(255, 255, 255))
- nBallPixels++;
- }
- }
- double diameterPx = 2 * sqrt((double)nBallPixels / 3.141592);
- double diameterCm = 0.001 * avgDist * diameterPx;
- std::cout << diameterCm << std::endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement