Converting Numpy Array to OpenCV Array

后端 未结 3 1540
半阙折子戏
半阙折子戏 2020-12-08 04:01

I\'m trying to convert a 2D Numpy array, representing a black-and-white image, into a 3-channel OpenCV array (i.e. an RGB image).

Based on code samples and the docs

3条回答
  •  既然无缘
    2020-12-08 05:03

    This is what worked for me...

    import cv2
    import numpy as np
    
    #Created an image (really an ndarray) with three channels 
    new_image = np.ndarray((3, num_rows, num_cols), dtype=int)
    
    #Did manipulations for my project where my array values went way over 255
    #Eventually returned numbers to between 0 and 255
    
    #Converted the datatype to np.uint8
    new_image = new_image.astype(np.uint8)
    
    #Separated the channels in my new image
    new_image_red, new_image_green, new_image_blue = new_image
    
    #Stacked the channels
    new_rgb = np.dstack([new_image_red, new_image_green, new_image_blue])
    
    #Displayed the image
    cv2.imshow("WindowNameHere", new_rgbrgb)
    cv2.waitKey(0)
    

提交回复
热议问题