Archive

Archive for the ‘matlab’ Category

parsing jason nested array using jsoncpp

May 22, 2013 Leave a comment

so first experiments with the json and json cpp.

the json string is as follow

{“id”: 1,”tags”: [ [800, 99.7], [], [76] ],”stock”: {“warehouse”: 300,”retail”: 20}}

Wanted to read the nested arrays of “tags” as you can see such an array could be e.g. cell datatype in the matlab or could be represented as the array of vectors

int main (){</pre>
unsigned int i,j;
char fileName[] ="abc.json";

ifstream infile;
infile.exceptions (ios::failbit|ios::badbit);
try
{
infile.open (fileName);
}
catch (ifstream::failure& e)
{
cerr<<"\n Exception opening file "<<fileName<<": "<<e.what ();
return 0;
}

string input;
Json::Reader reader;
Json::Value root;
bool parsingSuccessful;

try
{
while (getline (infile,input,'\n'))
{
if (false == (parsingSuccessful = reader.parse (input,root)))
{
cerr<<"\nFailed to parse configuration:"
<<reader.getFormatedErrorMessages ();
return 0;
}
else
{

int verbName = root.get ("id","").asInt ();
Json::Value data = root.get ("tags",0);
cout << "size is " << data.size() <<endl;

//vector<float> freq (mWidth * mHeight);
for (int i = 0; i < data.size(); i++){
int sizeint = data[i].size();
cout <<endl <<"---------------" <<endl;
cout << "Size of individual is " <<sizeint << endl;
Json::Value dat1 = data[i];

for(int j=0; j< dat1.size(); j++){
cout << dat1[j].asDouble() << " ,";
}

}
}
}
}
catch (ifstream::failure& e)
{

}

return 0;
}
<pre>
Advertisements
Categories: C++, code, jsoncpp, matlab 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

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

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

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