I executed the following command(Error using / Matrix dimensions must agree. Error in linspace (line 31) y = d1 + (0:n1).*(d2 - d1)/n1; Error in embedImg2Video>embedImg2VideoDefs (line 314) varargout{iArg}=transpose(linspace(inp(
7 views (last 30 days)
Show older comments
function outVideoFile=embedImg2Video(inVideo, varargin) isSaveAllFrames=false; isEncodeWithAvifile=false; iStart=1; iEnd=[]; framesList=[]; defaultCompression='NONE'; % LAGS is much better option...
% Image embedding default parameters imgMask=[]; embedImg=[]; objPath=[]; objAngle=0; objScale=1; objOpacity=1;
% automatically get all input pairs (name, value) and store in local vairables for iArg=1:2:length(varargin) % eval([varargin{iArg},'=varargin{iArg+1};']); % TODO get read of EVAL. assignin_value(varargin{iArg},varargin{iArg+1}); end
if exist('inVideo', 'var')~=1 exist(inVideo, 'file')~=2 videoFormats= VideoReader.getFileFormats(); videosFilesExtList={videoFormats.Extension}; videosFilesFilter=sprintf('*.%s;',videosFilesExtList{:});
FilterSpec={videosFilesFilter, sprintf('Video Files (%s)', videosFilesFilter);...
'*.*', 'All Files'};
dialogTitle='Select input video file.';
[fileName, pathName, ~] = uigetfile(FilterSpec, dialogTitle, pwd);
inVideo=strcat(pathName, fileName);
end
[videoPath, videoName, videoExt] = fileparts(inVideo);
if isempty(videoExt) % if extention missing, find first file with such extension tmpInVideo=ls(strcat(videoPath, filesep, videoName, '.*')); [~, ~, videoExt] = fileparts(tmpInVideo(1, :)); inVideo=strcat(videoPath, filesep, videoName, videoExt); end
assert(exist(inVideo, 'file')==2, 'No such video file is found.');
ObjInVideo = VideoReader([videoPath, filesep, videoName, videoExt]); % in case videoPath was missing- get it from ObjInVideo fields inVideo=strcat(ObjInVideo.Path, filesep, ObjInVideo.Name); [videoPath, videoName, videoExt] = fileparts(inVideo);
if isempty(iEnd) % default iEnd is the video last frame iEnd=ObjInVideo.NumberOfFrames; end
if isempty(framesList) % default list is from iStart to iEnd framesList=iStart:iEnd; end
fprintf('\n Image\\Object embedding into %s video started: %s \n',... [videoName, videoExt], datestr(now,'dd-mmm-yyyy HH:MM:SS'));
if exist('outVideoFile', 'var')~=1 % generate outVideoFile name if user didn't supply one outVideoFile=['embeddedImg_', videoName,videoExt]; outVideoFile=[videoPath, filesep, outVideoFile]; else [outVideoPath, outVideoName, outVideoExt] = fileparts(outVideoFile); if isempty(outVideoPath) % if no path was mentioned- use inputs path outVideoFile=[videoPath, filesep, outVideoName, outVideoExt]; end end
% inherit compression from parent file % mmfileinfo uses mmreader, which supposed to be removed starting R2011b inVideoFileInfo = mmfileinfo(inVideo); compression=inVideoFileInfo.Video.Format; % unfortunately inVideoFileInfo.Video.Format names differ from videoWrCompMethods
profiles = VideoWriter.getProfiles(); videoWrCompMethods={profiles.Name};
isVideoWriterCompression=any(~cellfun(@isempty,strfind(videoWrCompMethods, compression))); [~, ~, outVideoExt] = fileparts(inVideo); if strcmpi(outVideoExt,'.AVI') && ( ~isVideoWriterCompression ); % if an a file with AVI extention, and codec used in not supported by VideoWriter, isEncodeWithAvifile=true; % enable encoding with avifile, instead of VideoWriter % turn encoding warning off, when encoder not supported by Matlab (like LAGS) is used warning('off', 'MATLAB:aviset:compressionUnsupported'); if any(strcmpi(compression,{'RGB 24', 'RGB24', 'MJPG'})) compression=defaultCompression; end end
if isEncodeWithAvifile ObjOutVideo = avifile(outVideoFile, 'fps', ObjInVideo.FrameRate,... 'compression', compression); outVideoFile=ObjOutVideo.Filename; else % if compression is supported by VideoWriter, use it if isVideoWriterCompression ObjOutVideo = VideoWriter(outVideoFile, compression); else ObjOutVideo = VideoWriter(outVideoFile, 'Archival'); % default compression end ObjOutVideo.FrameRate=ObjInVideo.FrameRate; outVideoFile=strcat(ObjOutVideo.Path, filesep, ObjOutVideo.Filename); open(ObjOutVideo); end
currFrame=read(ObjInVideo, 1); frameSize=size(currFrame); if length(frameSize)==2 frameSize(3)=1; end
if isempty(embedImg) % Default object image- black sqaure embedImg=zeros( floor(frameSize(1)/5), floor(frameSize(2)/2), frameSize(3) ); end
if isempty(imgMask) % Default mask- all object is embedded imgMask=true(size(embedImg,1), size(embedImg,2)); end
nFrames=length(framesList); nVideoFrames=ObjInVideo.NumberOfFrames;
if isempty(objPath) % Default path- video frame center objPath=cat(2, frameSize(1)/2, frameSize(2)/2); end [objPath, objAngle, objScale, objOpacity]=embedImg2VideoDefs(framesList,... objPath, objAngle, objScale, objOpacity);
h=waitbar(0, 'Please wait, image embedding into video is in progress:',... 'Name', 'embedImg2Video'); hTicApply2VideoFrames=tic;
try for iFrame = 1:nVideoFrames % loop through all frames currFrame=read(ObjInVideo, iFrame); iFrameFromList=find(iFrame==framesList); if isempty(iFrameFromList) if isSaveAllFrames embededFrame=currFrame; else continue; end else if objAngle(iFrameFromList)~=0 currObj=imrotate(embedImg, objAngle(iFrameFromList)); currMask=imrotate(imgMask, objAngle(iFrameFromList)); else currObj=embedImg; currMask=imgMask; end
if objScale(iFrameFromList)~=1
currObj=imresize(currObj, objScale(iFrameFromList));
currMask=imresize(currMask, objScale(iFrameFromList));
end
% embed object to curent video frame
% mixedImg= imagesMixture(mainImg, blendImg, coords, 'coordsMode', coordsMode, 'opacity', opacity, 'blendMask', blendMask);
embededFrame=imagesMixture(currFrame, currObj, objPath(iFrameFromList, :),...
'coordsMode', 'Center', 'opacity', objOpacity(iFrameFromList), 'blendMask', currMask);
end % if any(iFrame==framesList)
% add updated frame to outVideo
if isEncodeWithAvifile
ObjOutVideo = addframe(ObjOutVideo, embededFrame);
else
writeVideo(ObjOutVideo, embededFrame);
end
% Present waitbar- a bar with progress, time passed and time remaining
waitbarProgress=(iFrame)/nVideoFrames;
stoperReading=toc(hTicApply2VideoFrames)*1e-5;
waitbar(waitbarProgress, h ,...
sprintf('Time passed %s [H:Min:Sec.mSec].\nTime remaining %s [H:Min:Sec.mSec].',...
datestr(stoperReading, 'HH:MM:SS.FFF'),...
datestr(((1-waitbarProgress)/waitbarProgress*stoperReading), 'HH:MM:SS.FFF')));
end
close(h);
if isEncodeWithAvifile
ObjOutVideo = close(ObjOutVideo);
else
close(ObjOutVideo);
end
catch exception % In case of error remeber to close open files- or esle you'll get another error nvideoExt time ;)
close(h);
if isEncodeWithAvifile
ObjOutVideo = close(ObjOutVideo);
else
close(ObjOutVideo);
end
rethrow(exception);
end
function varargout=embedImg2VideoDefs(iFrames, varargin) nOptArgin=length(varargin);
for iArg=1:nOptArgin inp=varargin{iArg}; for iCol=1:size(inp, 2) isNaNArr=isnan( inp(:, iCol) ); if any(isNaNArr) inp(isNaNArr, iCol) = interp1q( iFrames(~isNaNArr), inp(~isNaNArr, iCol),... iFrames(isNaNArr) ); varargout{iArg}=inp; else varargout{iArg}=transpose(linspace(inp(1, iCol), inp(end, iCol), iFrames)); end end end
0 Comments
Answers (0)
See Also
Categories
Find more on External Language Interfaces in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!