如何在 C 中将 jpg 转换为 yuv?
How to convert a jpg to yuv in C?
尽管这种性质的问题听起来非常相似,但我在将 jpg 图像转换为 C 中的 yuv 时遇到了问题(不使用 opencv)。
这是我目前的理解,如何解决这个问题:
Identify the structure of file formats for jpg and yuv. i.e what each byte in the file actually contains. This is what I think jpg format looks like.
With the above structure I tried to read a jpg file and tried to decipher its 18th and 19th bytes. I did type cast them to both char and int but I don`t get any meaningful values for width and height of the image.
Once I have read these values, I should be able to convert them from jpg to yuv. I was looking at this resource.
Appropriately, construct yuv image and write it to a (.yuv) file.
请帮我指点合适的资源。我会继续更新我在这个 post 上的进展。提前致谢。
基于https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
解码 JPEG,在没有库的纯 C 中很好......下面的代码有点简单......
https://bitbucket.org/Halicery/firerainbow-progressive-jpeg-decoder/src
假设您使用上述方法或库(使用库可能更容易)将 jpeg 解码为 rgb。
int width = (width of the image);
int height = (height of the image);
byte *mydata = (pointer to rgb pixels);
byte *cursor;
size_t byte_count = (length of the pixels .... i.e. width x height x 3);
int n;
for (cursor = mydata, n = 0; n < byte_count; cursor += 3, n += 3)
{
int red = cursor[0], green = cursor[1], blue = cursor[2];
int y = 0.299 * red + 0.587 * green + 0.114 * blue;
int u = -0.147 * red + -0.289 * green + 0.436 * blue;
int v = 0.615 * red + -0.515 * green + -0.100 * blue;
cursor[0] = y, cursor[1] = u, cursor[2] = v;
}
// At this point, the entire image has been converted to yuv ...
并将其写入文件...
FILE* fout = fopen ("myfile.yuv, "wb");
if (fout) {
fwrite (mydata, 1, byte_count, fout);
fclose (fout);
}
不确定您对 opencv
有什么意见,也许 ImageMagick
对您来说是可以接受的?它安装在大多数 Linux 失真器上,可用于 OSX 和 Windows。它有 C 绑定,还有我在这里展示的命令行版本。所以你可以像这样创建一个图像:
# Create test image
convert -size 100x100 \
\( xc:red xc:lime xc:blue +append \) \
\( xc:cyan xc:magenta xc:yellow +append \) \
-append image.jpg
现在转换为 YUV 并写入 3 个单独的文件:
convert image.jpg -colorspace yuv -separate bands.jpg
波段-0.jpg (Y)
频带-1.jpg (U)
频带-2.jpg(V)
或者,更接近您的要求,将所有三个波段 YUV
写入二进制文件:
convert image.jpg -colorspace yuv rgb:yuv.bin
通常图像已经存储在 YUV(或者,更准确地说:YCbCr)中。
读取文件时,jpeg reader 通常会将 YUV 转换为 RGB。转换回去会稍微降低质量。
在 libTurboJpeg 中(http://libjpeg-turbo.virtualgl.org/) you can read the jpeg without color conversion. Check https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/turbojpeg.h -
它具有 tjDecompressToYUV 函数,可以在 3 个不同的输出缓冲区上为您提供 3 个色彩空间。
尽管这种性质的问题听起来非常相似,但我在将 jpg 图像转换为 C 中的 yuv 时遇到了问题(不使用 opencv)。
这是我目前的理解,如何解决这个问题:
Identify the structure of file formats for jpg and yuv. i.e what each byte in the file actually contains. This is what I think jpg format looks like.
With the above structure I tried to read a jpg file and tried to decipher its 18th and 19th bytes. I did type cast them to both char and int but I don`t get any meaningful values for width and height of the image.
Once I have read these values, I should be able to convert them from jpg to yuv. I was looking at this resource.
Appropriately, construct yuv image and write it to a (.yuv) file.
请帮我指点合适的资源。我会继续更新我在这个 post 上的进展。提前致谢。
基于https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
解码 JPEG,在没有库的纯 C 中很好......下面的代码有点简单......
https://bitbucket.org/Halicery/firerainbow-progressive-jpeg-decoder/src
假设您使用上述方法或库(使用库可能更容易)将 jpeg 解码为 rgb。
int width = (width of the image);
int height = (height of the image);
byte *mydata = (pointer to rgb pixels);
byte *cursor;
size_t byte_count = (length of the pixels .... i.e. width x height x 3);
int n;
for (cursor = mydata, n = 0; n < byte_count; cursor += 3, n += 3)
{
int red = cursor[0], green = cursor[1], blue = cursor[2];
int y = 0.299 * red + 0.587 * green + 0.114 * blue;
int u = -0.147 * red + -0.289 * green + 0.436 * blue;
int v = 0.615 * red + -0.515 * green + -0.100 * blue;
cursor[0] = y, cursor[1] = u, cursor[2] = v;
}
// At this point, the entire image has been converted to yuv ...
并将其写入文件...
FILE* fout = fopen ("myfile.yuv, "wb");
if (fout) {
fwrite (mydata, 1, byte_count, fout);
fclose (fout);
}
不确定您对 opencv
有什么意见,也许 ImageMagick
对您来说是可以接受的?它安装在大多数 Linux 失真器上,可用于 OSX 和 Windows。它有 C 绑定,还有我在这里展示的命令行版本。所以你可以像这样创建一个图像:
# Create test image
convert -size 100x100 \
\( xc:red xc:lime xc:blue +append \) \
\( xc:cyan xc:magenta xc:yellow +append \) \
-append image.jpg
现在转换为 YUV 并写入 3 个单独的文件:
convert image.jpg -colorspace yuv -separate bands.jpg
波段-0.jpg (Y)
频带-1.jpg (U)
频带-2.jpg(V)
或者,更接近您的要求,将所有三个波段 YUV
写入二进制文件:
convert image.jpg -colorspace yuv rgb:yuv.bin
通常图像已经存储在 YUV(或者,更准确地说:YCbCr)中。 读取文件时,jpeg reader 通常会将 YUV 转换为 RGB。转换回去会稍微降低质量。
在 libTurboJpeg 中(http://libjpeg-turbo.virtualgl.org/) you can read the jpeg without color conversion. Check https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/turbojpeg.h - 它具有 tjDecompressToYUV 函数,可以在 3 个不同的输出缓冲区上为您提供 3 个色彩空间。