Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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

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
Python 质心计算程序中的时滞_Python_Opencv - Fatal编程技术网

Python 质心计算程序中的时滞

Python 质心计算程序中的时滞,python,opencv,Python,Opencv,我试图使用OpenCV和Python确定一个特定对象的质心。 我使用以下代码,但是计算质心花费了太多时间。 我需要一种更快的方法——我应该改变相机的分辨率以提高计算速度吗? 这是我的代码: meanI=[0] meanJ=[0] #taking infinite frames continuously to make a video while(True): ret, frame = capture.read() rgb_image = cv2.cvtColor(frame ,

我试图使用OpenCV和Python确定一个特定对象的质心。 我使用以下代码,但是计算质心花费了太多时间。 我需要一种更快的方法——我应该改变相机的分辨率以提高计算速度吗? 这是我的代码:

meanI=[0]
meanJ=[0]

#taking infinite frames continuously to make a video
while(True):
    ret, frame = capture.read()
    rgb_image = cv2.cvtColor(frame , 0)
    content_red = rgb_image[:,:,2] #red channel of image
    content_green = rgb_image[:,:,1] #green channel of image
    content_blue = rgb_image[:,:,0] #blue channel of image
    r = rgb_image.shape[0] #gives the rows of the image matrix
    c = rgb_image.shape[1] # gives the columns of the image matrix
    d = rgb_image.shape[2] #gives the depth order of the image matrux
    binary_image = np.zeros((r,c),np.float32)
    for i in range (1,r):  #thresholding the object as per requirements
        for j in range (1,c):
            if((content_red[i][j]>186) and (content_red[i][j]<230) and \
               (content_green[i][j]>155) and (content_green[i][j]<165) and \
               (content_blue[i][j]> 175) and (content_blue[i][j]< 195)):
                binary_image[i][j] = 1
                meanI.append(i)
                meanJ.append(j)

    cv2.imshow('frame1',binary_image)
    cv2.waitKey()
    cox = np.mean(meanI) #x-coordinate of centroid
    coy = np.mean(meanJ) #y-coordinate of centroid
meanI=[0]
平均值=[0]
#连续拍摄无限帧以制作视频
虽然(正确):
ret,frame=capture.read()
rgb_图像=cv2.CVT颜色(帧,0)
content_red=rgb_图像[:,:,2]#图像的红色通道
content_green=rgb_图像[:,:,1]#图像的绿色通道
content_blue=rgb_图像[:,:,0]#图像的蓝色通道
r=rgb_图像。形状[0]#给出图像矩阵的行
c=rgb_图像。形状[1]#给出图像矩阵的列
d=rgb_图像。形状[2]#给出图像材质的深度顺序
二进制图像=np.zeros((r,c),np.float32)
对于范围(1,r)内的i:#根据要求对对象设定阈值
对于范围(1,c)内的j:
如果((含量红[i][j]>186)和(含量红[i][j]155)以及(含量绿[i][j]175)和(含量蓝[i][j]<195)):
二值_图像[i][j]=1
平均数。附加(i)
平均附加(j)
cv2.imshow('frame1',二进制图像)
cv2.waitKey()
cox=np.平均值(平均值)#质心的x坐标
coy=np.平均值(平均值j)#质心的y坐标

正如您所发现的那样,Python中的嵌套循环非常复杂。最好避免使用嵌套循环对每个像素进行迭代。幸运的是,OpenCV有一些内置函数,它们完全可以实现您想要实现的功能:,它可以创建位于指定边界之间的像素的二值图像,您可以使用它来计算二值图像的质心。我强烈建议阅读OpenCV,了解图书馆提供的内容

将这两个函数组合在一起可生成以下代码:

import numpy as np
import cv2

lower = np.array([175, 155, 186], np.uint8) # Note these ranges are BGR ordered
upper = np.array([195, 165, 230], np.uint8)
binary = cv2.inRange(im, lower, upper) # im is your BGR image
moments = cv2.moments(binary, True)
cx = moments['m10'] / moments['m00']
cy = moments['m01'] / moments['m00']

cx
cy
是图像质心的x坐标和y坐标。这个版本比使用嵌套循环快了3000倍。

看看cv2.FindOntours()和cv2.moments(),非常感谢@奥雷利乌斯,贝拉克