Reputation: 53
I implemented Houghlinesp in OpenCV using VS 15. The code is as following-
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "\nThis program demonstrates line finding with the Hough transform.\n"
"Usage:\n"
"./houghlines <image_name>, Default is pic1.png\n" << endl;
}
int main(int argc, char** argv)
{
const char* filename = argc >= 2 ? argv[1] : "Turbine.jpg";
Mat src = imread(filename, 0);
if (src.empty())
{
help();
cout << "can not open " << filename << 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, 50, 110, 10);
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);
Point p1, p2;
p1 = Point(l[0], l[1]);
p2 = Point(l[2], l[3]);
//calculate angle in radian, if you need it in degrees just do angle * 180 / PI
double angle = atan2(p1.y - p2.y, p1.x - p2.x);
double angles = angle * 180 / 3.14159265358979323846;
cout << "line coordinates are " << l << endl;
cout << "Angles are " << angles << endl;
}
imshow("source", src);
imshow("detected lines", cdst);
waitKey();
return 0;
}
How can I know which coordinates are which lines? Is it possible to do so? Or maybe I can group closer lines to give me just three blade lines, are their code snippets I can use for this?
Also, I don't understand the angles that have been calculated. [I want the angles with respect to a horizontal line]. Can anyone help me understand this?
Is there any way I can restrict the lines found to the just the blade lines (without restricting the vertical lines found because at another instance the blade can also be in a vertical position)
Any help is appreciated.
Upvotes: 0
Views: 618
Reputation: 181
Lines calculated from HugeLinesP()
give two points which define the line. From there you can calculate function f(x) = k*x + n
which is the definition of a line. (just replace f(x) with y and x with x and you get a system of two equations with two variables).
Coordinate system of cv::Mat starts with 0,0 in upper left corner. While in generic coordinate system where we are use to x growing from left to right and y growing from bottom to top, here we have x still growing from left to right but y goes in reverse and grows from top to bottom. This will affect your angle calculation.
For your last question I can't help you much. First you will somehow have to remove the pole from your image before doing the Huges transform. If the only thing you are interested in are angles, you can just gather all angles that are in some small interval and calculate the average angle. If you are trying to follow the blades in a series of images you can speculate that angle of one blade will not change a lot between two pictures (this means you camera has to take at least two images before the blade does 60 degrees in it's fastest rotation).
Hope this helps
Upvotes: 1