Add pretzels to YUV videos

Source: Internet
Author: User
You can read YUV videos by frame. Then add salt and pepper noise to each frame of the image and save it as a YUV video by frame. % Read the YUV file information by frame and save it after downsampling % note: the Y, U, and V components of the YUV file are processed separately. However, the read operation is performed at. For YUV file storage, the flat format is used instead of the packaging format function yuvsubsample (filename, framenummax, formatt, method, outputfilename) CLC; clear; % considering that no parameter is input when the function is called, the default parameter value if nargin = 0 framenummax = 16 is assigned; % Number of YUV frames read: considering the above sampling problem, this should be one frame more than the final output video frame formatt = 'cif '; % YUV format file type method = 'paper '; % YUV file downsampling method filename = 'coast _ CIF. YUV '; % YUV format file name outputfilename = 'Salt _ pepper_coast_cif.yuv'; % YUV file sampling, save end % initialize frame format switch format with another name T case 'qcif 'H = 144; % high W = 176; % wide case 'cif' H = 288; % high W = 352; % wide otherwise H = 480; % high W = 720; % wide end % read YUV file by frame and save as YUV format file for framenum = 1: framenummax % reads the file content of a specified frame in YUV format [y u v] As the raw data of a read frame [y, U, V] = yuv_import (filename, [W, h], 1, framenum); % perform subsampling Based on the specified subsampling method: [Yi ui vi] % of the data after downsampling. Processing y = imnoise (uint8 (Y), 'Salt & pepper ', 0.005) with prepaid noise added here ); U = imnoise (uint8 (u), 'Salt & pepper ', 0.005); V = Imnoise (uint8 (V), 'Salt & pepper ', 0.005); % displays y, U, V subplot (1, 3, 1), imshow (uint8 (y )); subplot (1, 3, 2), imshow (uint8 (u); subplot (1, 3), imshow (uint8 (V )); % output the [Yi ui vi] data after downsampling and save it as the YUV format yuv_output (outputfilename, Y, U, V, 1, framenum) of the specified file name ); % pause 1/1000 s pause (1/1000) when playing each frame of the image; end % read the original data of a specified frame in YUV format [y u v: in fact, a YUV frame file is an image function [y, U, V] = yuv_import (filename, dims, numfrm, startfrm) % opened in read-only mode and associated with the file FID = Fopen (filename, 'R'); If (FID <0) error ('file does not exist! '); End; % calculate the file size per frame, and calculate the file offset location YD = zeros (dims (1), dims (2) for reading a specified frame )); UVD = zeros (dims (1)/2, dims (2)/2); % UV component Position frelem = numel (YD) + 2 * numel (UVD ); % width of the video image display if (nargin = 4) % offset fseek (FID, startfrm * frelem, 0) calculated from the start position of the file; end; % initializes y, U, and V components. The YUV file format is: 4: 2: 2 y = zeros (dims (2), dims (1), numfrm ); U = zeros (dims (2)/2, dims (1)/2, numfrm); V = zeros (dims (2)/2, dims (1)/2, numfrm); % read the YUV component for I = 1: numfrm YD = fread (FID ,[ Dims (1) dims (2)], 'uint8'); y = YD '; % transpose UVD = fread (FID, [dims (1)/2 dims (2) /2], 'uint8'); U = UVD '; % transpose UVD = fread (FID, [dims (1)/2 dims (2)/2], 'uint8'); V = UVD '; % transpose end; % close the file and remove the associated fclose (FID); % output the [Yi ui vi] data after downsampling, function yuv_output (outputfilename, Yi, UI, Vi, numfrm, startfrm) % in the YUV format saved as the specified file name determines whether the input is the first frame. If this is the first input, overwrite the original file with the same name; otherwise, append the information after the file if startfrm = 1 fileid = fopen (outputfilename, 'w'); else fileid = fopen (outputfilename, 'A'); end if (fileid <0) error ('file does not exist! '); End; % [Yi ui vi] Data removal entire YI = round (yi); UI = round (UI); VI = round (VI ); % calculate the size of each component and calculate the offset YD = size (yi) of the file when the image is output per frame; % Y: UD = size (UI ); % u component location Vd = size (VI); % v component location frelem = yd (1) * yd (2) + ud (1) * ud (2) + VD (1) * vd (2); % video image display width if (nargin = 4) % calculated offset fseek (FID, startfrm * frelem, 0); end; % output Yi, UI, VI component information fwrite (fileid, yi', 'uint8'); fwrite (fileid, Ui', 'uint8 '); fwrite (fileid, VI ', 'uint8'); % close the file and remove the associated fclose (fileid );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.