estimate the skew level with houghLines

2019-08-16 16:32发布

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 skew

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 enter image description here

greetings

1条回答
老娘就宠你
2楼-- · 2019-08-16 16:59

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:

Hough fail

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.

查看更多
登录 后发表回答