我是靠谱客的博主 积极发夹,最近开发中收集的这篇文章主要介绍图像基础:读取BMP图片,转换为RGB与YU12(YUV420P)格式,同时YU12、NV12、NV21之间相互转换BMP转RGBRGB转YU12(YUV420P)YU12转NV12YU12转NV21NV12转YU12NV21转YU12完整工程代码下载链接:,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
BMP转RGB
//输入:1、申请的RGB内存
// 2、打开的BMP文件
void ReadBmp(unsigned char *RGB, FILE *fp)
{
int i, j;
unsigned char temp;
//int fseek(FILE *stream, long offset, int fromwhere);
//指向以fromwhere为基准,偏移offset(指针偏移量)个字节的位置,函数返回0
//因为已知格式信息:分辨率为640*480 色深为24bit
//功能:跳过头文件
fseek(fp, 54, SEEK_SET);
//size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
//buffer 指向要读取的数组中首个对象的指针
//size 每个对象的大小(单位是字节)
//count 要读取的对象个数
//stream 输入流
//功能:将整个数据存储到RGB内存中的后半段中
fread(RGB + WIDTH*HEIGHT * 3, 1, WIDTH*HEIGHT * 3, fp);//读取
//功能:将RGB前半段按顺序填充
for (i = HEIGHT - 1, j = 0; i >= 0; i--, j++)//调整顺序
{
memcpy(RGB + j*WIDTH * 3, RGB + WIDTH*HEIGHT * 3 + i*WIDTH * 3, WIDTH * 3);
}
//功能:将R,G,B三个字节像素值顺序调整 B、G、R----》R、G、B
for (i = 0; (unsigned int)i < WIDTH*HEIGHT * 3; i += 3)
{
temp = RGB[i];
RGB[i] = RGB[i + 2];
RGB[i + 2] = temp;
}
}
RGB转YU12(YUV420P)
//输入:1、申请的RGB内存(已有数据)
// 2、申请的YUV内存
void Convert(unsigned char *RGB, unsigned char *YUV)
{
//变量声明
unsigned int i, x, y, j;
unsigned char *Y = NULL;
unsigned char *U = NULL;
unsigned char *V = NULL;
Y = YUV;
U = YUV + WIDTH*HEIGHT;
V = U + ((WIDTH*HEIGHT) >> 2);
for (y = 0; y < HEIGHT; y++)
for (x = 0; x < WIDTH; x++)
{
j = y*WIDTH + x;
i = j * 3;
Y[j] = (unsigned char)(DY(RGB[i], RGB[i + 1], RGB[i + 2]));
if (x % 2 == 1 && y % 2 == 1)
{
j = (WIDTH >> 1) * (y >> 1) + (x >> 1);
//上面i仍有效
U[j] = (unsigned char)
((DU(RGB[i], RGB[i + 1], RGB[i + 2]) +
DU(RGB[i - 3], RGB[i - 2], RGB[i - 1]) +
DU(RGB[i - WIDTH * 3], RGB[i + 1 - WIDTH * 3], RGB[i + 2 - WIDTH * 3]) +
DU(RGB[i - 3 - WIDTH * 3], RGB[i - 2 - WIDTH * 3], RGB[i - 1 - WIDTH * 3])) / 4);
V[j] = (unsigned char)
((DV(RGB[i], RGB[i + 1], RGB[i + 2]) +
DV(RGB[i - 3], RGB[i - 2], RGB[i - 1]) +
DV(RGB[i - WIDTH * 3], RGB[i + 1 - WIDTH * 3], RGB[i + 2 - WIDTH * 3]) +
DV(RGB[i - 3 - WIDTH * 3], RGB[i - 2 - WIDTH * 3], RGB[i - 1 - WIDTH * 3])) / 4);
}
}
}
YU12转NV12
void YU12_to_NV12( unsigned char* Src,
unsigned char* Dst,
int Width,
int Height) {
unsigned char* SrcU = Src + Width * Height;
unsigned char* SrcV = SrcU + Width * Height / 4;
memcpy(Dst, Src, Width * Height);
unsigned char* DstU = Dst + Width * Height;
for (int i = 0; i < Width * Height / 4; i++) {
(*DstU++) = (*SrcU++);
(*DstU++) = (*SrcV++);
}
}
YU12转NV21
void YU12_to_NV21(unsigned char *Src,
unsigned char *Dst,
int Width,
int Height){
unsigned char* SrcU = Src + Width * Height;
unsigned char* SrcV = SrcU + Width * Height / 4;
memcpy(Dst, Src, Width * Height);
unsigned char* DstV = Dst + Width * Height;
for (int i = 0; i < Width * Height / 4; i++)
{
(*DstV++) = (*SrcV++);
(*DstV++) = (*SrcU++);
}
}
NV12转YU12
void NV12_to_YU12( const unsigned char* image_src,
unsigned char* image_dst,
int image_width,
int image_height) {
unsigned char* p = image_dst;
memcpy(p, image_src, image_width * image_height * 3 / 2);
const unsigned char* pNV = image_src + image_width * image_height;
unsigned char* pU = p + image_width * image_height;
unsigned char* pV = p + image_width * image_height + ((image_width * image_height) >> 2);
for (int i = 0; i<(image_width * image_height) / 2; i++) {
if ((i % 2) == 0)
{
memcpy(pU, pNV + i, 1);
pU++;
}
else {
memcpy(pV, pNV + i, 1);
pV++;
}
}
}
NV21转YU12
void NV21_to_YU12(const unsigned char* image_src,
unsigned char* image_dst,
int image_width,
int image_height) {
unsigned char* p = image_dst;
memcpy(p, image_src, image_width * image_height * 3 / 2);
const unsigned char* pNV = image_src + image_width * image_height;
unsigned char* pU = p + image_width * image_height;
unsigned char* pV = p + image_width * image_height + ((image_width * image_height) >> 2);
for (int i = 0; i < (image_width * image_height) / 2; i++) {
if ((i % 2) == 0)
{
memcpy(pU, pNV + i, 1);
pU++;
}
else {
memcpy(pV, pNV + i, 1);
pV++;
}
}
}
完整工程代码下载链接:
https://download.csdn.net/download/weixin_43455834/12400337
最后
以上就是积极发夹为你收集整理的图像基础:读取BMP图片,转换为RGB与YU12(YUV420P)格式,同时YU12、NV12、NV21之间相互转换BMP转RGBRGB转YU12(YUV420P)YU12转NV12YU12转NV21NV12转YU12NV21转YU12完整工程代码下载链接:的全部内容,希望文章能够帮你解决图像基础:读取BMP图片,转换为RGB与YU12(YUV420P)格式,同时YU12、NV12、NV21之间相互转换BMP转RGBRGB转YU12(YUV420P)YU12转NV12YU12转NV21NV12转YU12NV21转YU12完整工程代码下载链接:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复