我需要opencv-python
中图像中某些彩色像素的所有坐标我猜 cv2.inRange 的返回值可能包含像素的坐标,但不能理解它的构造方式
有没有办法解决这个问题?
答案 0 :(得分:0)
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]))
这意味着像素(1,13),(1,14),(1,18),(2,22)等在我们给定的值之间。如果你想要它们已配对:
np.transpose(np.nonzero(array))
# array([[ 1, 13],
# [ 1, 14],
# ...
# [18, 1],
# [19, 30]])