[Python] Removing rows from numpy 3D array based on last element

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    What I'm trying to do is essentially removing all rows h,s in a 3D numpy array a if a[h,s,v] = some value for all v

    More specifically, I have a loaded image from cv2 which contains some transparent pixels. I'd like to create an HSV histogram without including the transparent pixels (i.e. k=255)

    Here's what I have now:

    import cv2
    import numpy as np

    IMAGE_FILE = './images/2024-11-17/00.png' # load image with some transparent pixels

    # Read image into HSV
    image = cv2.imread(IMAGE_FILE)
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # Remove all pixels with V = 255
    hsv_removed_transparency = []
    i = np.where(hsv[:, :, 2] == 255) # indices of pixels with V = 255
    for i1 in range(len(i[0])):
    hsv_removed_transparency.append(np.delete(hsv[i[0][i1]], i[1][i1], axis=0)

    Continue reading...

Compartilhe esta Página