Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2\highgui\highgui.hpp"
- #include "opencv2\imgproc\imgproc.hpp"
- #include "opencv2\\video\\tracking.hpp"
- #include "opencv2\opencv.hpp"
- #include <iostream>
- #include <fstream>
- using namespace std;
- using namespace cv;
- // Chú ý: Khi chạy video 1 thì sử dụng mức thres cho từng video. Nếu chạy video 1 thì đóng (//thres=197), nếu chạy video 2 thì đóng (//thres=90)
- //Thresh hold
- //int thres = 90; //Mức thresh cho video 1
- int thres = 180; //Mức thresh cho video 2
- int maxval = 255;
- //Làm mờ
- int kernel_size = 9;
- //Canny Edge
- int low_threshold = 20;
- int high_threshold = 40;
- //Hough line
- int rho = 1;// Khoảng cách RHO
- double theta = CV_PI / 180;// Góc theta
- int houghVote = 30; //Ngưỡng
- int max_line_gap = 30; //Chiều dài lớn nhất
- int min_line_gap = 10;//Chiều dài nhỏ nhất của hough
- //Vẽ line
- Scalar color = Scalar(0, 255, 0);//Màu
- int thickness = 3;//Độ dày
- int prev_left_x;//Chứa toa do diem trươc đó ben trái
- int prev_right_x;
- //X_center xét từ dưới lên
- template <typename T>
- std::string NumberToString(T Number)
- {
- std::ostringstream ss;
- ss << Number;
- return ss.str();
- }
- Mat XuLy(Mat img)
- {
- Mat out;
- if (img.channels() == 3)
- cvtColor(img, out, CV_BGR2GRAY);
- GaussianBlur(out, out, Size(kernel_size, kernel_size), 15, 2);
- threshold(out, out, thres, maxval, CV_THRESH_BINARY);
- //medianBlur(out, out, 15);
- Canny(out, out, low_threshold, high_threshold, 3, false);
- return out;
- }
- Mat Cat_Hinh_Anh(Mat &src)
- {
- return src(Rect(0, roundf(src.size().height * 3 / 4), src.size().width, roundf(src.size().height / 4)));
- }
- Point Tim_Tam(vector<Point> left, vector<Point> right)
- {
- if (left.size() == 0 || right.size() == 0)
- return NULL;
- int sum_left_x = 0;
- int sum_left_y = 0;
- int count_left = 0;
- int sum_right_x = 0;
- int sum_right_y = 0;
- int count_right = 0;
- for (int i = 0; i < left.size(); i++)
- {
- sum_left_x += left[i].x;
- sum_left_y += left[i].y;
- count_left++;
- }
- for (int i = 0; i < right.size(); i++)
- {
- sum_right_x += right[i].x;
- sum_right_y += right[i].y;
- count_right++;
- }
- double mid_left_x = sum_left_x / count_left;
- double mid_left_y = sum_left_y / count_left;
- double mid_right_x = sum_right_x / count_right;
- double mid_right_y = sum_right_y / count_right;
- return Point(roundf((mid_left_x + mid_right_x) / 2), roundf((mid_left_y + mid_right_y) / 2));
- }
- Point Process(Mat img, Mat src)
- {
- vector<Vec4i> lines;
- }
- int main()
- {
- VideoCapture capture("C:/Users/ADMIN/Desktop/test.avi");
- int t = capture.get(CV_CAP_PROP_FRAME_COUNT);
- int i = 0;
- int width = 0, height = 0, fps = 0;
- width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
- height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
- //fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
- //cout << capture.get(CV_CAP_PROP_FOURCC) << endl;
- //cout << "Input video: (" << width << "x" << height << ") at " << fps << " fps" << endl;
- cout << t << endl;
- /*ofstream outfile;
- outfile.open("C:/Users/ADMIN/Desktop/Tam_OUT_LaiXuanThoi.txt");*/
- //outfile << t << endl;
- int repeat=0;
- //VideoWriter ghi("C:/Users/ADMIN/Desktop/Video_OUT_LaiXuanThoi.avi", capture.get(CV_CAP_PROP_FOURCC), capture.get(CV_CAP_PROP_FPS), Size(width, height), true);
- while (i<t )
- {
- i++;
- Mat src;
- if (i == t - 1)
- {
- if (repeat == 2) break;
- else repeat++;
- i = 0;
- capture.set(CV_CAP_PROP_POS_FRAMES, 0);
- }
- capture.read(src);
- Mat cut = Cat_Hinh_Anh(src);
- cut = XuLy(cut);
- //cout << i << " ";
- Point tam = Process(cut, src);
- cout << i << " ";
- cout << " " << tam.x << " " << tam.y << endl;
- //Ghi ra man hin
- putText(src, NumberToString(tam.x) + "," + NumberToString(tam.y), Point(150, 290), 1, 2, Scalar(255, 255, 0), 2);
- //outfile << i << " " << tam.x << " " << tam.y << endl;
- imshow("Source", src);
- imshow("Cut image", cut);
- //ghi.write(src);
- if (waitKey(30)==27)
- waitKey(0);
- }
- //ghi.release();
- //destroyAllWindows();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement