cvtColor把yuv420sp转RGB24
时间: 2023-07-30 20:08:15 浏览: 124
可以使用以下代码将YUV420SP图像转换为RGB24:
```cpp
void YUV420SP_to_RGB24(unsigned char* yuv420sp, unsigned char* rgb24, int width, int height) {
int frameSize = width * height;
int i = 0, j = 0, y = 0;
int uvp = 0, u = 0, v = 0;
int r = 0, g = 0, b = 0;
for (j = 0, y = 0; j < height; j++) {
uvp = frameSize + (j >> 1) * width;
u = 0;
v = 0;
for (i = 0; i < width; i++, y++) {
int yvalue = (0xff & ((int) yuv420sp[y]));
if (i % 2 == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
r = (int) (yvalue + 1.370705 * v);
g = (int) (yvalue - 0.698001 * v - 0.337633 * u);
b = (int) (yvalue + 1.732446 * u);
r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
rgb24[y * 3] = (unsigned char) (r);
rgb24[y * 3 + 1] = (unsigned char) (g);
rgb24[y * 3 + 2] = (unsigned char) (b);
}
}
}
```
其中,yuv420sp是输入的YUV420SP图像数据,rgb24是输出的RGB24图像数据,width和height分别是图像的宽度和高度。
阅读全文