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和OpenCV颜色检查例程非常慢_Python_Opencv - Fatal编程技术网

Python和OpenCV颜色检查例程非常慢

Python和OpenCV颜色检查例程非常慢,python,opencv,Python,Opencv,我有一个程序,它迭代一组黑色背景的图像,有7个不同颜色的对象 我需要迭代每个像素,通过使用每个坐标的上下边界来找到中间的x点和y点,然后使用这些值来计算一些距离 这些图像的大小为640x480,每幅图像大约需要2秒钟,这看起来相当多。我已经附上了我如何在下面的像素中迭代的代码 def isBlack(r, g, b): return r == 0 and g == 0 and b == 0 def isRed(r, g, b): return r > 0 and g == 0 and b

我有一个程序,它迭代一组黑色背景的图像,有7个不同颜色的对象

我需要迭代每个像素,通过使用每个坐标的上下边界来找到中间的x点和y点,然后使用这些值来计算一些距离

这些图像的大小为640x480,每幅图像大约需要2秒钟,这看起来相当多。我已经附上了我如何在下面的像素中迭代的代码

def isBlack(r, g, b): return r == 0 and g == 0 and b == 0
def isRed(r, g, b): return r > 0 and g == 0 and b == 0
def isYellow(r, g, b): return r > 0 and g > 0 and b == 0 and r == g
def isOrange(r, g, b): return r > 0 and g > 0 and b == 0 and r != g
def isBlue(r, g, b): return r == 0 and g == 0 and b > 0
def isCyan(r, g, b): return r == 0 and g > 0 and b > 0
def isGreen(r, g, b): return r == 0 and g > 0 and b == 0
def isWhite(r, g, b): return r == g == b and r != 0

def getAbsoluteValues (im, side, frame):
    sizes = im.shape
    ny, nx, nc = sizes

    array_of_maxes_x = np.empty((14,))
    array_of_maxes_x[::2] = nx + 1
    array_of_maxes_x[1::2] = -1

    array_of_maxes_x = array_of_maxes_x.reshape(7, 2)
    array_of_maxes_y = array_of_maxes_x.copy()

    for idx_y, y in enumerate(im):
        for idx_x, x in enumerate(y):
            b, g, r = x
            if isBlack(r, g, b):
                continue
            elif isRed(r, g, b):
                array_of_maxes_x[0] = compareLoAndHi(idx_x, array_of_maxes_x[0])
                array_of_maxes_y[0] = compareLoAndHi(idx_y, array_of_maxes_y[0])
            elif isYellow(r, g, b):
                array_of_maxes_x[1] = compareLoAndHi(idx_x, array_of_maxes_x[1])
                array_of_maxes_y[1] = compareLoAndHi(idx_y, array_of_maxes_y[1])
            elif isOrange(r, g, b):
                array_of_maxes_x[2] = compareLoAndHi(idx_x, array_of_maxes_x[2])
                array_of_maxes_y[2] = compareLoAndHi(idx_y, array_of_maxes_y[2])
            elif isBlue(r, g, b):
                array_of_maxes_x[3] = compareLoAndHi(idx_x, array_of_maxes_x[3])
                array_of_maxes_y[3] = compareLoAndHi(idx_y, array_of_maxes_y[3])
            elif isCyan(r, g, b):
                array_of_maxes_x[4] = compareLoAndHi(idx_x, array_of_maxes_x[4])
                array_of_maxes_y[4] = compareLoAndHi(idx_y, array_of_maxes_y[4])
            elif isGreen(r, g, b):
                array_of_maxes_x[5] = compareLoAndHi(idx_x, array_of_maxes_x[5])
                array_of_maxes_y[5] = compareLoAndHi(idx_y, array_of_maxes_y[5])
            elif isWhite(r, g, b) and not isStray(im, idx_x, idx_y):
                array_of_maxes_x[6] = compareLoAndHi(idx_x, array_of_maxes_x[6])
                array_of_maxes_y[6] = compareLoAndHi(idx_y, array_of_maxes_y[6])

def compareLoAndHi(coord, current):
    if coord < current[0]: current[0] = coord
    if coord > current[1]: current[1] = coord

    return current

def isStray(im, x, y):
    values = np.array([[x, y - 1],[x, y + 1],[x - 1, y],[x + 1, y]])

    for i in range(0, 4):
        b, g ,r = im[values[i][1], values[i][0]]
        if(not isBlack(r, g, b) and not isWhite(r, g, b)):
            return True

    return False
                
