python:cv2.threshold将numpy更改为元组

时间:2017-04-18 21:15:13

标签: python opencv numpy motion-detection

我在第一个函数中处理每个帧,我在使用cv2.threshold(self.res, 10, 255, cv2.THRESH_BINARY_INV)进行预处理之前检测到运动。在下一个函数中,我只是计算黑色像素的数量并计算平均值。我给cv2.threshold一个numpy数组,但它返回tuple,当我尝试计算黑色像素if self.res[x,y] == 0.0:时,我得到此类型错误tuple indices must be integers, not tuple。已经两天但没有运气。

def processImage(self, frame):

        self.frame2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        self.res = cv2.absdiff(self.frame1gray, self.frame2gray)
        self.res = cv2.GaussianBlur(self.res, (15, 15), 0)
        kernel = np.ones((3, 3), np.uint8)
        self.res = cv2.morphologyEx(self.res, cv2.MORPH_OPEN, kernel)
        self.res = cv2.morphologyEx(self.res, cv2.MORPH_GRADIENT, kernel)
        self.res = cv2.threshold(self.res, 10, 255, cv2.THRESH_BINARY_INV)

        print "type: ",type(self.res)

def somethingHasMoved(self):

        nb=0 #Will hold the number of black pixels
        for x in range(self.height): #Iterate the hole image
            for y in range(self.width):

                if self.res[x,y] == 0.0:
                    nb += 1
        avg = (nb*100.0)/self.nb_pixels #Calculate the average of black pixel in the image
    print"average movement : ",avg
        if avg > self.threshold:#If over the ceiling trigger the alarm
            return True
        else:
            return False

0 个答案:

没有答案