调整尺寸(缩小)YUV420sp图像 [英] Resize (downsize) YUV420sp image

查看:846
本文介绍了调整尺寸(缩小)YUV420sp图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整YUV420sp格式的图像的尺寸(缩小).是否可以在不将其转换为RGB的情况下进行此类图像调整大小,从而直接操纵YUV420sp像素阵列?在哪里可以找到这样的算法?

I am trying to resize (scale down) an image which comes in YUV420sp format. Is it possible to do such image resizing without converting it into RGB, so directly manipulating the YUV420sp pixel array? Where can I find such algorithm?

谢谢

推荐答案

YUV 4:2:0平面看起来像这样:

YUV 4:2:0 planar looks like this:

----------------------
|     Y      | Cb|Cr |
----------------------

其中:

Y = width x height pixels
Cb = Y / 4 pixels
Cr = Y / 4 pixels

Total num pixels (bytes) = width * height * 3 / 2

子采样的用法如下:

这意味着每个色度像素值在4个亮度像素之间共享.

Which means that each chroma-pixel-value is shared between 4 luma-pixels.

一种方法是删除像素,确保保留/重新计算相应的Y-Cb-Cr关系.

One approach is just to remove pixels, making sure that corresponding Y-Cb-Cr relationship are kept/recalculated.

最近邻插值相似,但相反.

另一种方法是先将4:2:0子采样转换为4:4:4

Another approach is to first convert the 4:2:0 subsampling to 4:4:4

在这里,亮度和色度数据之间存在一对一的映射.

Here you have a 1 to 1 mapping between luma and chroma data.

这是在4:2:0和4:2:2之间内插色度的正确方法(亮度已经处于正确的分辨率) python中的代码,请按照html-link的c-dito进行操作. 代码不是很pythonic,只是c版本的直接翻译.

This is the correct way to interpolate chroma between 4:2:0 and 4:2:2 (luma is already at correct resolution) Code in python, follow html-link for c-dito. Code is not very pythonic, just a direct translation of the c-version.

def __conv420to422(self, src, dst):
    """
    420 to 422 - vertical 1:2 interpolation filter

    Bit-exact with
    http://www.mpeg.org/MPEG/video/mssg-free-mpeg-software.html
    """
    w = self.width >> 1
    h = self.height >> 1

    for i in xrange(w):
        for j in xrange(h):
            j2 = j << 1
            jm3 = 0 if (j<3) else j-3
            jm2 = 0 if (j<2) else j-2
            jm1 = 0 if (j<1) else j-1
            jp1 = j+1 if (j<h-1) else h-1
            jp2 = j+2 if (j<h-2) else h-1
            jp3 = j+3 if (j<h-3) else h-1

            pel = (3*src[i+w*jm3]
                 -16*src[i+w*jm2]
                 +67*src[i+w*jm1]
                +227*src[i+w*j]
                 -32*src[i+w*jp1]
                  +7*src[i+w*jp2]+128)>>8

            dst[i+w*j2] = pel if pel > 0 else 0
            dst[i+w*j2] = pel if pel < 255 else 255

            pel = (3*src[i+w*jp3]
                 -16*src[i+w*jp2]
                 +67*src[i+w*jp1]
                +227*src[i+w*j]
                 -32*src[i+w*jm1]
                 +7*src[i+w*jm2]+128)>>8

            dst[i+w*(j2+1)] = pel if pel > 0 else 0
            dst[i+w*(j2+1)] = pel if pel < 255 else 255
    return dst

运行两次以得到4:4:4. 然后,只需删除行和列即可.

Run this twice to get 4:4:4. Then it's just a matter of removing rows and columns.

或者您可以将色度像素从4:2:0增加到4:4:4,删除行和列,然后将4个Cb/Cr值平均为1以返回到4:2:0同样,这完全取决于您需要严格的程度:-)

Or you can just quadruple the chroma-pixels to go from 4:2:0 to 4:4:4, remove rows and columns and then average 4 Cb/Cr values into 1 to get back to 4:2:0 again, it all depends on how strict you need to be :-)

这篇关于调整尺寸(缩小)YUV420sp图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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