why I get the error: Undefined function or variable 'shapes' - matlab

I'm working in matlab 2016.
I have a program code. It needs to work correctly.
Here is the program:
clear all;
addpath('..');
training_files = dir('cootes/*.bmp.mat');
for i=1:numel(training_files)
load(sprintf('cootes/%s', training_files(i).name));
app = imread(sprintf('cootes/%s', training_files(i).name(1:end-4)));
% Map RGB colors to [0,1]
appearances(:,:,:,i) = double(app) ./ 255;
shapes(:,:,i) = xy2ij(annotations, size(app,1));
end
load('cootes/triangulation.mat');
test_img = 2;
one_out = [1:test_img-1, test_img+1:size(shapes,3)];
AAM = build_model_2d(shapes(:,:,one_out), appearances(:,:,:,one_out), 'triangulation', triangulation);
fprintf('\n******************************************************* 2D FITTING *******************************************************\n\n');
disp 'Figure 1: leave-one-out fitting result (red mesh) using as intialization a random shape from the training set (blue mesh).'
disp 'Figure 2: reconstructed appearance.'
disp 'Usage: Hit a random key to use a different initialization shape. Use CTRL+C to quit.'
fprintf('\n');
while 1
init_shape = one_out(round(rand()*(numel(one_out) - 1) + 1));
try
[ fitted_shape fitted_app ] = fit_2d(AAM, shapes(:,:,init_shape) + repmat([-5 -5], [size(shapes, 1) 1 1]), appearances(:,:,:,test_img), 20);
figure(1)
imshow(appearances(:,:,:,test_img));
hold on;
triplot(AAM.shape_mesh, shapes(:,2,init_shape), shapes(:,1,init_shape), 'b');
triplot(AAM.shape_mesh, fitted_shape(:,2), fitted_shape(:,1), 'r');
hold off;
figure(2)
imshow(fitted_app);
pause;
catch ME
fprintf('Fitting diverged: %s\n', ME.message);
end
end
After starting the program appears the following error:
Undefined function or variable 'shapes'.
Error in annotate_test (line 29)
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);
Сan anyone explain why the variable 'shapes' is not defined. Although clearly spelled out:
shapes(:,:,i) = xy2ij(annotations, size(app,1));
Please help, I'm newby and don't understand where can be the error.

training_files = dir('cootes/*.bmp.mat'); %error is here
try this.
training_files = dir(fullfile(mypath,'*.bmp.mat')); % should work if you have .bmp.mat files in that path

Related

Dimensions are not Ok in matlab

