Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To find the angles, use this code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
    Mat src = imread("rot.png");
    if (src.empty())
    {
        cout << "Could not find rot.png" << endl;
        return -1;
    }

    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);

    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1, CV_PI / 180, 1, 50, 10);

    cout << lines.size() << " lines" << endl;

    const double pi = 4.0 * atan(1.0);

    for (size_t i = 0; i < lines.size(); i++)
    {
        Vec4i l = lines[i];
        line(cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);

        double rise = l[3] - l[1];
        double run = l[2] - l[0];
        double slope = rise / run;
        double angle_radians = atan(slope);
        double angle_degrees = angle_radians*180.0f / pi;

        cout << angle_degrees << endl;
    }

    imshow("source", src);
    imshow("detected lines", cdst);

    waitKey();

    return 0;
}

To find the angles, use this code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
    Mat src = imread("rot.png");
    if (src.empty())
    {
        cout << "Could not find rot.png" << endl;
        return -1;
    }

    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);

    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1, CV_PI / 180, 1, 50, 10);

    cout << lines.size() << " lines" << endl;

    const double pi = 4.0 * atan(1.0);

    for (size_t i = 0; i < lines.size(); i++)
    {
        Vec4i l = lines[i];
        line(cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);

        double rise = l[3] - l[1];
        double run = l[2] - l[0];
        double slope = rise / run;
        double angle_radians = atan(slope);
        double angle_degrees = angle_radians*180.0f angle_radians*180.0 / pi;

        cout << angle_degrees << endl;
    }

    imshow("source", src);
    imshow("detected lines", cdst);

    waitKey();

    return 0;
}

To find the angles, angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;

 int main(void)
