Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV Python图像处理,类型错误_Opencv - Fatal编程技术网

OpenCV Python图像处理,类型错误

OpenCV Python图像处理,类型错误,opencv,Opencv,我使用下面提到的代码来跟踪棕色磁珠。行cv2.圆(帧,中心,5,(0,0255),-1)给出了一个类型错误。有人能提供这方面的见解吗 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

我使用下面提到的代码来跟踪棕色磁珠。行
cv2.圆(帧,中心,5,(0,0255),-1)
给出了一个类型错误。有人能提供这方面的见解吗

    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()

类型错误
在未提供预期类型的数据时发生。除预期类型之外的任何其他类型都会导致此错误

您必须从以下位置修改行:

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

致:

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


您没有将type
int
包含到中心的第二个坐标,因此您一直收到此错误。

尝试打印这一行
center=(int(M[“m10”]/M[“m00”],(M[“m01”]/M[“m00”])
并查看是否得到非零值是的,我得到的是非零整数值。中心=(450800.0)该值(450800.0)具有浮点值。将其转换为类型
int
,这就是为什么会出现
类型错误
谢谢!成功了。现在它给了我一个pts.appendleft(中)的名称错误。@marcosidaho这行有什么用?我的意思是,你打算用它做什么?这一行用来将检测到的斑点的所有质心附加到列表“pts”中。要附加到pts,请使用:
pts=np。append(center)
Hi Luke,我必须在中心的同时定义另一个参数,以便将值附加到pts列表中。代码似乎在工作,但我无法跟踪对象。你能对此提供一些见解吗?