Can anyone help me to debug this error

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 Comments

@Sangeetha Srinivas: what MATLAB version are you using?

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 9 May 2018
Edited: Guillaume on 9 May 2018
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 Comments

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!
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
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.
I have no idea what an absolute frame difference is. In any case, this is a topic for a new question.
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
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!