我将包含RGB图像数据的C数组传递给Python中的函数以进一步处理图像。
如何在Python中检索此图像并将其绘制出来?

包含RGB图像数据的名为c_data的C数组由创建

for(k = 0; k < c; ++k){
    for(j = 0; j < h; ++j){
        for(i = 0; i < w; ++i){
            int dst_index = i + w*j + w*h*k;
            int src_index = k + c*i + c*w*j;
            c_data[dst_index] = (float)stb_im[src_index]/255.;
        }
    }
}


C数组将转换为numpy数组,并通过名为im_data的参数通过以下标头传递给Python函数

def read_img_from_c(im_data, im_h, im_w):

print(im_h) // 480
print(im_w) // 640
print(im_data.shape) // (921600,) --> (480*640*3)


我试图简单地使用以下方法重塑numpy数组

data = im_data.reshape((im_h, im_w, 3))


并使用创建一个PIL图像对象

img = PIL.Image.fromarray(data, 'RGB')


,但是当我运行以下命令时

img.show()


我得到了以下内容,而不是原始图像。

python - 从包含RGB图像数据的1维数组中检索RGB图像-LMLPHP

更新:我按照建议通过将那些归一化的像素值乘以255.0,将numpy数组强制转换为int并绘图:

im_data = (im_data*255.0).astype(np.uint8)
im_data = im_data.reshape((im_h, im_w, 3))
img = Image.fromarray(im_data, 'RGB')
img.show()


我得到了具有重复图案的图像,而不是单个大RGB
图片:

python - 从包含RGB图像数据的1维数组中检索RGB图像-LMLPHP

最佳答案

尝试再次将data乘以255并将其四舍五入为整数。我猜RGB元组中的值应该在0-255范围内,而不是0-1

关于python - 从包含RGB图像数据的1维数组中检索RGB图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60317742/

10-14 18:00
查看更多