Archive

Archive for the ‘matlab Code’ Category

Yi Ma’s Robust Face Recognition using Sparse Representation; matlab code

June 29, 2012 3 comments

Following is a matlab version of simple Face Recognition algorithm given by Yi Ma.

</pre>
%build the dictionary from training
imgTrain = double(Tr_dataMatrix);
imgTest  = double(Tt_dataMatrix);

mx = mean(imgTrain,2);
D = imgTrain - repmat(mx, 1, size(imgTrain,2));

imgTest = imgTest - repmat(mx, 1, size(imgTest,2));

totalTest = size(imgTest,2);
lambda2 = 3;
addpath D:\mohsen\matlabCode\sparseCoding\elad\;
[U S V] = svd(D);
[egVal ind] = sort(diag(S), 'descend');
egV = U;
egV = egV(:, ind);

D = egV(:,1:200)'*D;
D = D./repmat(sqrt(sum(D.^2)),size(D,1),1);
imgTest = egV(:,1:200)'*imgTest;
 ci = zeros(size(D,2), size(imgTest,2));
ci = GetSparseFast(D,imgTest,0*zeros(size(D,2), size(imgTest,2)),500,1, 40);
testImgId = [];
rejectedI = [];
for i=1:totalTest
 recEr = 0;
 recMinid = -1;
 for p=1:totalPeople
   recErt = norm(imgTest(:,i)-D*(ci(:,i).*(imgTrainLabel'==p)));
   if recErt < recEr || p == 1
        recMinid = p; recEr = recErt;
   end
 end
 testImgId(i) = recMinid;
end

getPatch; matlab function to get a patch from a matrix

April 1, 2011 Leave a comment
function mat = getPatch(srcMat, rect)
%function getPatch(srcMat, rect)
%rect is [x y width height]

stX = max(rect(1),1);stY = max(rect(2),1);
endX = min(rect(1)+rect(3)-1, size(srcMat,1));endY = min(rect(2)+rect(4)-1, size(srcMat,2));
mat = srcMat(stY:endY, stX:endX);
Categories: code, matlab, matlab Code Tags: ,

VLFeat; library for SIFT and MSER

October 5, 2010 Leave a comment

While searching some code for Maximally Stable Extermal Regions, I came to this libraray.

http://www.vlfeat.org/index.html

Simple Selecting Points in 2 images using Matlab GUIDE

January 9, 2010 4 comments

Following is the program that lets you load two images and click points in both of them. It does not store the correspondence however because it stores points in the array so if you keep the order same in the both images it could be used for the point correspondence.
Use the guide command to make it them you can work on the each component by clicking on each component and selecting callback function.

function varargout = momentsGUI(varargin)
% MOMENTSGUI M-file for momentsGUI.fig
%      MOMENTSGUI, by itself, creates a new MOMENTSGUI or raises the existing
%      singleton*.
%
%      H = MOMENTSGUI returns the handle to a new MOMENTSGUI or the handle to
%      the existing singleton*.
%
%      MOMENTSGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOMENTSGUI.M with the given input arguments.
%
%      MOMENTSGUI('Property','Value',...) creates a new MOMENTSGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before momentsGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to momentsGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help momentsGUI

% Last Modified by GUIDE v2.5 09-Jan-2010 14:30:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @momentsGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @momentsGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before momentsGUI is made visible.
function momentsGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to momentsGUI (see VARARGIN)

% Choose default command line output for momentsGUI
handles.output   = hObject;
handles.selected = 0;
handles.point1 = [];
handles.point2 = [];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes momentsGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = momentsGUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.*');
img1 = imread([PathName FileName]);
%img1 = imread('carstick.bmp');
axes(handles.axes1);
imshow(img1);
handles.img1 = img1;
handles.size1 = size(img1);
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.*');
img2 = imread([PathName FileName]);
axes(handles.axes2);
imshow(img2);
handles.img2 = img2;
handles.size2 = size(img2);
% Update handles structure
guidata(hObject, handles);
%ginput(2)

% --- Executes on button press in selectImg1PtsPushButton.
function selectImg1PtsPushButton_Callback(hObject, eventdata, handles)
% hObject    handle to selectImg1PtsPushButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
pt = ginput(1);
pt=fliplr(pt);

if (sum(pt <=handles.size1(1:2))*sum(pt>[0 0]))
    handles.point1 = [handles.point1 ; pt];
    guidata(hObject, handles);
    handles.point1
    pt = round(pt);
    axes(handles.axes1);
    hold on;
    plot(pt(2), pt(1), 'r*');
end

% --- Executes on button press in selectPointImg2.
function selectPointImg2_Callback(hObject, eventdata, handles)
% hObject    handle to selectPointImg2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes2);
pt = ginput(1);
pt=fliplr(pt);

if (sum(pt <=handles.size2(1:2))*sum(pt>[0 0]))
    handles.point2 = [handles.point2 ; pt];
    guidata(hObject, handles);
    handles.point1
    pt = round(pt);
    axes(handles.axes2);
    hold on;
    plot(pt(2), pt(1), 'r*');
end

Working with 3D matrix in Matlab

November 16, 2009 2 comments

To visualize the 3D matrix just consider that them as stack of images or layers.

{There is one more very important way,  that is third dimension representing the feature vector’s length}

So a(:, :, i) = all elements in the layer i, so changing i will give you next image.

where a(r,c, i) will move in the image.

a = [];
a(:,1,:) = [111 112 113 114 ; 121 122 123 124 ; 131 132 133 134];
a(:,2,:) = [211 212 213 214 ; 221 222 223 224 ; 231 232 233 234];
a(:,3,:) = [311 312 113 114 ; 321 322 323 324 ; 331 332 333 334];
a(:,4,:) = [411 412 113 114 ; 421 422 423 424 ; 431 432 433 434];
a(:,5,:)= [511 512 513 514 ; 521 522 523 524 ; 531 532 533 534];

Will make the 3D matrix ‘a’

size(a)

ans =

     3     5     4

That is there are 4 images and each image is of 3 rows and 5 cols.

But let’s Say you want to represent 3rd dimension as the feature vector  so each a(r,c, : ) represents a feature vector.

Now let’s say you want to make a 3D matrix from one feature vector.


vt = squeeze([a( 2,1, : ) ] );
vt = vt(:)';
%//make it into 2 by 3 by length(feature) matrix
%//repmat will repeat this matrix and make a 6 row matrix
%//the reshape picks the elements from the 1st col, 1st row and start moving downward in the row and so on
%//therefore each time it will meet same element as it moves down the row and fills our first image
tempT = (reshape(repmat(vt, 2*3,1), 2, 3, length(vt)));
size(tempT)
ans =
     2     3     4

Running Variance

November 9, 2009 Leave a comment

Just in mid of the code wanted to find the Running Variance, just as habit typed and found not so good links.

1/N*[sum(i=1:N-1, Xi*Xi’) + X*X’] – (1/N^2)*[ sum(i=1:(N-1), Xi) + X][ sum(i=1:(N-1), Xi) + X]’

Matlab code for calculating the running Variance is as follow

%updating Mean

newMean = (newMean*(N-1) + X)/N;

sumOfSq = sumOfSq*(N-1)  + X*X';

covMat =  (1/N)*sumOfSQ - newMean*newMean'

Externally there are few links that are worth reading