opencv-rectangle检测忽略额外

b1payxdu  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(210)

我正在尝试创建一个算法,用于检测从机器人摄像头采集的opencv中的矩形。因此,由于机器人的移动,我自然会得到相当多的变化。使用此代码:

def original_settings(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    #cv.imshow("Gray", gray)
    blur = cv.medianBlur(gray,5)
    #cv.imshow("Blur", blur)
    sharpen = cv.filter2D(blur, -1, sharpen_kernel)
    #cv.imshow("Sharpen", sharpen)
    return sharpen

    def find_square(img):
    thresh = cv.threshold(img, 160, 255, cv.THRESH_BINARY_INV)[1]
    #cv.imshow("Threshhold", thresh)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    close = cv.morphologyEx(thresh, cv.MORPH_CLOSE, kernel, iterations=2)
    #cv.imshow("Morphology Search", close)
    close = cv.bitwise_not(close)
    #cv.imshow("Invert Search", close)
    cnts = cv.findContours(close, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]

    min_area = 3500
    max_area = 5000
    image_number = 0
    for c in cnts:
        area = cv.contourArea(c)
        #print(area)
        if area > min_area and area < max_area:
            x, y, w, h = cv.boundingRect(c)
            cv.rectangle(image, (x, y), (x + w, y + h), (36, 255, 12), 2)
            print("Width: {}, Height: {}, x: {}, y: {}".format(w,h,x,y))
            image_number += 1
            #print((x, y))
            return True
    return False

    find_square(original_settings(image))

我基本上是成功的与我的形象,但我有一个情况,我不能很准确地指出。当我处理这个图像时,由于反射,它看起来有一个额外的矩形位,我的算法捕捉到了整个过程。而不是像这样的大矩形。
如何让opencv忽略这个额外的矩形块?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题