简体   繁体   English

使用 python 查找图像中黑色/灰色像素的所有坐标

[英]Find all coordinates of black / grey pixels in image using python

I'm trying to find a way to read any any .png , .jpg or .tiff , and return the coordinates of all black or grey pixels in that image.我试图找到一种方法来读取任何.png.jpg.tiff ,并返回该图像中所有黑色或灰色像素的坐标。

I'm thinking of having a certain threshold grey color, and writing out the coordinates of every pixel that is darker than that.我正在考虑使用某个阈值灰色,并写出比该颜色更暗的每个像素的坐标。 I'm not sure how to manage the aspect of reading the image, however.但是,我不确定如何管理读取图像的方面。 I'm aiming to have my result be lists of all black pixels in the image, as such:我的目标是让我的结果是图像中所有黑色像素的列表,例如:

[x-coord, y-coord, black] [x 坐标,y 坐标,黑色]

I've looked into using cv.imread to read out the coordinates of pixels, but as far as I've been able to tell, it works exactly opposite to the way I want it - it takes coordinates as a parameter, and returns the RGB values.我已经研究过使用cv.imread来读出像素的坐标,但据我所知,它的工作方式与我想要的完全相反——它将坐标作为参数,并返回RGB 值。 Does anyone have tips / methods of making this work?有没有人有使这项工作的提示/方法?

For anyone with similar questions, I solved this using the answer below, then I turned the numpy-array into a list using np.ndarray.tolist() .对于任何有类似问题的人,我使用下面的答案解决了这个问题,然后我使用np.ndarray.tolist()将 numpy-array 变成了一个列表。 Additionally, since I only got a truncated version of the results, i used:此外,由于我只得到了结果的截断版本,因此我使用了:

import sys

np.set_printoptions(threshold=sys.maxsize)

Now it was simple to print any element from the list using indices.现在使用索引打印列表中的任何元素都很简单。

You can use np.column_stack() + np.where() .您可以使用np.column_stack() + np.where() The idea is to convert the image to grayscale then find the coordinates of all pixels below a certain threshold.这个想法是将图像转换为灰度,然后找到低于某个阈值的所有像素的坐标。 Note in grayscale, the image has one channel with pixel values [0... 255]注意在灰度中,图像有一个像素值为[0... 255]的通道


Using this input image with a threshold_level = 20将此输入图像与threshold_level = 20一起使用

We color in all pixels below this threshold level in blue我们将低于此阈值水平的所有像素着色为蓝色

All pixel coordinates can be determined from the mask using np.where() and stacked into (x, y) format with np.column_stack() .所有像素坐标都可以使用 np.where() 从掩码中确定,并使用np.where() np.column_stack()(x, y)格式。 Here are all coordinates of pixels lower than the threshold这是低于阈值的所有像素坐标

coords = np.column_stack(np.where(gray < threshold_level))
[[ 88 378]
 [ 89 378]
 [ 90 378]
 ...
 [474 479]
 [474 480]
 [474 481]]

With threshold_level = 50 :使用threshold_level = 50

[[ 21 375]
 [ 22 375]
 [ 23 376]
 ...
 [474 681]
 [474 682]
 [474 683]]

Code代码

import cv2
import numpy as np

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Set threshold level
threshold_level = 50

# Find coordinates of all pixels below threshold
coords = np.column_stack(np.where(gray < threshold_level))

print(coords)

# Create mask of all pixels lower than threshold level
mask = gray < threshold_level

# Color the pixels in the mask
image[mask] = (204, 119, 0)

cv2.imshow('image', image)
cv2.waitKey()

With your input image and threshold_level = 10使用您的输入图像和threshold_level = 10

[[  59  857]
 [  59  858]
 [  59  859]
 ...
 [1557  859]
 [1557  860]
 [1557  861]]

Version that use PIL library使用 PIL 库的版本

import numpy as np
from PIL import Image 

image = Image.open("1.png").convert('L')
pixels = np.asarray(image)

# Set threshold level
threshold_level = 50

# Find coordinates of all pixels below threshold
coords = np.column_stack(np.where(pixels < threshold_level))

based on @nathancy answer, thank you for the code!基于@nathancy 的回答,感谢您的代码!

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

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