简体   繁体   中英

Generalized Hough Transform in OpenCV - set angle precision

I have a problem with Generalized Hough Transform (Guil version) in OpenCV. My code:

def generalized_Hough():
    img = cv2.imread("img.png")
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    template = cv2.imread("template.png")
    height, width = template.shape[:2]

    edges = cv2.Canny(template, 200, 250)
    ght = cv2.createGeneralizedHoughGuil()
    ght.setTemplate(edges)

    ght.setMinDist(100)
    ght.setMinAngle(0)
    ght.setMaxAngle(360)
    ght.setAngleStep(1)
    ght.setLevels(360)
    ght.setMinScale(1)
    ght.setMaxScale(1.3)
    ght.setScaleStep(0.05)
    ght.setAngleThresh(100)
    ght.setScaleThresh(100)
    ght.setPosThresh(100)
    ght.setAngleEpsilon(1)
    ght.setLevels(360)
    ght.setXi(90)

    positions = ght.detect(img_gray)[0][0]

    for position in positions:
        center_col = int(position[0])
        center_row = int(position[1])
        scale = position[2]
        angle = int(position[3])

        found_height = int(height * scale)
        found_width = int(width * scale)

        rectangle = ((center_col, center_row),
                     (found_width, found_height),
                     angle)

        box = cv2.boxPoints(rectangle)
        box = np.int0(box)
        cv2.drawContours(img, [box], 0, (0, 0, 255), 2)

        for i in range(-2, 3):
            for j in range(-2, 3):
                img[center_row + i, center_col + j] = 0, 0, 255

    cv2.imwrite("results.png", img)

So I read image and template, get template as Canny edges, then configure GHT (Guil version for scale and angle) and plot detections. The problem is that the results are always snapped to nearest 90 degrees, despite setting angle min, max and step to 0, 360 and 1, respectively:

[[294. 110.   1. 270.]
 [100. 303.   1.   0.]
 [561. 312.   1.   0.]
 [461. 126.   1.  90.]
 [194. 109.   1.   0.]]

Image example:

在此处输入图像描述

Template example:

在此处输入图像描述

Results:

在此处输入图像描述

It's clearly visible that upper left and upper right detections are in more or less correct places and scales, but angle is wrong. How can I fix that?

ght.setXi(90) 将角度设置为 90 度差值更改为 1 度

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM