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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.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:使用r>;提取像素;b>;G_Python_Opencv_Image Processing_Mask - Fatal编程技术网

Python opencv:使用r>;提取像素;b>;G

Python opencv:使用r>;提取像素;b>;G,python,opencv,image-processing,mask,Python,Opencv,Image Processing,Mask,我试图用python中的opencv从图像中提取只有r>b>g的像素。我不想使用.split(),因为它太慢了。有人能帮我吗 我尝试过这样的事情:(但太慢了) 编辑,我想这样做: img[img[2]>img[1] and img[1]>img[0]]=0 我不确定这是否给出了预期的结果,但这对我来说没有错误 img = cv2.imread(...) img[ (img[:,:,2] > img[:,:,1]) & (img[:,:,1] > img[:,

我试图用python中的opencv从图像中提取只有r>b>g的像素。我不想使用.split(),因为它太慢了。有人能帮我吗

我尝试过这样的事情:(但太慢了)

编辑,我想这样做:

img[img[2]>img[1] and  img[1]>img[0]]=0

我不确定这是否给出了预期的结果,但这对我来说没有错误

img = cv2.imread(...)

img[ (img[:,:,2] > img[:,:,1]) & (img[:,:,1] > img[:,:,0]) ] = 0

cv2.imshow('image', img)
我不必把它分开

因为
cv2
使用
BGR
而不是
RGB
,所以我不得不按不同的顺序进行比较

我可以使用

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
但是我必须将它转换回
BGR
来显示它


顺便说一句:
cv2
使用numpy数组来保持
img

print( type(img) )

<class 'numpy.ndarray'>
打印(类型(img))

我不确定这是否给出了预期的结果,但这对我来说没有错误

img = cv2.imread(...)

img[ (img[:,:,2] > img[:,:,1]) & (img[:,:,1] > img[:,:,0]) ] = 0

cv2.imshow('image', img)
我不必把它分开

因为
cv2
使用
BGR
而不是
RGB
,所以我不得不按不同的顺序进行比较

我可以使用

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
但是我必须将它转换回
BGR
来显示它


顺便说一句:
cv2
使用numpy数组来保持
img

print( type(img) )

<class 'numpy.ndarray'>
打印(类型(img))
您可以执行以下操作:

import numpy as np

# Generate random image
np.random.seed(42)
r = np.random.randint(0,256,(8,5,3), dtype=np.uint8) 

# Make space for results
res = np.zeros((8,5),dtype=np.uint8)

# Calculate mask
res[(r[...,0]>r[...,1]) & (r[...,1]>r[...,2])] = 1  
输入数组
r

array([[[102, 220, 225],
        [ 95, 179,  61],
        [234, 203,  92],   <--- matches
        [  3,  98, 243],
        [ 14, 149, 245]],

       [[ 46, 106, 244],
        [ 99, 187,  71],
        [212, 153, 199],
        [188, 174,  65],   <--- matches
        [153,  20,  44]],

       [[203, 152, 102],   <--- matches
        [214, 240,  39],
        [121,  24,  34],
        [114, 210,  65],
        [239,  39, 214]],

       [[244, 151,  25],
        [ 74, 145, 222],
        [ 14, 202,  85],
        [145, 117,  87],
        [184, 189, 221]],

       [[116, 237, 109],
        [ 85,  99, 172],
        [226, 153, 103],
        [235, 146,  36],
        [151,  62,  68]],

       [[181, 130, 160],
        [160, 166, 149],
        [  6,  69,   5],
        [ 52, 253, 112],
        [ 14,   1,   3]],

       [[ 76, 248,  87],
        [233, 212, 184],
        [235, 245,  26],
        [213, 157, 253],
        [ 68, 240,  37]],

       [[219,  91,  54],
        [129,   9,  51],
        [  0, 191,  20],
        [140,  46, 187],
        [147,   1, 254]]], dtype=uint8)
您可以这样做:

import numpy as np

# Generate random image
np.random.seed(42)
r = np.random.randint(0,256,(8,5,3), dtype=np.uint8) 

# Make space for results
res = np.zeros((8,5),dtype=np.uint8)

# Calculate mask
res[(r[...,0]>r[...,1]) & (r[...,1]>r[...,2])] = 1  
输入数组
r

array([[[102, 220, 225],
        [ 95, 179,  61],
        [234, 203,  92],   <--- matches
        [  3,  98, 243],
        [ 14, 149, 245]],

       [[ 46, 106, 244],
        [ 99, 187,  71],
        [212, 153, 199],
        [188, 174,  65],   <--- matches
        [153,  20,  44]],

       [[203, 152, 102],   <--- matches
        [214, 240,  39],
        [121,  24,  34],
        [114, 210,  65],
        [239,  39, 214]],

       [[244, 151,  25],
        [ 74, 145, 222],
        [ 14, 202,  85],
        [145, 117,  87],
        [184, 189, 221]],

       [[116, 237, 109],
        [ 85,  99, 172],
        [226, 153, 103],
        [235, 146,  36],
        [151,  62,  68]],

       [[181, 130, 160],
        [160, 166, 149],
        [  6,  69,   5],
        [ 52, 253, 112],
        [ 14,   1,   3]],

       [[ 76, 248,  87],
        [233, 212, 184],
        [235, 245,  26],
        [213, 157, 253],
        [ 68, 240,  37]],

       [[219,  91,  54],
        [129,   9,  51],
        [  0, 191,  20],
        [140,  46, 187],
        [147,   1, 254]]], dtype=uint8)

为什么不呢?我找不到如何在不使用for循环的情况下比较数组中每个元组的元素。图片的形状是:img=[(蓝色,绿色,红色),(蓝色,绿色,红色),…],在numpy文档中找不到如何做。更清楚的是,我想这样做:img[img[2]>img[1]和img[1]>img[0]]=0为什么不呢?我找不到如何在不使用for循环的情况下比较数组中每个元组中的元素。图片的形状为:img=[(蓝色,绿色,红色),(蓝色,绿色,红色),…],在numpy文档中找不到如何进行比较。更清楚的是,我想这样做:img[img[2]>img[1]和img[1]>img[0]=0Ooops,我看我的和你的差不多。我将保留它,因为它生成测试数据并保存结果,而不覆盖原始数据,所以略有不同。向您致敬:-)@MarkSetchell代码中的示例数据是轻松快速测试代码的好主意。我喜欢在你的代码中使用我以前从未见过的
。哎呀,我看我的代码和你的差不多。我将保留它,因为它生成测试数据并保存结果,而不覆盖原始数据,所以略有不同。向您致敬:-)@MarkSetchell代码中的示例数据是轻松快速测试代码的好主意。我喜欢在你的代码中使用
,这是我以前从未见过的。