【问题标题】:How to convert RGB -> YUV -> RGB (both ways)如何转换 RGB -> YUV -> RGB(双向)
【发布时间】:2013-07-27 08:56:12
【问题描述】:

我想要一对转换算法,一个从 RGB 到 YUV,另一个从 YUV 到 RGB,它们是互逆的。也就是说,往返转换应该保持该值不变。 (如果喜欢,可以将 YUV 替换为 Y'UV、YUV、YCbCr、YPbPr。)

这样的事情存在吗?如果有,是什么?

当省略对 [0,255] 的钳位时,发布的解决方案(How to perform RGB->YUV conversion in C/C++?http://www.fourcc.org/fccyvrgb.phphttp://en.wikipedia.org/wiki/YUV)仅是逆矩阵(两个 3x3 矩阵是逆矩阵)。但是省略该钳位会允许出现负亮度之类的事情,这会对 YUV 空间中的图像处理造成严重破坏。保留钳位会使转换非线性,这使得定义逆运算变得很棘手。

【问题讨论】:

  • 钳位修复了颜色无法在“新”颜色系统中表达的情况。由于 RGB 和 YUV 不表达完全相同的一组颜色,因此您有时必须进行一些“近似”。这个公式应该适用于 YUV 到 RGB。 en.wikipedia.org/wiki/YUV#Numerical_approximations(或者至少在我对 YUV 代码进行一些测试时它对我有用)
  • 您想通过往返转换完成什么?在某些情况下,使用负色或过亮色是合法的。
  • 上下文是调试一个在 YUV 中运行的图像处理算法,但是给定 RGB 并发出 RGB。我想验证算法的包装器:RGB - YUV - 注释掉算法 - YUV - RGB 应该保持输入不变。

标签: c++ c rgb yuv bidirectional


【解决方案1】:

是的,存在可逆变换。

equasys GmbH 发布了从 RGB 到 YUV、YCbCr 和 YPbPr 的可逆转换,并解释了每种情况适用于哪种情况、这种钳制的真正含义以及参考链接。 (就像一个很好的答案。)

对于我自己的应用程序(jpg 图像,而不是模拟电压)YCbCr 是合适的,所以我为这两个转换编写了代码。事实上,对于许多图像,在 256 中反复出现的值相差不到 1 分;前后图像在视觉上无法区分。

PIL's colour space conversion YCbCr -> RGB 因提及 equasys 的网页而受到赞誉。

其他答案,可能会提高 equasys 的精确度和简洁性:

  • https://code.google.com/p/imagestack/ 包括 rgb_to_x 和 x_to_rgb 函数,但我没有尝试编译和测试它们。

  • Cory Nelson 的答案链接到具有类似功能的代码,但它说 一般来说,倒置是不可能的,这与 equasys 相矛盾。

  • FFmpeg、OpenCV、VLFeat 或 ImageMagick 的源代码。

2019 年编辑:这是我的评论中提到的来自 github 的 C 代码。

void YUVfromRGB(double& Y, double& U, double& V, const double R, const double G, const double B)
{
  Y =  0.257 * R + 0.504 * G + 0.098 * B +  16;
  U = -0.148 * R - 0.291 * G + 0.439 * B + 128;
  V =  0.439 * R - 0.368 * G - 0.071 * B + 128;
}
void RGBfromYUV(double& R, double& G, double& B, double Y, double U, double V)
{
  Y -= 16;
  U -= 128;
  V -= 128;
  R = 1.164 * Y             + 1.596 * V;
  G = 1.164 * Y - 0.392 * U - 0.813 * V;
  B = 1.164 * Y + 2.017 * U;
}

【讨论】:

  • github 上的文件顶部有几行实现此功能的 C++。
  • equasys 似乎在 2018 年 5 月撤下了该页面。这里是 web.archive.org 链接,供有兴趣阅读详细说明的人使用:web.archive.org/web/20180423091842/http://www.equasys.de/…
  • 这个公式对 10 位或 16 位 YUV 有效吗?
  • 此 C 代码假定 R、G、B、Y、U、V 的范围为 0 到 255。所以对于 10 位(在任一端,而不仅仅是 YUV),将该端乘以 4.0。对于 16 位,乘以 256.0。浮点公式仍然有效。一次又一次的测试很容易检查你是否做对了。
【解决方案2】:

RGB 到 YUV 再返回

维基百科上有一个关于YUV 主题的漂亮图表,它描述了YUV420p 的布局。但是,如果您像我一样想要 NV21,有时称为 YUV420sp,它将 V 和 U 分量交错在一个平面中,所以在这种情况下,该图是错误的,但它可以让您直观地了解它是如何工作的。

此格式(NV21)是Android相机上的标准图片格式 预习。 YUV 4:2:0 平面图像,带有 8 位 Y 样本,然后是 具有 8 位 2x2 子采样色度样本的交错 V/U 平面。

所以我看到的很多代码只是开始按照这个规范进行编码,而没有考虑到Endianess。此外,它们往往只支持 YUV 到 RGB 并且只支持一种或两种格式。然而,我想要一些更值得信赖的东西,结果证明取自 Android 源代码存储库的 C++ 代码可以解决问题。它几乎是纯 C++,应该很容易在任何项目中使用。

获取 RGB565 图像并将其转换为 NV21 的 JNI/C++ 代码

在这种情况下,从 Java 开始,但很容易通过 C 或 C++ 传入包含 RGB565 图像的字节数组并输出 NV21 字节数组。

#include <jni.h>
#include <cstring>
#include <cstdint>

#include "Converters.h"



#define JNI(X) JNIEXPORT Java_algorithm_ImageConverter_##X

#ifdef __cplusplus
extern "C" {
#endif

void JNI(RGB565ToNV21)(JNIEnv *env, jclass *, jbyteArray aRGB565in, jbyteArray aYUVout, jint width, jint height) {
    //get jbyte array into C space from JVN
    jbyte *rgb565Pixels = env->GetByteArrayElements(aRGB565in, NULL);
    jbyte *yuv420sp = env->GetByteArrayElements(aYUVout, NULL);

    size_t pixelCount = width * height;

    uint16_t *rgb = (uint16_t *) rgb565Pixels;

    // This format (NV21) is the standard picture format on Android camera preview. YUV 4:2:0 planar
    // image, with 8 bit Y samples, followed by interleaved V/U plane with 8bit 2x2 subsampled
    // chroma samples.

    int uvIndex = pixelCount;
    for (int row = 0; row < height; row++) {
        for (int column = 0; column < width; column++) {
            int pixelIndex = row * width + column;
            uint8_t y = 0;
            uint8_t u = 0;
            uint8_t v = 0;

            chroma::RGB565ToYUV(rgb[pixelIndex], &y, &u, &v);
            yuv420sp[pixelIndex] = y;
            if (row % 2 == 0 && pixelIndex % 2 == 0) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
                yuv420sp[uvIndex++] = u;
                yuv420sp[uvIndex++] = v;
#else
                yuv420sp[uvIndex++] = v;
                yuv420sp[uvIndex++] = u;
#endif
            }
        }
    }

    //release temp reference of jbyte array
    env->ReleaseByteArrayElements(aYUVout, yuv420sp, 0);
    env->ReleaseByteArrayElements(aRGB565in, rgb565Pixels, 0);
}

#ifdef __cplusplus
}
#endif

转换器.h

正如您将在标题中看到的那样,有许多不同的转换选项可用于/来自任意数量的格式。

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef HW_EMULATOR_CAMERA_CONVERTERS_H
#define HW_EMULATOR_CAMERA_CONVERTERS_H
#include <endian.h>
#ifndef __BYTE_ORDER
#error "could not determine byte order"
#endif
/*
 * Contains declaration of framebuffer conversion routines.
 *
 * NOTE: RGB and big/little endian considerations. Wherever in this code RGB
 * pixels are represented as WORD, or DWORD, the color order inside the
 * WORD / DWORD matches the one that would occur if that WORD / DWORD would have
 * been read from the typecasted framebuffer:
 *
 *      const uint32_t rgb = *reinterpret_cast<const uint32_t*>(framebuffer);
 *
 * So, if this code runs on the little endian CPU, red color in 'rgb' would be
 * masked as 0x000000ff, and blue color would be masked as 0x00ff0000, while if
 * the code runs on a big endian CPU, the red color in 'rgb' would be masked as
 * 0xff000000, and blue color would be masked as 0x0000ff00,
 */
namespace chroma {
/*
 * RGB565 color masks
 */
#if __BYTE_ORDER == __LITTLE_ENDIAN
static const uint16_t kRed5     = 0x001f;
static const uint16_t kGreen6   = 0x07e0;
static const uint16_t kBlue5    = 0xf800;
#else   // __BYTE_ORDER
static const uint16_t kRed5     = 0xf800;
static const uint16_t kGreen6   = 0x07e0;
static const uint16_t kBlue5    = 0x001f;
#endif  // __BYTE_ORDER
static const uint32_t kBlack16  = 0x0000;
static const uint32_t kWhite16  = kRed5 | kGreen6 | kBlue5;
/*
 * RGB32 color masks
 */
#if __BYTE_ORDER == __LITTLE_ENDIAN
static const uint32_t kRed8     = 0x000000ff;
static const uint32_t kGreen8   = 0x0000ff00;
static const uint32_t kBlue8    = 0x00ff0000;
#else   // __BYTE_ORDER
static const uint32_t kRed8     = 0x00ff0000;
static const uint32_t kGreen8   = 0x0000ff00;
static const uint32_t kBlue8    = 0x000000ff;
#endif  // __BYTE_ORDER
static const uint32_t kBlack32  = 0x00000000;
static const uint32_t kWhite32  = kRed8 | kGreen8 | kBlue8;
/*
 * Extracting, and saving color bytes from / to WORD / DWORD RGB.
 */
#if __BYTE_ORDER == __LITTLE_ENDIAN
/* Extract red, green, and blue bytes from RGB565 word. */
#define R16(rgb)    static_cast<uint8_t>((rgb) & kRed5)
#define G16(rgb)    static_cast<uint8_t>(((rgb) & kGreen6) >> 5)
#define B16(rgb)    static_cast<uint8_t>(((rgb) & kBlue5) >> 11)
/* Make 8 bits red, green, and blue, extracted from RGB565 word. */
#define R16_32(rgb) static_cast<uint8_t>((((rgb) & kRed5) << 3) | (((rgb) & kRed5) >> 2))
#define G16_32(rgb) static_cast<uint8_t>((((rgb) & kGreen6) >> 3) | (((rgb) & kGreen6) >> 9))
#define B16_32(rgb) static_cast<uint8_t>((((rgb) & kBlue5) >> 8) | (((rgb) & kBlue5) >> 14))
/* Extract red, green, and blue bytes from RGB32 dword. */
#define R32(rgb)    static_cast<uint8_t>((rgb) & kRed8)
#define G32(rgb)    static_cast<uint8_t>((((rgb) & kGreen8) >> 8) & 0xff)
#define B32(rgb)    static_cast<uint8_t>((((rgb) & kBlue8) >> 16) & 0xff)
/* Build RGB565 word from red, green, and blue bytes. */
#define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(b) << 6) | (g)) << 5) | (r))
/* Build RGB32 dword from red, green, and blue bytes. */
#define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(b) << 8) | (g)) << 8) | (r))
#else   // __BYTE_ORDER
/* Extract red, green, and blue bytes from RGB565 word. */
#define R16(rgb)    static_cast<uint8_t>(((rgb) & kRed5) >> 11)
#define G16(rgb)    static_cast<uint8_t>(((rgb) & kGreen6) >> 5)
#define B16(rgb)    static_cast<uint8_t>((rgb) & kBlue5)
/* Make 8 bits red, green, and blue, extracted from RGB565 word. */
#define R16_32(rgb) static_cast<uint8_t>((((rgb) & kRed5) >> 8) | (((rgb) & kRed5) >> 14))
#define G16_32(rgb) static_cast<uint8_t>((((rgb) & kGreen6) >> 3) | (((rgb) & kGreen6) >> 9))
#define B16_32(rgb) static_cast<uint8_t>((((rgb) & kBlue5) << 3) | (((rgb) & kBlue5) >> 2))
/* Extract red, green, and blue bytes from RGB32 dword. */
#define R32(rgb)    static_cast<uint8_t>(((rgb) & kRed8) >> 16)
#define G32(rgb)    static_cast<uint8_t>(((rgb) & kGreen8) >> 8)
#define B32(rgb)    static_cast<uint8_t>((rgb) & kBlue8)
/* Build RGB565 word from red, green, and blue bytes. */
#define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(r) << 6) | g) << 5) | b)
/* Build RGB32 dword from red, green, and blue bytes. */
#define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(r) << 8) | g) << 8) | b)
#endif  // __BYTE_ORDER
/* An union that simplifies breaking 32 bit RGB into separate R, G, and B colors.
 */
typedef union RGB32_t {
    uint32_t    color;
    struct {
#if __BYTE_ORDER == __LITTLE_ENDIAN
        uint8_t r; uint8_t g; uint8_t b; uint8_t a;
#else   // __BYTE_ORDER
        uint8_t a; uint8_t b; uint8_t g; uint8_t r;
#endif  // __BYTE_ORDER
    };
} RGB32_t;
/* Clips a value to the unsigned 0-255 range, treating negative values as zero.
 */
static __inline__ int
clamp(int x)
{
    if (x > 255) return 255;
    if (x < 0)   return 0;
    return x;
}
/********************************************************************************
 * Basics of RGB -> YUV conversion
 *******************************************************************************/
/*
 * RGB -> YUV conversion macros
 */
#define RGB2Y(r, g, b) (uint8_t)(((66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) +  16)
#define RGB2U(r, g, b) (uint8_t)(((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
#define RGB2V(r, g, b) (uint8_t)(((112 * (r) - 94 * (g) -  18 * (b) + 128) >> 8) + 128)
/* Converts R8 G8 B8 color to YUV. */
static __inline__ void
R8G8B8ToYUV(uint8_t r, uint8_t g, uint8_t b, uint8_t* y, uint8_t* u, uint8_t* v)
{
    *y = RGB2Y((int)r, (int)g, (int)b);
    *u = RGB2U((int)r, (int)g, (int)b);
    *v = RGB2V((int)r, (int)g, (int)b);
}
/* Converts RGB565 color to YUV. */
static __inline__ void
RGB565ToYUV(uint16_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
{
    R8G8B8ToYUV(R16_32(rgb), G16_32(rgb), B16_32(rgb), y, u, v);
}
/* Converts RGB32 color to YUV. */
static __inline__ void
RGB32ToYUV(uint32_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
{
    RGB32_t rgb_c;
    rgb_c.color = rgb;
    R8G8B8ToYUV(rgb_c.r, rgb_c.g, rgb_c.b, y, u, v);
}
/********************************************************************************
 * Basics of YUV -> RGB conversion.
 * Note that due to the fact that guest uses RGB only on preview window, and the
 * RGB format that is used is RGB565, we can limit YUV -> RGB conversions to
 * RGB565 only.
 *******************************************************************************/
/*
 * YUV -> RGB conversion macros
 */
/* "Optimized" macros that take specialy prepared Y, U, and V values:
 *  C = Y - 16
 *  D = U - 128
 *  E = V - 128
 */
#define YUV2RO(C, D, E) clamp((298 * (C) + 409 * (E) + 128) >> 8)
#define YUV2GO(C, D, E) clamp((298 * (C) - 100 * (D) - 208 * (E) + 128) >> 8)
#define YUV2BO(C, D, E) clamp((298 * (C) + 516 * (D) + 128) >> 8)
/*
 *  Main macros that take the original Y, U, and V values
 */
#define YUV2R(y, u, v) clamp((298 * ((y)-16) + 409 * ((v)-128) + 128) >> 8)
#define YUV2G(y, u, v) clamp((298 * ((y)-16) - 100 * ((u)-128) - 208 * ((v)-128) + 128) >> 8)
#define YUV2B(y, u, v) clamp((298 * ((y)-16) + 516 * ((u)-128) + 128) >> 8)
/* Converts YUV color to RGB565. */
static __inline__ uint16_t
YUVToRGB565(int y, int u, int v)
{
    /* Calculate C, D, and E values for the optimized macro. */
    y -= 16; u -= 128; v -= 128;
    const uint16_t r = (YUV2RO(y,u,v) >> 3) & 0x1f;
    const uint16_t g = (YUV2GO(y,u,v) >> 2) & 0x3f;
    const uint16_t b = (YUV2BO(y,u,v) >> 3) & 0x1f;
    return RGB565(r, g, b);
}
/* Converts YUV color to RGB32. */
static __inline__ uint32_t
YUVToRGB32(int y, int u, int v)
{
    /* Calculate C, D, and E values for the optimized macro. */
    y -= 16; u -= 128; v -= 128;
    RGB32_t rgb;
    rgb.r = YUV2RO(y,u,v) & 0xff;
    rgb.g = YUV2GO(y,u,v) & 0xff;
    rgb.b = YUV2BO(y,u,v) & 0xff;
    return rgb.color;
}
/* YUV pixel descriptor. */
struct YUVPixel {
    uint8_t     Y;
    uint8_t     U;
    uint8_t     V;
    inline YUVPixel()
        : Y(0), U(0), V(0)
    {
    }
    inline explicit YUVPixel(uint16_t rgb565)
    {
        RGB565ToYUV(rgb565, &Y, &U, &V);
    }
    inline explicit YUVPixel(uint32_t rgb32)
    {
        RGB32ToYUV(rgb32, &Y, &U, &V);
    }
    inline void get(uint8_t* pY, uint8_t* pU, uint8_t* pV) const
    {
        *pY = Y; *pU = U; *pV = V;
    }
};
/* Converts an YV12 framebuffer to RGB565 framebuffer.
 * Param:
 *  yv12 - YV12 framebuffer.
 *  rgb - RGB565 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void YV12ToRGB565(const void* yv12, void* rgb, int width, int height);
/* Converts an YV12 framebuffer to RGB32 framebuffer.
 * Param:
 *  yv12 - YV12 framebuffer.
 *  rgb - RGB32 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void YV12ToRGB32(const void* yv12, void* rgb, int width, int height);
/* Converts an YU12 framebuffer to RGB32 framebuffer.
 * Param:
 *  yu12 - YU12 framebuffer.
 *  rgb - RGB32 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void YU12ToRGB32(const void* yu12, void* rgb, int width, int height);
/* Converts an NV12 framebuffer to RGB565 framebuffer.
 * Param:
 *  nv12 - NV12 framebuffer.
 *  rgb - RGB565 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void NV12ToRGB565(const void* nv12, void* rgb, int width, int height);
/* Converts an NV12 framebuffer to RGB32 framebuffer.
 * Param:
 *  nv12 - NV12 framebuffer.
 *  rgb - RGB32 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void NV12ToRGB32(const void* nv12, void* rgb, int width, int height);
/* Converts an NV21 framebuffer to RGB565 framebuffer.
 * Param:
 *  nv21 - NV21 framebuffer.
 *  rgb - RGB565 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void NV21ToRGB565(const void* nv21, void* rgb, int width, int height);
/* Converts an NV21 framebuffer to RGB32 framebuffer.
 * Param:
 *  nv21 - NV21 framebuffer.
 *  rgb - RGB32 framebuffer.
 *  width, height - Dimensions for both framebuffers.
 */
void NV21ToRGB32(const void* nv21, void* rgb, int width, int height);
}; /* namespace chroma */
#endif  /* HW_EMULATOR_CAMERA_CONVERTERS_H */

Converters.cpp

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Contains implemenation of framebuffer conversion routines.
 */
#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera_Converter"
#include "Converters.h"
namespace chroma {
    static void _YUV420SToRGB565(const uint8_t* Y,
                                 const uint8_t* U,
                                 const uint8_t* V,
                                 int dUV,
                                 uint16_t* rgb,
                                 int width,
                                 int height)
    {
        const uint8_t* U_pos = U;
        const uint8_t* V_pos = V;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x += 2, U += dUV, V += dUV) {
                const uint8_t nU = *U;
                const uint8_t nV = *V;
                *rgb = YUVToRGB565(*Y, nU, nV);
                Y++; rgb++;
                *rgb = YUVToRGB565(*Y, nU, nV);
                Y++; rgb++;
            }
            if (y & 0x1) {
                U_pos = U;
                V_pos = V;
            } else {
                U = U_pos;
                V = V_pos;
            }
        }
    }
    static void _YUV420SToRGB32(const uint8_t* Y,
                                const uint8_t* U,
                                const uint8_t* V,
                                int dUV,
                                uint32_t* rgb,
                                int width,
                                int height)
    {
        const uint8_t* U_pos = U;
        const uint8_t* V_pos = V;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x += 2, U += dUV, V += dUV) {
                const uint8_t nU = *U;
                const uint8_t nV = *V;
                *rgb = YUVToRGB32(*Y, nU, nV);
                Y++; rgb++;
                *rgb = YUVToRGB32(*Y, nU, nV);
                Y++; rgb++;
            }
            if (y & 0x1) {
                U_pos = U;
                V_pos = V;
            } else {
                U = U_pos;
                V = V_pos;
            }
        }
    }
    void YV12ToRGB565(const void* yv12, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* Y = reinterpret_cast<const uint8_t*>(yv12);
        const uint8_t* U = Y + pix_total;
        const uint8_t* V = U + pix_total / 4;
        _YUV420SToRGB565(Y, U, V, 1, reinterpret_cast<uint16_t*>(rgb), width, height);
    }
    void YV12ToRGB32(const void* yv12, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* Y = reinterpret_cast<const uint8_t*>(yv12);
        const uint8_t* V = Y + pix_total;
        const uint8_t* U = V + pix_total / 4;
        _YUV420SToRGB32(Y, U, V, 1, reinterpret_cast<uint32_t*>(rgb), width, height);
    }
    void YU12ToRGB32(const void* yu12, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* Y = reinterpret_cast<const uint8_t*>(yu12);
        const uint8_t* U = Y + pix_total;
        const uint8_t* V = U + pix_total / 4;
        _YUV420SToRGB32(Y, U, V, 1, reinterpret_cast<uint32_t*>(rgb), width, height);
    }