When I take image from img from commands =
imread('C:\Users\Administrator\Desktop\Detect\F04U2MPIXQG15WQS.LARGE.jpg');
figure,imshow(img)
It works fine but when I use
[filename, pathname] = ...
uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'Select Image File');
img=strcat(pathname,filename);
error shown is Error in skinDetect2Func (line 8)
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4; Error in
bwFingers1 (line 39)
out=skinDetect2Func(img);
Anyone can help me out I am just stuck in here. :(
Here is the complete code:
function out=skinDetect2func(img)
imshow(img);
sz=size(img); r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
yuv(:,:,u) = img(:,:,r)-img(:,:,g);
yuv(:,:,v)=img(:,:,b)-img(:,:,g);
region = (yuv(:,:,u)>20 & yuv(:,:,v)<74) .* 255;
toc;
out=region;
%filtering
out=im2bw(out); out=bwareaopen(out,100);
out=imdilate(out,strel('diamond',4));
%retain largest only
res=out;
cc=bwconncomp(res);
arr=(cellfun('length',cc.PixelIdxList));
newLabel=res;
if ~isempty(round(arr))
msz=0;
for i=1:length(arr)
if msz<arr(i:i)
msz=arr(i:i);
index=i;
end
end
labels=labelmatrix(cc);
newLabel=(labels==index);
out=newLabel;
end
out=imfill(out,'holes');
end***
ehm,
This is a string:
img=strcat(pathname,filename); % STRing conCATenation
You still need to imread...
The correct code would be
imgpath=strcat(pathname,filename);
img=imread(imgpath);

In Matlab I keep getting undefined variable or function error despite clearly defining the variable

I have this piece of code in Matlab which should take an Airfoil profile and increase the number of points so that when I plot the profile in another programme I will get a smoother curve.
clear
%reading an external data file
fid = fopen('NACA0015.txt');
a = fscanf(fid,'%g %g',[2 inf]); % It has two rows now.
a = a'; % matrix transpose
n = input('200') %e.g., n=35
for i=1:n
for j=1:2
fprintf('%12.7f',a(i,j)); %a(i,1) is first column, a(i,2) is 2nd col
end
fprintf('\n');
end
fclose(fid);
for i=1:n
x(i)=a(i,1); %x , y vectors
y(i)=a(i,2);
end
% use spline to create more points
xx=0:0.01:1 % e.g., step =0.01 (number of points = (1-0)/0.01=100)
yy = spline(x,y,xx); % xx and yy are new generated values of Naca0012
fprintf('\n print spline values \n');
plot(xx,yy,'ro')
hold on
plot(x,y,'*')
When I run this I get the error
Undefined function or variable 'x'.
Error in reading_external_data_and_spline (line 26)
yy = spline(x,y,xx); % xx and yy are new generated values of Naca0012
I am at a complete loss as to why this is not working when the x variable is clearly defined in the code, please could someone help me with this
It's how you're using input. The argument in input isn't the default value, it's the prompt text. If you type the command into the console and hit enter, you get this:
>> n = input('200')
200
n =
[]
>>
Input doesn't accept a default. If you really want to have an interactive prompt with a default answer, you want inputdlg:
answer = inputdlg('Enter a number of lines to parse', 'n', 1, '200');
n = str2double(answer);
note that inputdlg returns text always, so you need to convert to a number.

Undefined function or variable 'resubPredict'. Why?

I have MATLAB R2015b and I am working on a Pattern Recognition project. I have loaded fisher iris data set.
I have written the code below for k-NN classification:
rng default;
fold_number = 5;
indices = crossvalind('Kfold',species, fold_number);
val = 1:2:5; % for these small k values there will not be an important difference
% regarding the cp ErrorRates. The difference is going to be
% observable for val = 1:2:100, for example!!! But the
% exercise asks only for k = 1,3,5.
err_arr = [];
for k=val
cp = classperf(species); % Reinitialize the cp-structure!
for i = 1:fold_number
test = (indices == i);
train = ~test;
class = knnclassify(meas(test,:),meas(train,:),species(train), k);
%class = knnclassify(meas(test,2),meas(train,2),species(train), k); % To experiment only with the 2nd feature
classperf(cp,class,test);
end
err_arr = [err_arr; cp.ErrorRate];
fprintf('The k-NN classification error rate for k = %d is: %f\n', k,cp.ErrorRate);
end
fprintf('\n The error array is: \n');
disp(err_arr);
fprintf('Program paused. Press enter to continue.\n');
pause
After that, I would like to plot the confusion matrix. So, I need this line of code:
[C,order] = confusionmat(species, predicted_species);
So, I have to find the predicted_species matrix. I have thought to write the below line of code about that:
predicted_species = resubPredict(class);
Here is the moment I am getting the on the title mentioned error and I do not know why, while resubPredict is a function supported by my MATLAB version.
Could anyone help me solve this problem?
Take a look at the top of the documentation for resubPredict, there you will notice the line
Class ClassificationKNN
Meaning that the function can only be used with inputs of that type. In your case you are passing doubles.
You have to switch to the new interface, fitcknn instead of knnclassify

For Loop and indexing

Following is my code :
function marks(my_numbers)
handle = zeros(5,1)
x = 10 ;
y = 10:20:100 ;
for i = 1
for j = 1:5 ;
handle(j,i) = rectangle('position',[x(i),y(j),20 10],'facecolor','r')
end
end
end
now lets say input argument my_numbers = 2
so i have written the code :
set(handle(j(my_numbers),1),'facecolor','g')
With this command, rectangle with lower left corner at (30,10) should have turned green. But MATLAB gives an error of index exceeds matrix dimensions
This is more an illustrated comment than an answer, but as #hagubear mentioned your i index is pointless so you could remove it altogether.
Using set(handle(my_numbers,1),'facecolor','g') will remove the error, because you were trying to access handles(j(2),1) and that was not possible because j is a scalar.
Anyhow using this line after your plot works fine:
set(handle(my_numbers,1),'facecolor','g')
According to your comment below, here is a way to call the function multiple times and add green rectangles as you go along. There are 2 files for the purpose of demonstration, the function per se and a script to call the function multiple times and generate an animated gif:
1) The function:
function marks(my_numbers)
%// Get green and red rectangles to access their properties.
GreenRect = findobj('Type','rectangle','FaceColor','g');
RedRect = findobj('Type','rectangle');
%// If 1st call to the function, create your plot
if isempty(RedRect)
handle = zeros(5,1);
x = 10 ;
y = 10:20:100 ;
for j = 1:5 ;
handle(j) = rectangle('position',[x,y(j),20 10],'facecolor','r');
end
set(handle(my_numbers,1),'facecolor','g')
%// If not 1st call, fetch existing green rectangles and color them green. Then color the appropriate rectangle given by my_numbers.
else
RedRect = flipud(RedRect); %// Flip them to maintain correct order
if numel(GreenRect) > 0
hold on
for k = numel(GreenRect)
set(GreenRect(k),'facecolor','g')
set(RedRect(my_numbers,1),'facecolor','g')
end
end
end
2) The script:
clear
clc
%// Shuffle order for appearance of green rectangles.
iter = randperm(5);
filename = 'MyGifFile.gif';
for k = iter
marks(k)
pause(1)
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if k == iter(1)
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
Here is the animated gif of the output:

How can I use Voila Jones algorithm in MATLAB 64 bit?

How can I use Voila Jones algorithm in MATLAB 64 bit?
close all
clear all
clc
aa = imread('p2.jpg');
ss = rgb2gray(aa);
pos = fdlibmex(ss);
strt(1,1)=pos(1)-(pos(3)/2);
strt(1,2)=pos(2)-(pos(3)/2);
face(1) = strt(1,1);
face(2) = strt(1,2);
face(3) = pos(3);
face(4) = pos(3);
Rectangle = [face(1) face(2); face(1)+ face(3) face(2); face(1) + face(3) face(2) + face(4); face(1) face(2) + face(4); face(1) face(2)];
figure(1);
imshow (aa);
truesize;
hold on;
plot (Rectangle(:,1), Rectangle(:,2), 'g');
hold off;
Undefined function 'fdlibmex' for input arguments of type 'uint8'.
Error in main (line 7)
pos = fdlibmex(ss);
Try this FEX file instead ....
You should put this function 'fdlibmex' in your working directory besides the main script. Or you can just use addpath command to add the path of this function to your matlab workspace.
addpath('folder/file.m')
Apparently you can give a JPG image as an input, so
I = imread('my_image.jpg');
pos = fdlibmex(I);