I'm trying to detect the skew angle of a text in an image link. The problem is that this post is written in C++, and I'm having trouble converting some things to Java.
I did the implementation of Hugh Transform. Most of the conversion to java I did guiding me from this post (1). But it's not working fine. Is giving an angle of 0.27919363 when is supposed to give an angle of 15.9882. This is the image I'm working with.
And this is my code:
public double compute_skew1(String filename){
Log.d(TAG, "Computing skew 1");
Mat src = Highgui.imread(filename, 0);
Size size = src.size();
//double minLineSize = 20;
double minLineSize = src.width() / 2.f;
Core.bitwise_not(src, src);
Mat lines = new Mat();
double angle = 0.;
try {
Imgproc.HoughLinesP(src, lines, 1, Math.PI / 180, 100, minLineSize, 20);
Mat disp_lines = new Mat(size, CvType.CV_8UC1, new Scalar(0, 0, 0));
int nb_lines = lines.cols();
for (int i = 0; i < nb_lines; i++) {
double[] vec = lines.get(0, i);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Core.line(disp_lines, start, end, new Scalar(255,0,0));
angle += Math.atan2(y2 - y1, x2 - x1);
}
angle /= nb_lines; // mean angle, in radians.*/
//Log.d(TAG, "ANGLE: "+angle);
Log.d(TAG, "ANGLE: "+ angle * 180 / Math.PI);
} catch (Exception e) {
Log.e(TAG, "Error in compute_skew1");
Log.e(TAG, e.getMessage());
}
return angle;
}
I'm almost certain the problem is with this line "int nb_lines = lines.cols();" since the original line is "unsigned nb_lines = lines.size();", java doesn't jave unsigned variables, and that's the way it works in the post(1). Also, I don't quite understand this line "double[] vec = lines.get(0, i);" but that's the way it worked in the post (1) as well. What am I doing wrong?
Also, after I get the angle, I need to do the rotation or deskewing of the text, and I also have some trouble with this conversion, especially with this part of the code:
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());
With the help of this post2, I believed this to be the conversion:
List <Point> points = new ArrayList<Point>();
for (int i = 0; i < img.rows(); i++) {
for (int j = 0; j < img.cols(); j++) {
double pixel = img.get(i, j)[0];
if (pixel != 0.0)
points.add(new Point(i,j));
}
}
But does not work, there is never a pixel = 0.0 and so the p array just fills with every pixel.
So. Please let me know what I'm doing wrong. Thanks in advance.