将YUV导入为字节数组 [英] Import YUV as a byte array

查看:430
本文介绍了将YUV导入为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我必须对YUV420_SP_NV21图像(从Android摄像头拍摄)应用阈值,以确定哪些像素是黑色",哪些像素是白色".

I'm working on a project where I have to apply threshold to a YUV420_SP_NV21 image (taken from an Android camera) to determine which pixels are 'black' and which are 'white'.

因此,我想将其作为字节数组(使用OpenCV,NumPy,PIL等)导入Python中,以便可以对Y值进行一些快速的按位运算.

Therefore, I want to import it in Python as a bytearray (using OpenCV, NumPy, PIL, ...), so I can do some quick bitwise operations on the Y-values.



但是,当我尝试使用以下方法导入图像时,会得到无用的输出:



However, when I try to import the image using the following methods, I get useless outputs:

当我键入

import cv2
import numpy as np

img = cv2.imread('test.yuv')
imgArr = np.array(img)

print(img)
print(imgArr)

我得到输出:

None
None



当我键入



And when I type

import numpy as np

f = open('test.yuv', 'rb')
img = f.read()
imgArr = np.array(img)

我得到了一些无用的字符序列.
当我现在输入时(例如)

I get some useless character sequence.
And when I type now (for exemple)

print(imgArr[0])

我得到输出:

IndexError: too many indices for array

这意味着imgArr根本没有数组!

Which means imgArr is no array at all!



谁能告诉我我做错了吗? 预先感谢!



Can anyone please tell me what I'm doing wrong? Thanks in advance!

推荐答案

您确实应该提供示例图像,以便人们可以更好地帮助您-否则,他们花了很长时间开发代码,以猜测是您想要的图像格式,并且如果他们猜错了,那是浪费精力.

You really should provide a sample image so folks can help you better - else they spend ages developing code for what they guess is the image format you mean and it is a waste of effort if they guess wrong.

假设您的数据来自 Wikipedia ,则您可以看到所有Y样本首先是所有U样本和所有V样本.但是,U/V样本只有Y样本的1/4倍,因为这两个分量在水平和垂直方向上都以2:1的比例进行了二次采样:

Assuming your data look like this from Wikipedia, you can see that all the Y samples come first, followed by all the U samples and all the V samples. However there are only 1/4 as many U/V samples as Y samples because these two components are subsampled at 2:1 ratio both horizontally and vertically:

因此,我们的想法是读取整个YUV样本集,然后将第一个w*h字节作为Y值,接下来的w*h/4个样本作为U,将接下来的w*h/4个样本作为V:

So, the idea is to read in the entire set of YUV samples, then take the first w*h bytes as the Y values, the next w*h/4 samples as U and the next w*h/4 samples as V:

import numpy as np

# Guess a width and height and derive number of pixels in image
w,h = 2048,1536
px = w*h

# Read entire file into YUV
YUV = np.fromfile('NV21_2048x1536.yuv',dtype='uint8')

# Take first h x w samples and reshape as Y channel
Y = YUV[0:w*h].reshape(h,w)

# Take next px/4 samples as U
U = YUV[px:(px*5)//4].reshape(h//2,w//2)

# Take next px/4 samples as V
V = YUV[(px*5)//4:(px*6)//4].reshape(h//2,w//2)

# Undo subsampling of U and V by doubling height and width
Ufull = U.copy().resize((w,h))
Vfull = V.copy().resize((w,h))

我不知道你下一步打算做什么,所以我现在就把它留在那儿!

I have no idea what you plan to do next, so I'll leave it at that for now!

关键字:NV21,YUV,YUV420,Android,YUV430P,枕头,PIL,Python,图像,图像处理.

Keywords: NV21, YUV, YUV420, Android, YUV430P, Pillow, PIL, Python, image, image processing.

这篇关于将YUV导入为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