简体   繁体   中英

OpenCV average direction/angle of line(s)

Im trying to get the angle of the line(s) on each side of my path so I can determine where to go (I want to make sure Im driving straight down it).

In the first image, the "average" direction should be straight down the middle (90deg). In the second, it should be pointing a bit right (60deg).

The way I did it up to now was split the image into two halves, and count the white pixels on each side. If there were more on the left, it would turn right, if there were more on the right, it would turn left.

This has many downsides, though. Can anyone suggest a better way?

在此处输入图片说明

在此处输入图片说明

By eroding and boxing the resulting points, you can calculate the angle:

    cv::Mat img = cv::imread(filename, 0);
    .
    .
    .
    cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 3));
    cv::erode(img, img, element);

    std::vector<cv::Point> points;
    cv::Mat_<uchar>::iterator it = img.begin<uchar>();
    cv::Mat_<uchar>::iterator end = img.end<uchar>();

    for (; it != end; ++it)
       if (*it)
          points.push_back(it.pos());

    cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));

    double angle = box.angle;
    if (angle < -45.)
       angle += 90.;

OpenCV Bouding Box & Skew Angle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM