Получить RGB каналы изображения - PullRequest
0 голосов
/ 14 марта 2019

Я пытаюсь разбить трехмерный массив NumPy на его красный, зеленый и синий слои.

Пока у меня есть эта функция: s_W - ширина экрана, а s_H - высота экрана

def webcam_func(cap, s_W, s_H):
    # Capture frame by frame
    ret, frame = cap.read()

    if ret:
        # make a np array from the frame
        frame_one = np.array(frame)

        # seperate the height, width and depth of the array
        arr_height = (frame_one.shape[0])
        arr_width = (frame_one.shape[1])
        arr_rgb = (frame_one.shape[2])

        # This is what I tried to get a copy of the whole 
        # array except for the first depth slice, 
        # which I believe is RED channel
        green_frame = frame_one[0:arr_height, 0:arr_width, 0:1]

        # flip the frame
        frame_flip = np.rot90(green_frame)

        # create a pygame surface and then scale the surface
        webcam_frame = pyg.surfarray.make_surface(frame_flip)
        webcam_frame = pyg.transform.scale(webcam_frame, (s_W, s_H))

        return(webcam_frame)

Тем не менее, я получаю эту ошибку при попытке создать поверхность из нарезанного кадра.

ValueError: must be a valid 2d or 3d array

Есть идеи?

1 Ответ

2 голосов
/ 14 марта 2019

Если вам нужны каналы rgb изображения, представленного массивом numpy, вы можете использовать:

b,g,r = cv2.split(frame)

или:

b = frame[:,:,0]
g = frame[:,:,1]
r = frame[:,:,2]

Таким образом, вы можете изменить свою функцию:

def webcam_func(cap, s_W, s_H):
    # Capture frame by frame
    ret, frame_one = cap.read()

    if ret:


    # seperate the height, width and depth of the array
        arr_height = (frame_one.shape[0])
        arr_width = (frame_one.shape[1])
        arr_rgb = (frame_one.shape[2])

        green_frame = frame_one[:,:, 1] #This will return the green channel

    # flip the frame
        frame_flip = np.rot90(green_frame)

    # create a pygame surface and then scale the surface
        webcam_frame = pyg.surfarray.make_surface(frame_flip)
        webcam_frame = pyg.transform.scale(webcam_frame, (s_W, s_H))

        return(webcam_frame)
...