本文介绍了如何在Matlab中使用for循环处理大视频,并且没有内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是Matlab处理新手,我想在for循环(或不用它)内读取和处理一个大视频(超过200k帧)。特别是,我想: 用VideoReader阅读视频, 细分视频进入每个1000帧的n个纪元, 处理1000个帧的每个纪元,读取:纪元的第一帧,跳过2,读取帧,跳过2,等等(例如i = 1:3:nFrames), 考虑到每个我需要将每个RGB帧转换为im2bw的新纪元 在转换之后,我需要考虑第一个视频帧(mov(1,1).cdata)以及在时间段内读取的每一帧进行corr22D互相关。 将结果从corr2存储到一个向量中。 总之,这是我需要做的。谢谢大家 这是我到目前为止对corr2 for frame_ind = 1:nFrames mov(frame_ind).cdata = im2bw(rgb2gray(read(xyloObj,frame_ind)),0.20); (frame_ind-1) R(frame_ind2)= corr2(mov(1, frame_ind2).cdata,MOV(1,frame_ind2 + 1).cdata); 结束 TF = isnan(R); g = sum(TF); f =(length(R)-g); (g〜=(​​length(R))) %%如果零件有错误 disp(''Part_1'有视频干扰/噪音/问题,请参阅Testresult.txt获取更多信息。 else %%如果零件没有错误 displ = strcat('Part_1没有视频干扰/噪音/问题。 end 解决方案这是我的版本: mov = VideoReader('movie.avi'); nFrames = mov.NumberOfFrames; len = 1000; %#epoch length step = 3; %#步长 %每个时代的指数指数= bsxfun(@plus,1:step:len,(0:ceil(nFrames / len-1))'* len ); %#' indices = num2cell(indices,2); indices {end}(indices {end}> nFrames)= []; %每个时代的循环 corr_coef = cell(size(indices)); for e = 1:numel(indices)%#读取历元中的第一幅图像 img1 = read(mov,indices {e}(1)); img1 = rgb2gray(img1);读取图像的剩余时间 corr_coef {e} = zeros(1,numel(indices {e}) - 1); for f = 2:numel(indices {e}) img2 = read(mov,indices {e}(f)); img2 = rgb2gray(img2); %#两个图像之间的计算corr2 corr_coef {e}(f-1)= corr2(img1,img2); end end 单元格数组 corr_coef 包含每个时期的相关系数,其中每个单元格包含一个 corr_coef {e}(i) of corr2 请注意,如果其中一个帧是恒定的(例如全黑),那么在第一帧和第(i + 1) ,二维相关系数仅仅是NaN(零点除以零在公式) I'm new to Matlab processing, and I would like to read and process a large video (more than 200k frames) inside a "for loop" (or without it). In particular, i would like to:read the video with VideoReader,subdivide the video into n-epoch of 1000 frames each ones,process every epoch of 1000 frames, reading: the first frame of the epoch, skip two, read the frame, skip two, and so on (for example i=1:3:nFrames),considering every epoch i need to convert every "RGB-frame" read into im2bwafter the conversion i need to make the "corr2" 2D cross-correlation considering the first video frame ("mov(1,1).cdata") and every frames read within the epoch,store the result from "corr2" into a vector. In summary, this is what i need to do. Thank You allThis is what I have so far, about "corr2":for frame_ind = 1 : nFrames mov(frame_ind).cdata = im2bw(rgb2gray(read(xyloObj,frame_ind)),0.20); end%% Corr2 to compare BW video framesfor frame_ind2 = 1:(frame_ind-1) R(frame_ind2)=corr2(mov(1,frame_ind2).cdata,mov(1,frame_ind2+1).cdata);end TF= isnan(R); g=sum(TF); f=(length(R)-g);if (g~=(length(R))) %%If Part has errors disp('"Part_1" has video interferences/noise/problems, see "Testresult.txt" for more information.'); else %%If Part has not errors displ=strcat('"Part_1" has not video interferences/noise/problems.'); end 解决方案 Here is my version:mov = VideoReader('movie.avi');nFrames = mov.NumberOfFrames;len = 1000; %# epoch lengthstep = 3; %# step size%# indices of each epochindices = bsxfun(@plus, 1:step:len, (0:ceil(nFrames/len-1))'*len); %#'indices = num2cell(indices,2);indices{end}(indices{end}>nFrames) = [];%# loop over each epochcorr_coef = cell(size(indices));for e=1:numel(indices) %# read first image in epoch img1 = read(mov, indices{e}(1)); img1 = rgb2gray(img1); %# instead of im2bw(img1, graythresh(img1)) %# read rest of images in epoch corr_coef{e} = zeros(1,numel(indices{e})-1); for f=2:numel(indices{e}) img2 = read(mov, indices{e}(f)); img2 = rgb2gray(img2); %# compute corr2 between the two images corr_coef{e}(f-1) = corr2(img1,img2); endendThe cell array corr_coef contains the correlation coefficients in each epoch, where each cell contains a vector corr_coef{e}(i) of corr2 between the first frame and the (i+1)-th frame.Note that if one of the frames is constant (all black for example), the 2D correlation coefficient is simply NaN (zero divided by zero in the formula‌​) 这篇关于如何在Matlab中使用for循环处理大视频,并且没有内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 12:58