简体   繁体   English

如何从图像中提取指定坐标的像素

[英]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:你能检查一下吗:

  1. Whether path/image filename is correct路径/图像文件名是否正确
  2. Whether the image has a size of more than 111 (which is the minimum location)(图片的大小是否大于111(即最小位置)(

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

相关问题 从标记图像中提取 yxz 像素坐标 - Extract yxz pixels coordinates from a labelled image 如何从图像中提取全白像素? - How do I extract fully white pixels from an image? 从图像中提取坐标到numpy - Extract coordinates from image to numpy 如何从图像上具有不同值的不同像素的坐标数据帧创建热图 - How to create a heatmap from a data frame of coordinates from different pixels with different values over an image 如何从图像中提取(x,y)坐标并写入 CSV 文件? - How to extract (x, y) coordinates from image and write to CSV file? 将像素转换为LatLng来自谷歌静态图像的坐标 - Converting Pixels to LatLng Coordinates from google static image 从图像(地图)中提取多边形坐标 - Extract polygon coordinates from image (map) 如何从二维 numpy 阵列中提取图像像素? - How can I extract the image pixels from 2D numpy array? 如何从视频中提取一行像素(roi),合并成单个.tif图像中的行? - How to extract a line of pixels (roi) from video, combined lines in a single .tif image? 如何有效地从 C++ 中的 cv::Mat 图像中提取某些像素? - How to efficiently extract certain pixels from cv::Mat image in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM