OpenCV2 - 使用SimpleBlobDetector找到的窄矩形,但无法提取角度

时间:2017-03-29 10:05:13

标签: python opencv raspberry-pi

我试图在多米诺骨牌中获得中线的角度。 SimpleBlobDetector可以找到它,但它给我一个-1的角度值。 有谁知道为什么会这样?我怎么能解决仍然使用SimpleBlobDetector?如果SimpleBlobDetector不是一个选项,我还能做些什么来找到这些线的中心和角度?

import numpy as np
import cv2
import os
import time
#from matplotlib import pyplot as plt

def main():
    capWebcam = cv2.VideoCapture(0)         # declare a VideoCapture object and associate to webcam, 0 => use 1st webcam

    params = cv2.SimpleBlobDetector_Params()
    params.filterByCircularity = True
    params.minCircularity = 0.73

    params2 = cv2.SimpleBlobDetector_Params()
    params2.filterByInertia = True
    params2.maxInertiaRatio = 0.3

                    # show original resolution
    print "default resolution = " + str(capWebcam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "x" + str(capWebcam.get(cv2.CAP_PROP_FRAME_HEIGHT))

    capWebcam.set(cv2.CAP_PROP_FRAME_WIDTH, 320.0)              # change resolution to 320x240 for faster processing
    capWebcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240.0)
    capWebcam.set(cv2.CAP_PROP_FPS, 1)


                                            # show updated resolution
    print "updated resolution = " + str(capWebcam.get(cv2.CAP_PROP_FRAME_WIDTH)) + "x" + str(capWebcam.get(cv2.CAP_PROP_FRAME_HEIGHT))

    if capWebcam.isOpened() == False:               # check if VideoCapture object was associated to webcam successfully
        print "error: capWebcam not accessed successfully\n\n"      # if not, print error message to std out
        os.system("pause")                                          # pause until user presses a key so user can see error message
        return                                                      # and exit function (which exits program)
    # end if

    while cv2.waitKey(1) != 27 and capWebcam.isOpened():            # until the Esc key is pressed or webcam connection is lost
        blnFrameReadSuccessfully, img = capWebcam.read()            # read next frame

        if not blnFrameReadSuccessfully or img is None:     # if frame was not read successfully
            print "error: frame not read from webcam\n"             # print error message to std out
            os.system("pause")                                      # pause until user presses a key so user can see error message
            break                                                   # exit while loop (which exits program)

        det = cv2.SimpleBlobDetector_create(params)
        det2 = cv2.SimpleBlobDetector_create(params2)


        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)



        time.sleep(0.1)
        keypts = det.detect(gray)
        time.sleep(0.1)
        keypts2 = det2.detect(gray)

        print len(keypts)
        print len(keypts2)

        for l in keypts2:
            print l.pt
            print l.angle                




        time.sleep(0.3)
        imgwk = cv2.drawKeypoints(gray, keypts, img,color= (200,0,139),flags=2)

        time.sleep(0.1)
        imgwk2 = cv2.drawKeypoints(img, keypts2, gray,color= (200,0,139),flags=2)



        cv2.namedWindow("img", cv2.WINDOW_AUTOSIZE)           # or use WINDOW_NORMAL to allow window resizing

        cv2.imshow("img", img)

                              # hold windows open until user presses a key

    cv2.destroyAllWindows()

    return


###################################################################################################
if __name__ == "__main__":
    main()

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定SimpleBlob探测器是否最适合这种情况,但你看过

Detect centre and angle of rectangles in an image using Opencv

它似乎与你的问题非常相似,似乎在识别矩形的角度方面做得很好