简体   繁体   English

如何在python中获取图像的特定RGB值的像素索引?

[英]How to get the pixel indices of a specific RGB value of a image in python?

I have imported a color image using openCV imread function. 我使用openCV imread函数导入了一个彩色图像。

im = cv2.imread('test.jpg')

I am looking to find the indices of white pixels, which have a pixel value of [255, 255, 255]. 我希望找到白色像素的索引,其像素值为[255,255,255]。 I know im is actually a 3D array. 我知道我实际上是一个3D阵列。 But what is weird is the value of im[0,0] is [255 255 255], rather than [255, 255, 255]. 但奇怪的是im [0,0]的值是[255 255 255],而不是[255,255,255]。 And im[0,0,0] is 255. So [255 255 255] seems like a list or something, but actually not equivalent to [255, 255, 255]. 并且im [0,0,0]是255.所以[255 255 255]看起来像列表或其他东西,但实际上不等同于[255,255,255]。

>>> print im[0,0]
[255 255 255]

>>> print im[0,0,0]
255

So my questions are: 所以我的问题是:

  1. What is the difference between [255 255 255] and [255, 255, 255]? [255 255 255]和[255,255,255]之间有什么区别?
  2. How can I get the indices of all withe pixels? 如何获得所有像素的索引? What search algorithm should I use? 我应该使用什么搜索算法?

If you want to get the indices of all white pixels, you can convert your image into a 2D array and then find the indices 如果要获取所有白色像素的索引,可以将图像转换为2D数组,然后查找索引

import operator

image = cv2.imread("test.jpg")
img = image.astype('float')
img = img[:,:,0] # convert to 2D array
row, col = img.shape
for i in range(row):
    for j in range(col):
        if img[i,j] == 255:
            x.append(i) # get x indices
            y.append(j) # get y indices

Note that image[0,0] shows 2D matrix whereas image[0,0,0] shows a 3D matrix with 0-color band. 注意,图像[0,0]显示2D矩阵,而图像[0,0,0]显示具有0色带的3D矩阵。 This element basically defines the color band of the image. 该元素基本上定义了图像的色带。 If you do not specify third element, it will show you three values for each color band of the image. 如果未指定第三个元素,它将为图像的每个色带显示三个值。 But when you mention the color band, it will show you the exact pixel value. 但是当你提到色带时,它会显示确切的像素值。

NOTE: There is no need to convert the image into a 2D array. 注意:无需将图像转换为2D阵列。 you can always do it for the actual array too. 你总是可以为实际的数组做这件事。 I did so to simply explain the algorithm to you. 我这样做只是为了向你解释算法。

You are right neuxx. 你是对的neuxx。 the value [255 255 255] is not equal to [255, 255, 255]. 值[255 255 255]不等于[255,255,255]。 so you need to convert pixel value ( [255 255 255] ) into array value( [255, 255, 255] ) to compare. 所以你需要将像素值([255 255 255])转换为数组值([255,255,255])进行比较。 so you need to use '[:]' to convert it to array. 所以你需要使用'[:]'将它转换为数组。 Moreover, if you want to find all white pixels you can use this code. 此外,如果要查找所有白色像素,可以使用此代码。 While pixel indices will be stored in the list "White_pix_ind" as a tuple. 而像素索引将作为元组存储在列表“White_pix_ind”中。

White_pix_ind = []
row,col,depth = Img.shape
for i in range(row):
    for j in range(col):
        if( (Img[(i,j)][:]==White_pix_ind ).all() ):
            print(i,j)
            White_pix_ind.append((i,j))
            break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM