Opencv如果对象消失了,为什么轮廓仍保留?

8cdiaqws  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(123)

我在网上找到了一个预先写好的物体检测程序。我想用这个程序在装配线上制作一个塑料瓶盖传感器,它会告诉我瓶盖的尺寸。只有当你把瓶盖从传送带上移走时,从轮廓上得到的坐标才会留在那里。有什么想法吗?

import cv2
from object_detector import *
import numpy as np
import time

# Load Object Detector
detector = HomogeneousBgDetector()

# Load Cap
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)

while True:
    _, img = cap.read()
    img = img[0:600, 60:700 ]

    contours = []
    contours = detector.detect_objects(img)

        # Draw objects boundaries
    for cnt in contours:
        # Get rect
        rect = cv2.minAreaRect(cnt)
        (x, y), (w, h), angle = rect

        # Get Width and Height of the Objects by applying the Ratio pixel to cm
        object_width = w
        object_height = h

        # Display rectangle
        box = cv2.boxPoints(rect)
        box = np.int0(box)

    cv2.circle(img, (int(x), int(y)), 5, (0, 0, 255), -1)
    cv2.polylines(img, [box], True, (255, 0, 0), 2)
    cv2.putText(img, "Width {} px".format(round(object_width, 1)), (int(x - 100), int(y - 20)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)
    cv2.putText(img, "Height {} px".format(round(object_height, 1)), (int(x - 100), int(y + 15)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)

    cv2.imshow("Image", img)
    key = cv2.waitKey(1)

    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

对象_检测器.py

import cv2

class HomogeneousBgDetector():
    def __init__(self):
        pass

    def detect_objects(self, frame):
        def difference_of_Gaussians(img, k1, s1, k2, s2):
            b1 = cv2.GaussianBlur(img,(k1, k1), s1)
            b2 = cv2.GaussianBlur(img,(k2, k2), s2)
            return b1 - b2      
        # Convert Image to grayscale
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        DoG_img = difference_of_Gaussians(gray, 7, 7, 17, 13)
        
        # Create a Mask with adaptive threshold
        #mask = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 19, 5)
        mask =  cv2.threshold(DoG_img ,130,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

        # Find contours
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        cv2.imshow("mask", mask)
        objects_contours = []

        for cnt in contours:
            area = cv2.contourArea(cnt)
            if area > 2000:
                
                objects_contours.append(cnt)

        return objects_contours

我试图创建轮廓变量空之前,但失败了:

....
_, img = cap.read()
img = img[0:600, 60:700 ]

contours = []# here
contours = detector.detect_objects(img)

    # Draw objects boundaries
for cnt in contours:
.....

图片:

jdzmm42g

jdzmm42g1#

Eran和Christoph是对的。如果没有找到轮廓,最后一个位置仍然在全局变量xy中。也许您想增加绘制圆、直线和文本的四行的缩进,以便它属于上面的for循环?

....
    img = img[0:600, 60:700 ]

    contours = detector.detect_objects(img)

    # Draw objects boundaries
    for cnt in contours:
....
        # Display rectangle
        box = cv2.boxPoints(rect)
        box = np.int0(box)

        # keep indent here
        cv2.circle(img, (int(x), int(y)), 5, (0, 0, 255), -1)
        cv2.polylines(img, [box], True, (255, 0, 0), 2)
        cv2.putText(img, "Width {} px".format(round(object_width, 1)), (int(x - 100), int(y - 20)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)
        cv2.putText(img, "Height {} px".format(round(object_height, 1)), (int(x - 100), int(y + 15)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)
....

相关问题