Undefined Variable f as i am trying to extract the features
1 次查看(过去 30 天)
显示 更早的评论
clc
clear
close all
format compact
face_folder='pos/';
folder_content=dir([face_folder,'*']);
face=size (folder_content,1);
for k=3:face
k1=k-2
string=[face_folder,folder_content(k,1).name];
image=imread(string);
f(k1,:)=cohog(image);
end
face=face-2;
x=[f(1:face,:);]';
y=[ones(face,1);]';
save testinria x y face
This is my code through which i want to extract the features, but while executing the program it shows error as
Undefined variable f.
I have defined f within the for loop, then why it's showing this error, please help me to rectify the error.
Error in feature1 (line 15)
x=[f(1:face,:)]';
1 个评论
Image Analyst
2012-10-11
编辑:Image Analyst
2012-10-11
Are you absolutely 100% sure it entered the loop?
采纳的回答
Walter Roberson
2012-10-11
Because you do not initialize f, you would get that error if dir() returned no more than 2 results.
Note: you should not be assuming that the first two entries should be discarded. dir() does not sort the results, and just returns whatever order is returned by the operating system. In turn, the operating system does not sort the results and returns whatever order is used by the file system. Different file system types on the same machine may return different orders. For example some filesystem types sort in strict ASCII order and so may sort a filename that begins with a space before they sort the '.' directory.
Also, using a variable named "image" will conflict with the image() function.
Adjusted code:
folder_content = dir([face_folder,'*']);
folder_content([folder_content.isdir]) = [];
face = length(folder_content);
f = [];
for k = 1 : face
string = fullfile(face_folder,folder_content(k).name);
thisimage = imread(string);
f(k, :) = cohog(thisimage);
end
0 个评论
更多回答(2 个)
Image Analyst
2012-10-11
You need to preallocate f before the loop - at least one row. In your line it doesn't know how many columns there are and it needs to if you're going to assign it like that.
3 个评论
Image Analyst
2012-10-11
So, assuming it entered the loop like he says it definitely did, why didn't it create the array f? Instead of saying it was not defined yet, why didn't it just define/create it? I couldn't run the code because I don't have cohog.
My guess would be that it never did enter the loop so by the time it gets to x=[f..... there is no f yet. But he says it was defined in the loop. I wonder if Muzafar knows how to use the debugger.
By the way Muzafar, don't use "image" as the name of your variable - that will blow away the "image()" function that is built in to MATLAB.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!