def为黑色(r,g,b):返回r==0,g==0,b==0
def isRed(r,g,b):返回r>0,g==0,b==0
def为黄色(r,g,b):返回r>0和g>0,b==0和r==g
def isOrange(r、g、b):返回r>0和g>0,b==0和r!=G
def isBlue(r,g,b):返回r==0和g==0,b>0
def isCyan(r,g,b):返回r==0,g>0,b>0
def为绿色(r,g,b):返回r==0,g>0,b==0
def isWhite(r,g,b):返回r==g==b和r!=0
def GetAbsoluteValue(im、侧面、帧):
尺寸=im.shape
ny、nx、nc=尺寸
数组_of_maxes_x=np.empty((14,))
数组_of_maxes_x[::2]=nx+1
_maxes_x[1::2]=-1的数组
最大值的数组=最大值的数组。重塑(7,2)
数组_of_maxes_y=数组_of_maxes_x.copy()
对于idx_y,枚举(im)中的y:
对于idx_x,枚举中的x(y):
b、 g,r=x
如果为黑色(r、g、b):
持续
elif isRed(r、g、b):
最大值的数组\u\u x[0]=compareLoAndHi(idx\u x,最大值的数组\u\u x[0])
_maxes_y[0]的数组_=compareLoAndHi(idx_y,_maxes_y[0]的数组_)
elif为黄色(r、g、b):
_maxes_x[1]的数组_=compareLoAndHi(idx_x,_maxes_x[1]的数组_)
_maxes_y[1]的数组_=compareLoAndHi(idx_y,_maxes_y[1]的数组_)
elif等值线(r、g、b):
_maxes_x[2]的数组_=compareLoAndHi(idx_x,_maxes_x[2]的数组_)
数组\u of_maxes\u y[2]=compareLoAndHi(idx\u y,数组\u of_maxes\u y[2])
elif isBlue(r、g、b):
最大值的数组\u\u x[3]=compareLoAndHi(idx\u x,最大值的数组\u\u x[3])
_maxes_y[3]的数组_=compareLoAndHi(idx_y,_maxes_y[3]的数组_)
埃利夫·伊斯克扬(r、g、b):
_maxes_x[4]的数组_=compareLoAndHi(idx_x,_maxes_x[4]的数组_)
_maxes_y[4]的数组_=compareLoAndHi(idx_y,_maxes_y[4]的数组_)
elif isGreen(r、g、b):
_maxes_x[5]的数组_=compareLoAndHi(idx_x,_maxes_x[5]的数组_)
数组\u of_maxes\u y[5]=compareLoAndHi(idx\u y,数组\u of_maxes\u y[5])
elif为白色(r、g、b),而非isStray(im、idx_x、idx_y):
_maxes_x[6]的数组_=compareLoAndHi(idx_x,_maxes_x[6]的数组_)
_maxes_y[6]的数组_=compareLoAndHi(idx_y,_maxes_y[6]的数组_)
def compareLoAndHi(协调,当前):
如果坐标<当前[0]:当前[0]=坐标
如果坐标>当前[1]:当前[1]=坐标
回流
def托盘(im、x、y):
values=np.数组([[x,y-1],[x,y+1],[x-1,y],[x+1,y]]
对于范围(0,4)内的i:
b、 g,r=im[值[i][1],值[i][0]]
如果(非黑色(r,g,b)和非白色(r,g,b)):
返回真值
返回错误
我不知道如何使这更快,我一直在寻找矩阵例程和一切,但我找不到一个适合我的问题的答案

下面是一个示例图像


尝试对相同的图像(如320x240)使用较小的分辨率,并查看迭代所需的时间。我认为这会将时间减少一半。

您可以只检查检测到的轮廓的颜色

将numpy导入为np
进口cv2
image=cv2.imread('image.png')
cv2.imshow(“图像”,图像)
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
_,threshold=cv2.threshold(灰色,0,255,cv2.THRESH_二进制)
cv2.imshow(“阈值”,阈值)
轮廓,层次=cv2.findContours(阈值,cv2.RETR\u外部,cv2.CHAIN\u近似值\u简单)
空白图像=np.类零(图像)
对于轮廓中的cnt:
M=cv2.力矩(cnt)
cX=int(M[“m10”]/M[“m00”])
cY=int(M[“m01”]/M[“m00”])
颜色=(int(图像[cY,cX,0]),int(图像[cY,cX,1]),int(图像[cY,cX,2]))
打印(f’point:({cX},{cY}),颜色(BGR:{color})
cv2.圆圈(空白图像,(cX,cY),2,彩色,2)
cv2.imshow(“轮廓图像”,空白图像)
cv2.等待键(0)
cv2.destroyAllWindows()
输出:

point: (464,219),  color (BGR): (0, 156, 213)
point: (368,220),  color (BGR): (0, 215, 0)
point: (388,197),  color (BGR): (217, 217, 217)
point: (384,176),  color (BGR): (211, 0, 0)
point: (338,176),  color (BGR): (111, 238, 238)
point: (333,171),  color (BGR): (215, 215, 0)
point: (366,143),  color (BGR): (2, 2, 216)
X_Cordinates : 
 [105 105 106 106 106 106 107 107 107 107 108 108 108 108 109 109 109 109
 110 110 126 126 126 127 127 127 127 128 128 128 128 128 129 129 129 129
 130 130 130 130 130 130 130 131 131 131 131 131 131 131 131 132 132 132
 132 132 132 132 132 133 133 133 133 133 133 133 133 134 134 134 134 134
 146 147 147 147 147 147 148 148 148 148 148 149 149 149 149 149 163 163
 163 163 163 163 163 163 164 164 164 164 164 164 164 164 164 165 165 165
 165 165 165 165 165 165 165 166 166 166 166 166 166 166 166 166 167 167
 167 167 167 167 167 167]

Y_Cordinates : 
 [274 275 273 274 275 276 273 274 275 276 273 274 275 276 273 274 275 276
 274 275 249 250 251 249 250 251 252 248 249 250 251 252 249 250 251 252
 249 250 251 253 254 288 289 252 253 254 255 287 288 289 290 252 253 254
 255 287 288 289 290 252 253 254 255 287 288 289 290 252 253 254 288 289
 291 289 290 291 292 293 289 290 291 292 293 289 290 291 292 293 275 276
 277 278 347 348 349 350 275 276 277 278 346 347 348 349 350 274 275 276
 277 278 346 347 348 349 350 275 276 277 278 346 347 348 349 350 275 276
 277 278 347 348 349 350]


此外,您只能迭代非零点

将numpy导入为np
进口cv2
image=cv2.imread('image.png')
cv2.imshow(“图像”,图像)
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
X_Cordinates,Y_Cordinates=np.非零(灰色)
打印(“X\u Cordinates:\n”,X\u Cordinates)
打印(“\nY\u Cordinates:\n”,Y\u Cordinates)
输出:

point: (464,219),  color (BGR): (0, 156, 213)
point: (368,220),  color (BGR): (0, 215, 0)
point: (388,197),  color (BGR): (217, 217, 217)
point: (384,176),  color (BGR): (211, 0, 0)
point: (338,176),  color (BGR): (111, 238, 238)
point: (333,171),  color (BGR): (215, 215, 0)
point: (366,143),  color (BGR): (2, 2, 216)
X_Cordinates : 
 [105 105 106 106 106 106 107 107 107 107 108 108 108 108 109 109 109 109
 110 110 126 126 126 127 127 127 127 128 128 128 128 128 129 129 129 129
 130 130 130 130 130 130 130 131 131 131 131 131 131 131 131 132 132 132
 132 132 132 132 132 133 133 133 133 133 133 133 133 134 134 134 134 134
 146 147 147 147 147 147 148 148 148 148 148 149 149 149 149 149 163 163
 163 163 163 163 163 163 164 164 164 164 164 164 164 164 164 165 165 165
 165 165 165 165 165 165 165 166 166 166 166 166 166 166 166 166 167 167
 167 167 167 167 167 167]

Y_Cordinates : 
 [274 275 273 274 275 276 273 274 275 276 273 274 275 276 273 274 275 276
 274 275 249 250 251 249 250 251 252 248 249 250 251 252 249 250 251 252
 249 250 251 253 254 288 289 252 253 254 255 287 288 289 290 252 253 254
 255 287 288 289 290 252 253 254 255 287 288 289 290 252 253 254 288 289
 291 289 290 291 292 293 289 290 291 292 293 289 290 291 292 293 275 276
 277 278 347 348 349 350 275 276 277 278 346 347 348 349 350 274 275 276
 277 278 346 347 348 349 350 275 276 277 278 346 347 348 349 350 275 276
 277 278 347 348 349 350]

Python循环通常非常慢。此外,您能否提供所需的所有功能,以便我们可以运行您的示例(例如,
compareLoAndHi
isXXX
)?请共享具有代表性的示例图像,以便我们了解我们正在处理的内容。谢谢。@JérômeRichard我知道,但显然使用NumPy会使它更快,但我不知道im数组是如何输入的NumPy@MarkSetchell我已经添加了该图像。请查看OpenCV
findContours()
和“中心时刻”。这样做是不可能的,因为它会影响形状谢谢你的回答,所以对于第二种方法,坐标是(x[0],y[0]),(x[1],y[1])。。(x[n],y[n])对吗?这就是我想做的。还有,(x[0],y[0]),(x[1],y[1])的调整大小是什么。。(x[n],y[n])这是正确的,您可以删除调整大小。