I'm trying to convert the 3d array returned by cv2.imread to the continguous rgb byte array, in order to display in GTK. And below is my conversion code:
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
So, in my code, I first get the three dimensions separately into r_arr, g_arr, b_arr, then I put the values in the order or RGB into the 'arr'. So after the iteration the array 'arr' will be like 'r0, g0, b0, r1, g1, b1, ...'
And then I use "GdkPixbuf.Pixbuf.new_from_data" function to get the pixbuf from the arr returned by the "ndarr2rgb" function above. And I use "image.set_from_pixbuf" to display the image. But I got the following result:
It's like that there is some noisy area, so please help me solve my problem, thx.