Archive

Posts Tagged ‘matlab’

matlab and OpenCV development kit

January 26, 2013 Leave a comment

mexopencv comes from Kota Yamaguchi. It is a development kit which is collection of mex functions for bridging matlab and OpenCV. I have not used it till now, but it looks easy to use. Especially interesting to me is it provides the data conversion utility to transfer matlab data to OpenCV datatypes, so one can use them to write their own mex functions. 

So do use and provide feed back. 

 

http://www.cs.stonybrook.edu/~kyamagu/mexopencv/

Categories: Uncategorized Tags: ,

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

Matlab and Object orientation

March 3, 2012 Leave a comment

It’s high time to start implementing the object orientation in the matlab. If you are good in C/C++ you know how to develop classes and why to develop them.
Few caveats however, changes inside the method of any class member does not mean that object has been updated. Remember to return the object itself and re-assign it to iself. Unless you define it as subclass of ‘handle’. http://www.mathworks.com/help/techdoc/matlab_oop/brfylzt-1.html#brfylzt-2

 

When Pre-Allocating (i.e. array of classes )use the “empty”
have a look at http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html#brd4nrh

srun and matlab

November 19, 2011 Leave a comment

ok Mr. Cluster, here I come, I have unlocked the power to issue commands to your nodes for my maltab function ha ha ha ha!

so if you are in slurm
srun -n1 -o t.txt matlab -nodesktop -nosplash -nodisplay -r “temp4(29)” &

t.txt <— will get all the output

after -r you can give all the commands as the form of script i.e. ” ”

in above case temp4 was matlab file function, and 29 was input

& so that it can run in background

-n1 only one measly node 😦

But then you can run multiple of such commands and each one gets node 🙂

Don’t forget to open the matlabpool in the file you are running if you are using the parallel commands.

Categories: basic, code, matlab Tags: , , , , ,

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: ,

calculating distance matrix; Matlab

July 7, 2010 Leave a comment

A simple code to calculate the L2 distance matrix between any features.

UPDATE: found pdist and pdist2 functions in matlab, so no more need of following function

function D = calculateDistMatrix( points1, points2, f)
%D = calculateDistMatrix( points1, points2)
%points1: is m by n matrix, where n is number of points
if nargin ❤
    f = 0;
end
totalPoints1 = size(points1,2);
totalPoints2 = size(points2,2);
if f == 0
D= zeros(totalPoints1, totalPoints2);
else
D = sparse(totalPoints1, totalPoints2);
end
for i=1:totalPoints1
    D(i,:) = sqrt(sum((points2-repmat(points1(:,i), 1,totalPoints2)).^2));
end

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