Archive

Posts Tagged ‘code’

jsoncpp writing back the structures and arrays

May 24, 2013 Leave a comment

Well successfully read the json file of the matlab mat structure. Lots of things to care of, not as simple as it looks.

Now writing back, that is taking the C++ class, structure and basic data types and outputting a json structure. Since I have already written the reader to the writer should match the reader’s expectation.

This one is quite decent information and advice on how to design class for such activity. http://www.danielsoltyka.com/programming/2011/04/15/simple-class-serialization-with-jsoncpp/

 

Will follow the writing part now and later will try to implement the reader in much more structured way.

Categories: C++, code, jsoncpp Tags: , , , ,

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>
Categories: C++, code, jsoncpp, matlab Tags: , , , , , ,

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

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

Recovering 2D motion between the frames of video

June 28, 2010 Leave a comment

Motion2D is C/C++ library by Irsia/INRIA to estimate 2D parametric motion in between the frames of video.  Works very good. Have a good example code.

Only headache is I am having problem in compiling with the VC. They don’t have any dsp file for compilation of code in Windows, so don’t know which libaries to create. Have to use cygwin.

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