Cómo convertir correctamente la matriz 3d en bytes rgb distinguidos
Estoy tratando deconvierte la matriz 3d devuelta por cv2.imread a la matriz de bytes rgb distintiva, para mostrar en GTK. Y a continuación está mi código de conversión:
def ndarr2rgb(img):
r_arr = img[:, :, 0].ravel() # read channel
g_arr = img[:, :, 1].ravel() # green channel
b_arr = img[:, :, 2].ravel() # blue channel
numItems = img.shape[0] * img.shape[1] * img.shape[2] # number of entries in byte array
z = img.shape[2] # z dimension, always equal to 3
arr = np.zeros((numItems, 1))
# to fill the byte array with r,g,b channel
for i in xrange(0, numItems):
if i % z == 0:
arr[i] = r_arr[i/z]
if i % z == 1:
arr[i] = g_arr[i/z]
if i % z == 2:
arr[i] = b_arr[i/z]
return arr
Entonces, en mi código, primero obtengo las tres dimensiones por separado en r_arr, g_arr, b_arr, luego pongo los valores en el orden o RGB en el 'arr'. Entonces, después de la iteración, la matriz 'arr' será como 'r0, g0, b0, r1, g1, b1, ...'
Y luego uso "GdkPixbuf.Pixbuf.new_from_data"función para obtener el pixbuf del arr devuelto por el"ndarr2rgb"funciona arriba. Y yo uso"image.set_from_pixbuf"para mostrar la imagen. Pero obtuve el siguiente resultado:
Es como si hubiera un área ruidosa, así que por favor ayúdame a resolver mi problema, gracias.