OpenCV-Python图像处理,类型错误

时间:2017-04-25 13:57:34

标签: opencv

我使用下面提到的代码来跟踪棕色磁珠。第cv2.circle(frame, center, 5 ,(0,0,255),-1)行给出了一个Type错误。任何人都可以提供这方面的见解吗?

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    import imutils

    black_lower = (0,0,0) #this two-blacks corresponds to the colour range in  hsv, which we are looking in ants
    black_upper = (130, 130, 128)
    kernel_erode = np.ones((1,1),np.uint8) #these kernels are defined for eroding and dialating image
    kernel_dialate = np.ones((3,3),np.uint8)
    r,h,c,w = 600, 400, 100, 700 # i have to play with the size of the window for getting the roi (currently cutting defined roi)
    track_window = (c,r,w,h)
    vid = cv2.VideoCapture('/home/idaho/Desktop/opencv_files/brown_beads.mp4')
    while (vid.isOpened()):
        ret,frame = vid.read()
        frame = imutils.resize(frame) #we have resized the frame, now we have to convert to hsv color space!
        hsv_frame = cv2.cvtColor(frame,cv2.COLOR_RGB2HSV) # we have converted the frame in to hsv space!
        mask1 = cv2.inRange(hsv_frame,black_lower,black_upper)#now we should define a mask for color ranges from black_lower to black_upper and remove other noices!
        mask2 = cv2.erode(mask1,kernel_erode,iterations=1) #for increasing the object detection , we have to reduce the erode_kernel_size and increase the dialate_kernel_size
        mask3 = cv2.dilate(mask2,kernel_dialate,iterations=2)
        mask4 = cv2.GaussianBlur(mask3,(1,1),0)
        x,y,w,h = track_window # these are for defining the roi in the video, will have to play with this!
        cv2.rectangle(mask4,(x,y),(x+w,y+h),(255,0,0),2)
        dst = np.zeros_like(mask4)
        dst[y:y+h,x:x+w] = mask4[y:y+h,x:x+w]
        cv2.imshow('gray',frame)
        #this is finding the specific object in the video and drawing a contour against it
        counts = cv2.findContours(mask4.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        center = None
        if len(counts) > 0:
            calculate = max(counts,key = cv2.contourArea)
            ((x,y),radius) = cv2.minEnclosingCircle(calculate)
            M = cv2.moments(calculate)
            center = (int(M["m10"]/M["m00"]),(M["m01"]/M["m00"]))
            if radius > 1:
                cv2.circle(frame, (int(x),int(y)),int(radius),(0,255,255),2)
                cv2.circle(frame, center, 5 ,(0,0,255),-1)
        pts.appendleft(center)
        for i in xrange(1,len(pts)):
            if pts[i-1] is None or pts[i] is None:
                continue
                thickness = int(np.sqrt(64/float(i+1))*2.5)
                cv2.line(frame,pts[i-1],pts[i],(0,0,255),thickness)
        cv2.imshow('frame',frame)
        if cv2.waitKey(50) and 0xFF == ord('q'):
            break
    vid.release()
    cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

如果未提供预期的数据类型,则会出现

Type error。除预期类型之外的任何其他类型都会导致此错误。

您必须修改以下行:

center = (int(M["m10"]/M["m00"]),(M["m01"]/M["m00"]))

为:

center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"]))

您没有在中心的第二个坐标中包含int类型,因此您一直都会收到此错误。