Y分量单独存放,UV分量交错存放(先U后V),2个Y共用1个UV,总长度为W×H×2。通道数为2,平均每个像素占16位,故称NV16格式。
Y Y Y Y
Y Y Y Y
U V U V
U V U V
二、NV16转BGRstatic void NV16_2_BGR(const cv::Mat &yuv_image, unsigned char *buffer) {int bgr_image_width = yuv_image.cols;
int bgr_image_height = yuv_image.rows / 2;
cv::Mat bgr_image = cv::Mat(bgr_image_height, bgr_image_width, CV_8UC3);
int length_y = bgr_image_width * bgr_image_height;
int length_uv = length_y >>1;
unsigned char *pt_yuv_y = yuv_image.data;
unsigned char *pt_yuv_uv = yuv_image.data + length_y;
for (int h = 0; h< bgr_image_height; ++h)
{cv::Vec3b *pt_bgr_image_line = bgr_image.ptr(h);
unsigned char *pt_yuv_y_line = pt_yuv_y + h * bgr_image_width;
unsigned char *pt_yuv_uv_line = pt_yuv_uv + h * bgr_image_width;
for (int w = 0; w< bgr_image_width; ++w)
{ unsigned char y, u, v;
int b, g, r;
y = pt_yuv_y_line[w];
if (w%2 == 0)
{u = pt_yuv_uv_line[w];
v = pt_yuv_uv_line[w+1];
}
if (w%2 == 1)
{u = pt_yuv_uv_line[w-1];
v = pt_yuv_uv_line[w];
}
r = y + ((360 * (v - 128) + 128) >>8);
g = y - (((88 * (u - 128) + 184 * (v - 128)) - 128) >>8);
b = y + ((455 * (u - 128) + 128) >>8);
if (b< 0)
b = 0;
if (b >255)
b = 255;
if (g< 0)
g = 0;
if (g >255)
g = 255;
if (r< 0)
r = 0;
if (r >255)
r = 255;
pt_bgr_image_line[w][0] = b;
pt_bgr_image_line[w][1] = g;
pt_bgr_image_line[w][2] = r;
}
}
int length = bgr_image_width*bgr_image_height*3;
memcpy(buffer, bgr_image.data, length);
}
三、BGR转NV16static void BGR_2_NV16(const cv::Mat &bgr_image, unsigned char *buffer) {int bgr_image_width = bgr_image.cols;
int bgr_image_height = bgr_image.rows;
int length_y = bgr_image_width * bgr_image_height;
int length_uv = length_y >>1;
unsigned char *pt_yuv_y = buffer;
unsigned char *pt_yuv_uv = buffer + length_y;
for (int h = 0; h< bgr_image_height; ++h)
{const cv::Vec3b *pt_bgr_image_line = bgr_image.ptr(h);
unsigned char *pt_yuv_y_line = pt_yuv_y + h * bgr_image_width;
unsigned char *pt_yuv_uv_line = pt_yuv_uv + h * bgr_image_width;
for (int w = 0; w< bgr_image_width; ++w)
{ unsigned char b, g, r;
int y, u, v;
b = pt_bgr_image_line[w][0];
g = pt_bgr_image_line[w][1];
r = pt_bgr_image_line[w][2];
y = (77 * r + 150 * g + 29 * b + 128) >>8;
if (y< 0)
y = 0;
if (y >255)
y = 255;
pt_yuv_y_line[w] = y;
if (w%2 == 0)
{u = ((-44 * r - 87 * g + 131 * b + 128) >>8) + 128;
v = ((131 * r - 110 * g - 21 * b + 128) >>8) + 128;
if (u< 0)
u = 0;
if (u >255)
u = 255;
if (v< 0)
v = 0;
if (v >255)
v = 255;
pt_yuv_uv_line[w] = u;
pt_yuv_uv_line[w+1] = v;
}
}
}
}
YUV格式详解参见:YUV 格式详解,只看这一篇就够了(转)
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
本文题目:NV16和BGR相互转换C++实现-创新互联
分享地址:https://www.cdcxhl.com/article16/coosgg.html
成都网站建设公司_创新互联,为您提供外贸网站建设、App设计、网站设计公司、企业网站制作、标签优化、外贸建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联