/* Common converter for YUV 4:2:0 interleaved to RGB565.
 * y, u, and v point to Y,U, and V panes, where U and V values are interleaved.
 */
    static void _NVXXToRGB565(const uint8_t* Y,
                              const uint8_t* U,
                              const uint8_t* V,
                              uint16_t* rgb,
                              int width,
                              int height)
    {
        _YUV420SToRGB565(Y, U, V, 2, rgb, width, height);
    }
/* Common converter for YUV 4:2:0 interleaved to RGB32.
 * y, u, and v point to Y,U, and V panes, where U and V values are interleaved.
 */
    static void _NVXXToRGB32(const uint8_t* Y,
                             const uint8_t* U,
                             const uint8_t* V,
                             uint32_t* rgb,
                             int width,
                             int height)
    {
        _YUV420SToRGB32(Y, U, V, 2, rgb, width, height);
    }
    void NV12ToRGB565(const void* nv12, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* y = reinterpret_cast<const uint8_t*>(nv12);
        _NVXXToRGB565(y, y + pix_total, y + pix_total + 1,
                      reinterpret_cast<uint16_t*>(rgb), width, height);
    }
    void NV12ToRGB32(const void* nv12, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* y = reinterpret_cast<const uint8_t*>(nv12);
        _NVXXToRGB32(y, y + pix_total, y + pix_total + 1,
                     reinterpret_cast<uint32_t*>(rgb), width, height);
    }
    void NV21ToRGB565(const void* nv21, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* y = reinterpret_cast<const uint8_t*>(nv21);
        _NVXXToRGB565(y, y + pix_total + 1, y + pix_total,
                      reinterpret_cast<uint16_t*>(rgb), width, height);
    }
    void NV21ToRGB32(const void* nv21, void* rgb, int width, int height)
    {
        const int pix_total = width * height;
        const uint8_t* y = reinterpret_cast<const uint8_t*>(nv21);
        _NVXXToRGB32(y, y + pix_total + 1, y + pix_total,
                     reinterpret_cast<uint32_t*>(rgb), width, height);
    }
}; /* namespace chroma */

【讨论】:

  • 因此,要转换您自己管理存储的像素,只需编译 Converters.h 和 .cpp,并使用第一个代码块作为如何调用 RGB565ToYUV 及其姊妹函数的示例。
  • @CamilleGoudeseune 是的。
【解决方案3】:

一旦你夹住,你就完成了。它们变成不同的颜色,你不能回去。如果你想看的话,我已经写了一些我自己的代码给convert between all of those and more,但这无助于将钳位颜色反转回原来的颜色。

【讨论】:

  • 谢谢@Zac,不知道为什么会这样。
猜你喜欢
  • 2012-11-25
  • 2014-09-18
  • 2014-01-25
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多