以下是我前段时间编写的用于处理视频文件的代码。在执行此文件之前,将 ROI1.m 文件保存在路径中。
%frame by frame processing of video files
clear all;
close all;
clc;
mov=VideoReader('C:\Users\Syd_R\OneDrive\Desktop\entrap\holo_bright_10_MMStack_Pos0.ome.avi');
vidFrames=read(mov);
nFrames=mov.NumberOfFrames;
A=vidFrames(:,:,1);
for fr=1:nFrames
set(0,'DefaultFigureVisible','off')
elseX=vidFrames(:,:,fr);
if exist('position')==0
ROI1
else
imshow(elseX)
I2 = imcrop(elseX,[position(:,1) position(:,2) position(:,3) position(:,4)]);
end
mycell_h(fr)={I2};
end
close all;
set(0,'DefaultFigureVisible','on')
% This file should be saved (as ROI1.m) in the same path as this file will be called while executing the main file
% ROI
if exist('A')==1;
figure, imshow(A);
h = imrect(gca,[10 10 512 512]);
position = wait(h); % returns coordinates in "position" when user doubleclicks on rectangle
figure, imshow(A)
I2=imcrop(A,position);
phROI2=I2;
figure(11);
imshow(phROI2);
imwrite(phROI2, 'roi', 'tiff')
end;
裁剪后的帧将保存在单元格 [mycell_h] 中。查看裁剪后的帧,例如:
imshow(mycell_h{1,1})