V = 'army.avi';
xy1oObj = VideoReader (V); %Using video reader reading video
% Extracting frames
T = xyloObj.NumberOfframes % Calculate number of frames
for g = 1: T
p = read(xyloObj, g); %retrieve data from video
if (g ~=xyloObj.NumberOfFrames)
J = read (xyloObj, g + 1);
th = difference (p,J); %calculate histogram diff btwn two frames
X(g) =th;
end
end
% caculating mean and sd nd extracting frames
mean = mean2 (X)
std = std2 (X)
threshold = std + mean * 4
for g = 1: T
p = read (xyloObj, g);
if (g ~= xyloObj.NumberOfFrames)
J = read (xyloObj, g + 1);
th = difference (p,j);
if (th> mean) %greater than threshold select key frame
filename = fullfile ('C: \ Keyframes', sprintf ('frame_% frame20.JPG', g)); %writing keyframes
imwrite (J, filename);
end
end
end
function[ r ] = difference (f1,f2)
k = rgb2gray (f1);%convert into grayscale
l = rgb2gray (f2);
f11 = imhist (k); %histogram of data
f12 = imhiist (l);
diffe = imabsdiff (f11, f12); %absolute different of two images
r = sum(diffe);
end
error is:
Error: File: differ.m Line: 28 Column: 1
Function definitions are not permitted in this context.
Undefined variable "xyloObj" or function "xyloObj.NumberOfframes".
Error in differ (line 4)
T = xyloObj.NumberOfframes % Calculate number of frames

2 commentaires

Stephen23
Stephen23 le 9 Mai 2018
@Sangeetha Srinivas: what MATLAB version are you using?
Sangeetha Srinivas
Sangeetha Srinivas le 9 Mai 2018
matlab 2014 version

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 9 Mai 2018
Modifié(e) : Guillaume le 9 Mai 2018

0 votes

The ability to define functions in scripts was introduced in R2016b. In earlier version, your function must be in its own file. Therefore you must move your difference function into its own difference.m file.

8 commentaires

Sangeetha Srinivas
Sangeetha Srinivas le 9 Mai 2018
If I do that i'm getting another error as Undefined variable "xyloObj" or function "xyloObj.NumberOfframes".
Error in differ (line 4) T = xyloObj.NumberOfframes % Calculate number of frames
Try
xyloObj = VideoReader (V);
xy1oObj = VideoReader (V)
The character between xy and oObj is the number 1.
T = xyloObj.NumberOfframe
The character between xy and oObj is the letter el.
You need to be consistent!
Sangeetha Srinivas
Sangeetha Srinivas le 9 Mai 2018
Thank you sir.But im still getting errors please help me out to solve it. Attempt to execute SCRIPT rgb2gray as a function: C:\Users\roshini\Documents\MATLAB\rgb2gray.m
Error in difference (line 2) k = rgb2gray (f1);%convert into grayscale
Error in differ (line 9) th = difference (p,J); %calculate histogram diff btwn two frames
Sangeetha Srinivas
Sangeetha Srinivas le 9 Mai 2018
I have extracted frames from a video now I need to find the absolute frame difference between two frames.Sir, can u please help me out.
Guillaume
Guillaume le 9 Mai 2018
I have no idea what an absolute frame difference is. In any case, this is a topic for a new question.
Sangeetha Srinivas
Sangeetha Srinivas le 9 Mai 2018
It is nothing but finding out difference between two images.
now can u please help me out to solve this problem Attempt to execute SCRIPT rgb2gray as a function: C:\Users\roshini\Documents\MATLAB\rgb2gray.m
Error in difference (line 2) k = rgb2gray (f1);%convert into grayscale
Error in differ (line 9) th = difference (p,J); %calculate histogram diff btwn two frames
Guillaume
Guillaume le 9 Mai 2018
Modifié(e) : Guillaume le 9 Mai 2018
It is nothing but finding out difference between two images.
Depending on what difference mean, that can be a huge topic.
With regards to the error, it looks like you named your script rgb2gray.m which of course prevent you from using the function rgb2gray. Don't name the script the same as functions you use.
Note that if that C:\Users\roshini\Documents\MATLAB\rgb2gray.m file is actually the difference function I told you to create, then its name must be C:\Users\roshini\Documents\MATLAB\difference.m and its content:
function [r] = difference (f1,f2)
k = rgb2gray (f1);%convert into grayscale
l = rgb2gray (f2);
f11 = imhist (k); %histogram of data
f12 = imhiist (l);
diffe = imabsdiff (f11, f12); %absolute different of two images
r = sum(diffe);
end

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by