matlab yuv分量图提取,如何使用Matlab从给定的yuv文件中提取Y,U和V分量?每个组件用于进一步的像素级操纵...

icon1.gif 如何使用Matlab从给定的yuv文件中提取Y,U和V分量?每个组件用于进一步的像素级操纵

大家好。我目前正在使用YUV文件。您对如何从yuv视频中提取y,u,v分量有任何建议吗?我找到了一个程序 ,如下所示。但是我不知道哪一部分是我想要的有效组件。谢谢。

% function mov = loadFileYuv(fileName, width, height, idxFrame) function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame) % load RGB movie [0, 255] from YUV 4:2:0 file fileId = fopen(fileName, 'r'); subSampleMat = [1, 1; 1, 1]; nrFrame = length(idxFrame); for f = 1 : 1 : nrFrame % search fileId position sizeFrame = 1.5 * width * height; fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof'); % read Y component buf = fread(fileId, width * height, 'uchar'); imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape RESHAPE(X,M,N) returns the M-by-N matrix %whose elements are taken columnwise from X. %An error results if X does not have M*N elements % read U component buf = fread(fileId, width / 2 * height / 2, 'uchar'); imgYuv(:, :, 2) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample % read V component buf = fread(fileId, width / 2 * height / 2, 'uchar'); imgYuv(:, :, 3) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample % normalize YUV values % imgYuv = imgYuv / 255; % convert YUV to RGB imgRgb = reshape(convertYuvToRgb(reshape(imgYuv, height * width, 3)), height, width, 3); % imgRgb = ycbcr2rgb(imgYuv); %imwrite(imgRgb,'ActualBackground.bmp','bmp'); mov(f) = im2frame(imgRgb); % mov(f).cdata = uint8(imgRgb); % mov(f).colormap = []; % imwrite(imgRgb,'ActualBackground.bmp','bmp'); %figure, imshow(imgRgb); %name = 'ActualBackground.bmp'; %Image = imread(name, 'bmp'); %figure, imshow(Image); end fclose(fileId);

回答:

不知道我是否对YUV文件有一些基本的误解,但是如果您像下面那样编辑函数,则每个帧中的YUV组件都在一个名为imgYUV变量中。请注意,您可能会在此过程中耗尽内存,或者不想一次性加载电影的所有帧。

function imgYUV = loadFileYuv(fileName, width, height, idxFrame) % load YUV data from YUV 4:2:0 file fileId = fopen(fileName, 'r'); subSampleMat = [1, 1; 1, 1]; nrFrame = length(idxFrame); %# preassign imgYUV. In case we can't keep everything in RAM, %# it is better that the function crashes here, rather than after %# having wasted time slowly filling up the memory. %# Since the images are likely to be of class 'uint8', %# you can save on a lot of memory by initializing %# imgYUV as zeros(width/2,height/2,3,nrFrame,'uint8'); imgYUV = zeros(width/2 height/2, 3, nrFrame); for f = 1 : 1 : nrFrame %# search fileId position sizeFrame = 1.5 * width * height; fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof'); %# read Y component buf = fread(fileId, width * height, 'uchar'); imgYuv(:, :, 1, f) = reshape(buf, width, height).'; %# read U component buf = fread(fileId, width / 2 * height / 2, 'uchar'); imgYuv(:, :, 2, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample % read V component buf = fread(fileId, width / 2 * height / 2, 'uchar'); imgYuv(:, :, 3, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample end fclose(fileId);

更多&回答...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将两个片作为水印嵌入一个YUV像的Y和U层的MATLAB代码: ```matlab % 读取载体像 carrier = imread('carrier.jpg'); % 将载体像转换为YUV格式 yuv_carrier = rgb2ycbcr(carrier); % 读取第一个水印像 watermark1 = imread('watermark1.png'); % 读取第二个水印像 watermark2 = imread('watermark2.png'); % 将两个水印像合并 watermark = cat(3, watermark1, watermark2); % 将水印像转换为灰度像 watermark_gray = rgb2gray(watermark); % 调整水印像的大小以匹配载体像 watermark_resized = imresize(watermark_gray, size(yuv_carrier(:,:,1))); % 嵌入水印到Y和U层 alpha = 0.5; % 水印强度 yuv_carrier(:,:,1) = yuv_carrier(:,:,1) + alpha * watermark_resized; yuv_carrier(:,:,2) = yuv_carrier(:,:,2) + alpha * watermark_resized; % 将YUV格式像转换回RGB格式 rgb_watermarked = ycbcr2rgb(yuv_carrier); % 显示结果 subplot(1,2,1); imshow(carrier); title('原始载体像'); subplot(1,2,2); imshow(rgb_watermarked); title('嵌入水印后的像'); % 提取水印 extracted_watermark1 = (yuv_carrier(:,:,1) - yuv_carrier(:,:,2)) / alpha; extracted_watermark2 = (yuv_carrier(:,:,2) - yuv_carrier(:,:,1)) / alpha; extracted_watermark = cat(3, extracted_watermark1, extracted_watermark2); extracted_watermark = uint8(extracted_watermark); % 显示提取的水印 figure; imshow(extracted_watermark); title('提取的水印像'); ``` 需要注意的是,此代码假设水印像已经调整为与载体像相同大小。如果水印像大小与载体像不同,需要进行相应的调整。另外,此代码假设水印是灰度像,如果水印是彩色像,需要将其转换为灰度像或将其嵌入到Y、U和V层

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值