问题
i want to detect the skew level from an image. I've the following code:
public void analyse(CvMat img) {
rows = img.rows();
cols = img.cols();
// create edge-map from rois
CvMat edgeMap = cvCreateMat(rows, cols, CV_8UC1);
cvCanny(img, edgeMap, 100, 400, 3);
// transform to hough
CvMemStorage storage = cvCreateMemStorage(0);
lines = cvHoughLines2(edgeMap, storage, CV_HOUGH_PROBABILISTIC, 1,
EuclideanDistance euclideanDistance = new EuclideanDistance();
double maxDistance = Double.MIN_VALUE;
for (int i = 0; i < lines.total(); ++i) {
Pointer line = cvGetSeqElem(lines, i);
CvPoint pt1 = new CvPoint(line).position(0);
CvPoint pt2 = new CvPoint(line).position(1);
double distance = euclideanDistance.getDistance(pt1, pt2);
double currentAngle = Math.atan2(pt2.y() - pt1.y(),
pt2.x() - pt1.x())
* 180 / Math.PI;
System.out.println(currentAngle);
if (distance > maxDistance) {
skewAngle = currentAngle;
}
}
My test image is
I think the skew level is by -16 degree but my code says, that is by 25...
The for prints out a avg angle by 25,too. Thats wrong with my hough parameters?
//EDIT here is a drawing from the houghLines
greetings
回答1:
Firstly, since you've got a rather clear text, I suggest you used dilate/erode prior to Canny and Hough. You'll get much more points inside the letters, from which Hough will definitely benefit.
Secondly, you choose you "best angle" as a maximum distance between two points of a line.
double distance = euclideanDistance.getDistance(pt1, pt2);
...
if (distance > maxDistance) {
skewAngle = currentAngle;
}
This will NOT work, because in fact Hough may detect an uncorrelated line, which is longer than any line of text:
You may come out with a better algorithm if you display to yourself every step that is made - what you get after Canny, what lines did Hough transform produce etc.
来源:https://stackoverflow.com/questions/12214556/estimate-the-skew-level-with-houghlines