简体   繁体   English

如何在MatLab中打开原始的YCbCr图像文件?

[英]How to open a raw YCbCr image file in MatLab?

I am trying to open a 1920x1080 YCbCr raw image file in Matlab, however I am having difficulties getting the following code below to work: 我试图在Matlab中打开1920x1080 YCbCr原始图像文件,但是我很难使以下代码正常工作:

fid = fopen(filePath);
image = fread(fid, 1920*1080, 'uint8=>uint8');
fclose(fid); 
image = reshape(image, 1080, 1920);

However, when I go to show the image, it does not look as expected. 但是,当我显示图像时,它看起来不像预期的那样。

The actual image should be colour, however I get a strange black and white image, not resembling the expected output at all. 实际的图像应该是彩色的,但是我得到一个奇怪的黑白图像,根本不符合预期的输出。

I have also tried loading it into a 3D array, with each dimension representing one of the Y, Cb and Cr channels, however this also produced a similar output as described before. 我还尝试将其加载到3D数组中,每个维度代表Y,Cb和Cr通道之一,但是这也产生了与前面所述类似的输出。

Any help would be appreciated. 任何帮助,将不胜感激。

Ignore this bit and look below at the EDIT: 忽略此位,然后在下面查看EDIT:

I don't understand why you are using fread ? 我不明白您为什么要使用fread Why not use imread , which is mean't for reading images? 为什么不使用imread ,这并不意味着读取图像? Using this infamous original image , as a base for my test script, I could display a YCbCr image, as shown in the little script below. 使用这个臭名昭著的原始图像作为测试脚本的基础,我可以显示YCbCr图像,如下面的小脚本所示。

 original = imread("lenna.jpg"); % figure, imshow(original); % if you want to see how the original image looks YCbCr_version = rgb2ycbcr(original); % figure, imshow(YCbCr_version); % if you want to see how the YCbCr image looks imwrite(YCbCr_version, "out.jpg"); YCbCr_fromFile = imread("out.jpg"); figure, imshow(YCbCr_fromFile); 

EDIT: 编辑:

  • IF however you have a binary version of the file and can only read it using using fread , 但是,如果您拥有该文件的二进制版本,并且只能使用fread读取,

then the following script should work, 那么下面的脚本应该工作,

clc;
clear;
close all;

original = imread("lenna.jpg");
% figure, imshow(original); % if you want to see how the original image looks
YCbCr_version = rgb2ycbcr(original);
% figure, imshow(YCbCr_version);  % if you want to see how the YCbCr image looks

fileID = fopen('out.bin','w');
fwrite(fileID, YCbCr_version, 'uint8');
fclose(fileID);

fileID = fopen('out.bin','r');
fromFile = fread(fileID, 512*600*3, 'uint8=>uint8');
fclose(fileID);
image = reshape(fromFile, 512, 600, 3);
imagesc(image)

The point is, in the read operation, you have to give the 3 channels in the multiplier also, as color images have this 3rd dimension, ie 512*600*3 . 关键是,在读取操作中,您还必须在乘法器中提供3个通道,因为彩色图像具有第3维,即512*600*3 If you only give 512*600 , as you were doing, you would have no color info. 如果您只给512*600 ,那么您将没有颜色信息。 Also the reshape function would need to be changed to take into account the 3rd dimension. 同样,还需要更改重塑功能以考虑第三维。 Hence, reshape(fromFile, 512,600, 3) . 因此, reshape(fromFile, 512,600, 3)

  • YCbCr version loaded from the file 从文件加载的YCbCr版本

从文件加载的YCbCr版本

As you said "Any help would be appreciated" , I thought I would mention you can simply convert a raw YCbCr file to PNG, TIFF, JPEG or any other format file with ImageMagick which is installed on most Linux distros and is available for macOS and Windows. 正如您所说的“任何帮助将不胜感激” ,我想我想您可以使用ImageMagick将原始的YCbCr文件转换为PNG,TIFF,JPEG或任何其他格式的文件,该文件已安装在大多数Linux发行版中,并且可用于macOS和视窗。

Start a Terminal, (or Command Prompt if under Windows), and convert the YCbCr image.raw to PNG with: 启动终端(如果在Windows下,则启动命令提示符),然后使用以下命令将YCbCr image.raw转换为PNG:

magick -size 1920x1080 -depth 8  YCbCr:image.raw  result.png

Or, say a CCIR 601 YUV file to NetPBM PPM format: 或者,将CCIR 601 YUV文件转换为NetPBM PPM格式:

magick -size 800x600 -depth 8 YUV:image.raw result.ppm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM