简体   繁体   English

如何获取图像中像素的x,y(坐标)数组

[英]how to get x,y(coordinate) array of pixels in image

I need all coordinates of certain colored pixels in image by opencv - python我需要opencv-python图像中某些彩色像素的所有坐标

I guess return value of cv2.inRange may contains coordinates of pixels, but can`t understands how it is constructed我猜cv2.inRange 的返回值可能包含像素的坐标,但无法理解它是如何构造的

Is there any way to get this?有没有办法得到这个?

import cv2
import numpy as np


img = cv2.imread("sample.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Create an array with HSV between 110,50,50 and 130,255,255
array = cv2.inRange(hsv, np.array([110,50,50]), np.array([130,255,255]))

np.nonzero(array)
# (array([ 1,  1,  1,  2, 12, 13, 15, 15, 15, 18, 19]),
#  array([13, 14, 18, 22, 21,  1, 20, 22, 23,  1, 30]))

That means that pixels (1, 13), (1, 14), (1, 18), (2, 22), etc. are between our given values.这意味着像素 (1, 13)、(1, 14)、(1, 18)、(2, 22) 等位于我们给定的值之间。 If you want them already paired:如果您希望它们已经配对:

np.transpose(np.nonzero(array))
# array([[ 1, 13],
#        [ 1, 14],
#        ...
#        [18,  1],
#        [19, 30]])

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

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