[英]How to extract pixels of specified coordinates from image
I am extracting some pixels from an image using the following:我正在使用以下方法从图像中提取一些像素:
import cv2
import numpy as np
img = cv2.imread('random.png')
targets = np.array([[111, 111], [222, 222], [333, 333]], dtype=np.uint8)
targeted = []
for x, y in targets:
targeted.append(img[x, y])
Is there a more pythonic or vectorized method to do it in one line instead of this for
loop?是否有一种更 Pythonic 或矢量化的方法可以在一行中完成,而不是这个
for
循环?
Perhaps this is what you are looking for (in the place of the for
loop):也许这就是您正在寻找的(代替
for
循环):
targeted = img[tuple(targets.T)].tolist()
If you don't want to convert into a list:如果您不想转换为列表:
targeted = img[tuple(targets.T)]
I got the output after renaming r to targets.在将 r 重命名为目标后,我得到了 output。 It could be because your image object is None.
这可能是因为您的图像 object 为无。 Can you check:
你能检查一下吗:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.