YUV420相关问题

baiux 2017-12-04 01:55:06
需要实现的功能:将一张yuv420格式的图片,以(x,y)为圆心,r为半径的圆外所有像素点设置成黑色的。圆内保持不变。
(以下代码新建一个yuv图片,圆内的像素颜色默认,圆外rgb黑色)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>
#include <math.h>

//背景色
#define R 0
#define G 0
#define B 0

#define Y 0.299*R + 0.587*G + 0.114*B
#define U -0.1687*R - 0.3313*G + 0.5*B + 128
#define V 0.5*R - 0.4187*G - 0.0813*B + 128

int main()
{
int width = 256;
int height = 256;

FILE *fp=fopen("F:/yuv/256x256.yuv","wb");

unsigned char *pic = (unsigned char *)malloc(width*height*3/2);

memset(pic, 0, width*height*3/2);

//圆心 半径
double centerHX = 128;
double centerWY = 128;
float radius = 50.0;

for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
float nLength = 0.0;
nLength = sqrt(pow(centerHX - i, 2) + pow(centerWY - j, 2));
if(nLength > radius)
{
pic[i*width + j]=Y;
pic[width*height + i*(width/4) + j/4] = U;
pic[width*height*5/4 + i*(width/4) + j/4] = V;
}
}
}

fwrite(pic,width*height*3/2,1,fp);
fclose(fp);
free(pic);

return 0;
}
编译通过,得到的结果确不对,有没有大神帮忙看下,问题出现在什么地方???
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
FoolCarpe 2017-12-05
  • 打赏
  • 举报
回复
根据https://en.wikipedia.org/wiki/YUV 公式
size.total = size.width * size.height;
y = yuv[position.y * size.width + position.x];
u = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total];
v = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total + (size.total / 4)];
代码修改成:
					pic[j * width + i] = Y;
					pic[(j / 2) * (width / 2) + i / 2 + width * height] = U;
					pic[(j / 2) * (width / 2) + i / 2 + width * height + width * height / 4] = V;
baiux 2017-12-05
  • 打赏
  • 举报
回复
引用 2 楼 hefashion0190 的回复:
根据https://en.wikipedia.org/wiki/YUV 公式
size.total = size.width * size.height;
y = yuv[position.y * size.width + position.x];
u = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total];
v = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total + (size.total / 4)];
代码修改成:
					pic[j * width + i] = Y;
					pic[(j / 2) * (width / 2) + i / 2 + width * height] = U;
					pic[(j / 2) * (width / 2) + i / 2 + width * height + width * height / 4] = V;
非常感谢~不过我还要理解一下
baiux 2017-12-04
  • 打赏
  • 举报
回复

64,653

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