How to correctly covert 3d array into continguous

2019-02-27 10:12发布

问题:

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.

回答1:

just do:

all_in_one_row = np.reshape(img,(-1))


回答2:

The problem is possibly due to GdkPixbuf.Pixbuf.new_from_data not managing memory correctly, see [1]. A workaround is to write the data to a temporary file and use GdkPixbuf.Pixbuf.new_from_file [2]. Example using PIL:

from PIL import Image

image = Image.frombytes('RGB', (width, height), data)
image.save(filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename)
  1. https://bugzilla.gnome.org/show_bug.cgi?id=721497
  2. http://lazka.github.io/pgi-docs/api/GdkPixbuf-2.0/classes/Pixbuf.html#GdkPixbuf.Pixbuf.new_from_file


回答3:

Due to changes in io, PIL and/or GdkPixbuf I had trouble figuring this out, even after reading numerous suggestions. Finally I've took quite a bare bone solution:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf

def array2pixbuf(width, height, arr):
    # pnm
    header = b"P6 %d %d 255 " % (width, height)
    # flatten and convert to an array of bytes (not a bytearray!)
    data = bytes(colour for pixel in arr for colour in pixel)

    loader = GdkPixbuf.PixbufLoader.new()
    loader.write(header)
    loader.write(data)
    pixbuf = loader.get_pixbuf()
    loader.close()

    return pixbuf


回答4:

You don't need to write a temp file in order to workaroud GdkPixbuf.Pixbuf.new_from_data not managing memory correctly, [1] you can use the pnm image format [2]:

from gi.repository import GdkPixbuf
import cv2

filename = './file.png'
# read and convert image from BGR to RGB (pnm uses RGB)
im = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
# get image dimensions (depth is not used)
height, width, depth = im.shape
pixl = GdkPixbuf.PixbufLoader.new_with_type('pnm')
# P6 is the magic number of PNM format, 
# and 255 is the max color allowed, see [2]
pixl.write("P6 %d %d 255 " % (width, height) + im.tostring())
pix = pixl.get_pixbuf()
pixl.close()

After that hack, you can use: image.set_from_pixbuf( pix )

References:

  1. https://bugzilla.gnome.org/show_bug.cgi?id=732297
  2. http://en.wikipedia.org/wiki/Netpbm_format