matlab公共函数之RGB与YUV转换

本文介绍了MATLAB中RGB色彩空间到YUV色彩空间的转换,特别是在Y的值要求满足[0, 255]规范下的转换方法。提供了一种不同于内置函数的自定义脚本,以满足特定需求。" 90181466,8569874,机器学习基础:中心极限定理与正态分布,"['机器学习', '统计学', '优化', '数学基础', '深度学习']
摘要由CSDN通过智能技术生成

matlab中有自带的rgb转ycbcr函数,但是根据观测,其Y的值为[16 235],不符合我们的要求,所以,提供另一种规范下的转换脚本函数,其Y的值满足[0 255]

RGB转YUV

% function yuv = myrgb2yuv(image)
% input params.
% image: input color image with 3 channels, which value must be [0 255]
% output 
% yuv: 3 channels(YUV444, Y plane, U plane, V plane), value [0 255], double
%
%
% Author: KevenLee 
% Contact: hudalikm@163.com
% Version: V1.0

function yuv = myrgb2yuv(image)
image = double(image);
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);

yuv(:,:,1) = 0.299.*R + 0.587.*G + 0.114.*B;
yuv(:,:,2) = - 0.1687.*R - 0.3313.*G + 0.5.*B + 128;
yuv(:,:,3) = 0.5.*R - 0.4187.*G - 0.0813.*B + 128;
end

 YUV转RGB

% fun
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
保存YUV数据是视频处理中常用的操作之一,MATLAB提供了许多函数来实现这个功能。下面是一个简单的MATLAB公共函数,可以将一个YUV数据保存为一个YUV文件。 ```matlab function save_yuv(yuv_file, Y, U, V, width, height) % Save YUV data to a YUV file % Inputs: % yuv_file: the path of the YUV file to save % Y: the Y component of the YUV data (a matrix of size height x width) % U: the U component of the YUV data (a matrix of size height/2 x width/2) % V: the V component of the YUV data (a matrix of size height/2 x width/2) % width: the width of the video frame % height: the height of the video frame % Open the YUV file for writing fid = fopen(yuv_file, 'w'); % Write the Y component fwrite(fid, Y', 'uint8'); % Write the U component (downsampled by a factor of 2 in both dimensions) U_d = imresize(U, 0.5, 'bilinear'); fwrite(fid, U_d', 'uint8'); % Write the V component (downsampled by a factor of 2 in both dimensions) V_d = imresize(V, 0.5, 'bilinear'); fwrite(fid, V_d', 'uint8'); % Close the YUV file fclose(fid); end ``` 使用该函数,可以将YUV数据写入到一个YUV文件中。例如: ```matlab % Generate some sample YUV data width = 640; height = 480; Y = uint8(rand(height, width)*255); U = uint8(rand(height/2, width/2)*255); V = uint8(rand(height/2, width/2)*255); % Save the YUV data to a file save_yuv('test.yuv', Y, U, V, width, height); ``` 这个函数可以方便地将YUV数据保存到文件中,并且支持不同分辨率的视频。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值