VI 在图像上绘制了一组轮廓点,并将其存储为 2D numpy 数组。轮廓由 2 个 numpy 数组表示,每个数组的 x 和 y 坐标为浮点值。这些坐标不是整数,并且不能与像素完美对齐,但它们确实可以告诉您轮廓点相对于像素的位置。
我希望能够选择轮廓内的像素。我编写了一些与此处给出的答案几乎相同的代码:使用 Python 中的 OpenCV 在轮廓边界内访问像素值
temp_list = []
for a, b in zip(x_pixel_nos, y_pixel_nos):
temp_list.append([[a, b]]) # 2D array of shape 1x2
temp_array = np.array(temp_list)
contour_array_list = []
contour_array_list.append(temp_array)
lst_intensities = []
# For each list of contour points...
for i in range(len(contour_array_list)):
# Create a mask image that contains the contour filled in
cimg = np.zeros_like(pixel_array)
cv2.drawContours(cimg, contour_array_list, i, color=255, thickness=-1)
# Access …Run Code Online (Sandbox Code Playgroud)