Advertisement
jack06215

[OpenCV] line segment detector example

Jul 8th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include "lsd.h"
  2.  
  3. #include <opencv2/videoio.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <opencv2/core.hpp>
  6. #include <opencv2/imgproc.hpp>
  7.  
  8. #include <string>
  9. #include <iostream>
  10. #include <thread>
  11. #include <chrono>
  12.  
  13. template<typename T>
  14. void initWebcam(cv::VideoCapture& videoCapture, T cameraNumber)
  15. {
  16.     // Get access to the default camera.
  17.     try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
  18.         videoCapture.open(cameraNumber);
  19.     }
  20.     catch (cv::Exception& e) {
  21.         std::cerr << "ERROR: Could not access the camera!" << std::endl;
  22.         exit(1);
  23.     }
  24.     if (!videoCapture.isOpened()) {
  25.         std::cerr << "ERROR: Could not access the camera!" << std::endl;
  26.         exit(1);
  27.     }
  28.     std::cout << "Loaded camera " << cameraNumber << "." << std::endl;
  29. }
  30.  
  31. int main() {
  32.     cv::Mat frame;
  33.     cv::VideoCapture cap;
  34.     initWebcam(cap, "up1.mp4");
  35.     std::this_thread::sleep_for(std::chrono::seconds(1));
  36.  
  37.     cap >> frame;
  38.     while (!frame.empty()) {
  39.         cv::Mat tmp, src_gray;
  40.         frame.copyTo(src_gray);
  41.         cv::cvtColor(frame, tmp, cv::COLOR_RGB2GRAY);
  42.         tmp.convertTo(src_gray, CV_64FC1);
  43.         int cols = src_gray.cols;
  44.         int rows = src_gray.rows;
  45.  
  46.         image_double image = new_image_double(cols, rows);
  47.         image->data = src_gray.ptr<double>(0);
  48.         ntuple_list ntl = lsd(image);
  49.  
  50.         cv::Mat lsd = cv::Mat::zeros(rows, cols, CV_8UC1);
  51.         cv::Point pt1, pt2;
  52.         for (int j = 0; j != ntl->size; ++j) {
  53.             pt1.x = ntl->values[0 + j * ntl->dim];
  54.             pt1.y = ntl->values[1 + j * ntl->dim];
  55.             pt2.x = ntl->values[2 + j * ntl->dim];
  56.             pt2.y = ntl->values[3 + j * ntl->dim];
  57.             cv::line(lsd, pt1, pt2, cv::Scalar(255), 1);
  58.         }
  59.  
  60.         free_ntuple_list(ntl);
  61.         cv::imshow("Result", frame);
  62.         cv::namedWindow("lsd", cv::WINDOW_AUTOSIZE);
  63.         cv::imshow("lsd", lsd);
  64.         if (cv::waitKey(30) >= 0)
  65.             break;
  66.  
  67.         cap >> frame;
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement