YUVtoJPG matlab

1 篇文章 0 订阅
1 篇文章 0 订阅

///

函数一

function YUVtoJPEG

    for ii = 0:29    
         [Y,U,V] = ReadMultiFrames('mobile_cif_300f.yuv','cif',[ii,ii]);


         U = imresize(U,2,'nearest');
         V = imresize(V,2,'nearest');


         fb = 2.032 * U + Y;
         fr = 1.140 * V + Y;
         fg = Y - 0.394 * U - 0.581 * V;


         f = cat(3,fr,fg,fb);
         fs = mat2gray(f);
         %figure
         %imshow(fs);
         imwrite(fs,strcat(num2str(ii),'.jpg'),'jpg');
    end

end

函数二

function [Y,U,V] = ReadMultiFrames(yuvfilename,format,init2last)
%该函数用于将yuv格式的视频连续多帧分别读入到三维数组Y,U,V之中
%yuv视频的采样格式为4:2:0
%输入参数:
%      yuvfilename  ---- 视频yuv文件路径名
%      format       ---- 视频格式(格式名or分辨率[rows,cols])
%      init2last    ----  读取的视频帧的范围[初始帧序号,终止帧序号]
%输出参数:
%         Y         ---- 亮度,三维数组,第三维为帧序号,前两维是单帧的行和列
%        U,V        ---- 色差,三维数组,第三维为帧序号,前两维是单帧的行和列
%调用示范:
%  [Y,U,V] = ReadMultiFrames('.\videoname.yuv','cif',[100,101]);
%  [Y,U,V] = ReadMultiFrames('.\videoname.yuv',[288,352],[100,101]);
close all;
if ischar(format)
    format = lower(format);
    switch format
        case 'sub_qcif'
            cols = 128; rows = 96;
        case 'qcif'
            cols = 176; rows = 144;
        case 'cif'
            cols = 352; rows = 288;
        case 'sif'
            cols = 352; rows = 240;
        case '4cif'
            cols = 704; rows = 576;
        otherwise
            error('no format!');
    end
elseif isequal(size(format),[1,2])||isequal(size(format),[2,1])
    cols = format(2);rows = format(1);
else
    error('第二参数输入有误!');
end




point = fopen(yuvfilename,'r');
if point == -1
    error('打开文件失败!');
end


order_num = init2last(1);
frames_num = init2last(2) - order_num + 1;
k = 0;
Y = zeros(rows,cols,frames_num);
U = zeros(rows/2,cols/2,frames_num);
V = U;


offset = order_num*(rows*cols + rows*cols/2);
status = fseek(point,offset,'bof');
pro = fread(point,1,'uchar');
if (isempty(pro)&&feof(point)) || status ~=0
    error('读取位置定位失败!');
end
fseek(point,-1,'cof');


for ii = 1:frames_num
    k = k + 1;
    pro = fread(point,1,'uchar');
    if feof(point)&&isempty(pro)
        disp('读取帧数范围已超过yuv视频总帧数!');
        Y = Y(:,:,1:k-1);
        U = U(:,:,1:k-1);
        V = V(:,:,1:k-1);
        break;
    end
    fseek(point,-1,'cof');
    temp = fread(point,[cols,rows],'uchar');
    Y(:,:,ii) = temp';
    temp = fread(point,[cols/2,rows/2],'uchar');
    U(:,:,ii) = temp';
    temp = fread(point,[cols/2,rows/2],'uchar');
    V(:,:,ii) = temp';
end
fclose(point);
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
利用jpeglib库JPEG static void put_jpeg_yuv420p_file(FILE *fp, unsigned char *image, int width, int height, int quality) { int i, j; JSAMPROW y[16],cb[16],cr[16]; // y[2][5] = color sample of row 2 and pixel column 5; (one plane) JSAMPARRAY data[3]; // t[0][2][5] = color sample 0 of row 2 and column 5 struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; data[0] = y; data[1] = cb; data[2] = cr; cinfo.err = jpeg_std_error(&jerr); // Errors get written to stderr jpeg_create_compress(&cinfo); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; jpeg_set_defaults(&cinfo); jpeg_set_colorspace(&cinfo, JCS_YCbCr); cinfo.raw_data_in = TRUE; // Supply downsampled data #if JPEG_LIB_VERSION >= 70 //#warning using JPEG_LIB_VERSION >= 70 cinfo.do_fancy_downsampling = FALSE; // Fix segfault with v7 #endif cinfo.comp_info[0].h_samp_factor = 2; cinfo.comp_info[0].v_samp_factor = 2; cinfo.comp_info[1].h_samp_factor = 1; cinfo.comp_info[1].v_samp_factor = 1; cinfo.comp_info[2].h_samp_factor = 1; cinfo.comp_info[2].v_samp_factor = 1; jpeg_set_quality(&cinfo, quality, TRUE); cinfo.dct_method = JDCT_FASTEST; jpeg_stdio_dest(&cinfo, fp); // Data written to file jpeg_start_compress(&cinfo, TRUE); for (j = 0; j < height; j += 16) { for (i = 0; i < 16; i++) { y[i] = image + width * (i + j); if (i % 2 == 0) { cb[i / 2] = image + width * height + width / 2 * ((i + j) / 2); cr[i / 2] = image + width * height + width * height / 4 + width / 2 * ((i + j) / 2); } } jpeg_write_raw_data(&cinfo, data, 16); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值