main()
{
    Mat src frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (src.empty())
(circles.size() < 2)
    {
        cout << "Could not "Warning: Found " << circles.size() << " circles. You should find rot.png" two circles." << endl;
        return -1;
0;
    }

    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);

    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1, CV_PI / 180, 1, 50, 10);

    cout << lines.size() << " lines" << endl;

    const double pi = 4.0 * atan(1.0);

    for (size_t i = 0; i < lines.size(); i++)
    {
        Vec4i l = lines[i];
        line(cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, Point centre1(cvRound(circles[0][0]), cvRound(circles[0][1]));
    Point centre2(cvRound(circles[1][0]), cvRound(circles[1][1]));
    line(frame, centre1, centre2, Scalar(255, 255, 255), 3, CV_AA);
     circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    double rise = l[3] circles[0][1] - l[1];
    circles[1][1];
    double run = l[2] circles[0][0] - l[0];
    circles[1][0];
    double slope = rise / run;
     double angle_radians = atan(slope);
     double angle_degrees = angle_radians*180.0 / pi;

        cout << angle_degrees << endl;
    }

    imshow("source", src);
    imshow("detected lines", cdst);

    waitKey();

CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    Mat frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (circles.size() < 2)
    {
        cout << "Warning: Found "Found " << circles.size() << " circles. You should find two circles." << endl;
        return 0;
    }

    Point centre1(cvRound(circles[0][0]), cvRound(circles[0][1]));
    Point centre2(cvRound(circles[1][0]), cvRound(circles[1][1]));
    line(frame, centre1, centre2, Scalar(255, 255, 255), 3, CV_AA);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    double rise = circles[0][1] - circles[1][1];
    double run = circles[0][0] - circles[1][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    Mat frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (circles.size() < 2)
    {
        cout << "Found " << circles.size() << " circles. You should find two circles." only two." << endl;
        return 0;
    }

    Point centre1(cvRound(circles[0][0]), cvRound(circles[0][1]));
    Point centre2(cvRound(circles[1][0]), cvRound(circles[1][1]));
    line(frame, centre1, centre2, Scalar(255, 255, 255), 3, CV_AA);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    double rise = circles[0][1] - circles[1][1];
    double run = circles[0][0] - circles[1][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main()
main(void)
{
    Mat frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    // Values are very much obtained by trial and error
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (circles.size() < 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point centre1(cvRound(circles[0][0]), cvRound(circles[0][1]));
    Point centre2(cvRound(circles[1][0]), cvRound(circles[1][1]));
    line(frame, centre1, centre2, Scalar(255, 255, 255), 3, CV_AA);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    double rise = circles[0][1] - circles[1][1];
    double run = circles[0][0] - circles[1][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    // Values are very much obtained by trial and error
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (circles.size() < != 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point centre1(cvRound(circles[0][0]), cvRound(circles[0][1]));
    Point centre2(cvRound(circles[1][0]), cvRound(circles[1][1]));
    line(frame, Point2d centre1(circles[0][0], circles[0][1]);
    Point2d centre2(circles[1][0], circles[1][1]);

    circle(frame, centre1, centre2, Scalar(255, 255, 255), 3, CV_AA);
10, Scalar(0, 0, 0), 3);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    // Get pointing vector
    Point2d vec(centre2 - centre1);
    // Rotate pointing vector 
    double x, y;
    double alpha = -(CV_PI / 2);
    x = vec.x*cos(alpha) - vec.y*sin(alpha);
    y = vec.x*sin(alpha) + vec.y*cos(alpha);
    vec.x = x;
    vec.y = y;

    // Get largest dimension
    Point2d output_vec(0, 0);

    if (abs(vec.x) > abs(vec.y))
        output_vec.x = x;
    else
        output_vec.y = y;

    // If y is largest dimension
    if (output_vec.x == 0)
    {
        if (output_vec.y > 0)
            cout << "down" << endl;
        else if (output_vec.y < 0)
            cout << "up" << endl;
    }
    // If x is largest dimension
    else if(output_vec.y == 0)
    {
        if (output_vec.x > 0)
            cout << "right" << endl;
        else if (output_vec.x < 0)
            cout << "left" << endl;
    }

    // Calculate angle
    double rise = circles[0][1] - circles[1][1];
    double run = circles[0][0] - circles[1][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("rot.png");
    cvtColor(frame, frame, CV_BGR2GRAY);

    vector<Vec3f> circles;
    // Values are very much obtained by trial and error
    HoughCircles(frame, circles, CV_HOUGH_GRADIENT, 1, 5, 5, 24, 0, 13);

    if (circles.size() != 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point2d centre1(circles[0][0], circles[0][1]);
    Point2d centre2(circles[1][0], circles[1][1]);

    circle(frame, centre1, 10, Scalar(0, 0, 0), 3);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    // Get pointing vector
    Point2d vec(centre2 - centre1);
    // Rotate pointing vector 
    double x, y;
    double alpha = -(CV_PI / 2);
    x = vec.x*cos(alpha) - vec.y*sin(alpha);
    y = vec.x*sin(alpha) + vec.y*cos(alpha);
    vec.x = x;
    vec.y = y;

    // Get largest dimension
    Point2d output_vec(0, 0);

    if (abs(vec.x) > abs(vec.y))
        output_vec.x = x;
    else
        output_vec.y = y;

    // If y is largest dimension
    if (output_vec.x == 0)
    {
        if (output_vec.y > 0)
            cout << "down" << endl;
        else if (output_vec.y < 0)
            cout << "up" << endl;
    }
    // If x is largest dimension
    else if(output_vec.y == 0)
    {
        if (output_vec.x > 0)
            cout << "right" << endl;
        else if (output_vec.x < 0)
            cout << "left" << endl;
    }

    // Calculate angle
    double rise = circles[0][1] circles[1][1] - circles[1][1];
circles[0][1];
    double run = circles[0][0] circles[1][0] - circles[1][0];
circles[0][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("rot.png");
     cvtColor(frame, frame, CV_BGR2GRAY);

    // Find the two circles.
    vector<Vec3f> circles;
    // Values 
    // Parameter values are very much obtained by trial and error
    HoughCircles(frame, // input
        circles, // output
        CV_HOUGH_GRADIENT, // method
        1, // dp
        5, // minimum distance
        5, // param1
        24, // param2
        0, 13);
// minimum radius
        13); // maximum radius

    if (circles.size() != 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point2d centre1(circles[0][0], circles[0][1]);
    Point2d centre2(circles[1][0], circles[1][1]);

    circle(frame, centre1, 10, Scalar(0, 0, 0), 3);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    // Get pointing vector
    Point2d vec(centre2 - centre1);
    // Rotate pointing vector 
    double x, y;
    double alpha = -(CV_PI / 2);
2); // equal to -90 degrees
    x = vec.x*cos(alpha) - vec.y*sin(alpha);
    y = vec.x*sin(alpha) + vec.y*cos(alpha);
    vec.x = x;
    vec.y = y;

    // Get largest dimension
    Point2d output_vec(0, 0);

    if (abs(vec.x) > abs(vec.y))
        output_vec.x = x;
    else
        output_vec.y = y;

    // If y is largest dimension
    if (output_vec.x == 0)
    {
        if (output_vec.y > 0)
            cout << "down" << endl;
        else if (output_vec.y < 0)
            cout << "up" << endl;
    }
    // If x is largest dimension
    else if(output_vec.y == 0)
    {
        if (output_vec.x > 0)
            cout << "right" << endl;
        else if (output_vec.x < 0)
            cout << "left" << endl;
    }

    // Calculate angle
    double rise = circles[1][1] - circles[0][1];
    double run = circles[1][0] - circles[0][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}

To find the angle, use this (simpler) code:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("rot.png");

    cvtColor(frame, frame, CV_BGR2GRAY);

    // Find the two circles.
    vector<Vec3f> circles;

    // Parameter values are very much obtained by trial and error
    HoughCircles(frame, // input
        circles, // output
        CV_HOUGH_GRADIENT, // method
        1, // dp
        5, // minimum distance
        5, // param1
        24, // param2
        0, // minimum radius
        13); // maximum radius

    if (circles.size() != 2)
    {
        cout << "Found " << circles.size() << " circles. You should find only two." << endl;
        return 0;
    }

    Point2d centre1(circles[0][0], circles[0][1]);
    Point2d centre2(circles[1][0], circles[1][1]);

    circle(frame, centre1, 10, Scalar(0, 0, 0), 3);
    circle(frame, centre2, 10, Scalar(0, 0, 0), 3);

    // Get pointing vector
    Point2d vec(centre2 - centre1);
    // Rotate pointing vector 
    double x, y;
    double alpha = -(CV_PI / 2); // equal to -90 degrees
    x = vec.x*cos(alpha) - vec.y*sin(alpha);
    y = vec.x*sin(alpha) + vec.y*cos(alpha);
    vec.x = x;
    vec.y = y;

    // Get largest dimension
    Point2d output_vec(0, 0);

    if (abs(vec.x) > abs(vec.y))
        output_vec.x = x;
    else
        output_vec.y = y;

    // If y is largest dimension
    if (output_vec.x == 0)
    {
        if (output_vec.y > 0)
            cout << "down" << endl;
        else if (output_vec.y < 0)
            cout << "up" << endl;
    }
    // If x is largest dimension
    else if(output_vec.y == 0)
    {
        if (output_vec.x > 0)
            cout << "right" << endl;
        else if (output_vec.x < 0)
            cout << "left" << endl;
    }

    // Calculate angle
    double rise = circles[1][1] - circles[0][1];
    double run = circles[1][0] - circles[0][0];
    double slope = rise / run;
    double angle_radians = atan(slope);
    double angle_degrees = angle_radians*180.0 / CV_PI;

    imshow("Hough Circles", frame);

    waitKey(0);
    return 0;
}