Error using calcCovarMatrix in openCv - eclipse

I am trying to calculate the covariance matrix in my code but I am getting an error doing that. I have the array of mean values which I want to use. Here is my code
Mat Zt(Z);
Mat Zttranspose;
Mat covarZ=cvCreateMat(image->nChannels,image->nChannels,CV_32FC1);
Zttranspose=Zt.t();
Mat_<float> arraymean=(Mat_<float>(3,3)<< meanb, meang, meanr);
calcCovarMatrix(Zt,covarZ,arraymean,CV_COVAR_USE_AVG,CV_64F)
But I get the following error:
OpenCV Error: Assertion failed (((flags & CV_COVAR_ROWS) != 0) ^ ((flags & CV_COVAR_COLS) != 0)) in calcCovarMatrix, file /usr/local/src/OpenCV-2.3.0/modules/core/src/matmul.cpp, line 2127 terminate called after throwing an instance of 'cv::Exception'

Mat covarZ=cvCreateMat(..); I think you have mix out with C and C++ API.
You might want to have a look at this link http://pastebin.com/cWQi4ngv.
I have tried and it works.

You're creating the matrix with CV_32FC1 and then calling calcCovarMatrix with CV_64F - you need to make them consistent for starters.

This will solve your problem
-
calcCovarMatrix(Zt,covarZ,arraymean,CV_COVAR_USE_AVG | CV_COVAR_ROWS,CV_64F)

Related

runtime error in bundle adjustment example - matlab

I asked it in MATLAB forums but didnt get a response. Hoping someone can answer the question here:
I tried using Bundle Adjustment example at https://www.mathworks.com/help/vision/ref/bundleadjustment.html#inputarg_xyzPoints
However, I get an error: "Error using getPrmDflt (line 47) odd number of parameters in prm Error in bundleAdjustment (line 49) getPrmDflt( varargin,{ 'KMask', [], 'nItr', 500, ..."
at this line: [xyzRefinedPoints,refinedPoses] = bundleAdjustment(xyzPoints,pointTracks,cameraPoses,cameraParams);
After looking more into it the input to getPrmDflt is totally different that what the function expects. Is there some bug or wrong function call in bundle adjustment code?
it was an error from my side. I had downloaded Vincent's MATLAB toolbox a couple of years ago for use and it had a bundleAdjustment function call that overrode the MATLAB function.

MATLAB : Error using histc. First Input must be a real non-sparse numeric array

I've got the error of "Error using histc. First Input must be a real non-sparse numeric array" from the following codes.
N=10^4;
d=rand(1,N)>0.5;
symbols=unique(d);
probs = histc(d,symbols)./numel(d);
P/s: I try to generate using randsrc before. It did worked.But, I'm hoping not to use randsrc because it will affect my code later on. Any ideas on this would be appreciated.
Thanks.
Here is the working code
N=10^4;
d=double(rand(1,N)>0.5);
symbols=unique(d);
probs = histc(d,symbols)./numel(d);

test if netcdf file is valid without crashing

I am reading multiple netcdf files using the ncread function in matlab.
For a reason unknown to me, some files (described by FILEPATH in the following) are not properly read and ncread crashes, producing the error message:
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open FILEPATH for reading.
Error in internal.matlab.imagesci.nc (line 121) this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);
Error in MY_FUNCTION (line 102)
Lon = nanmean(ncread(FILEPATH,'Lon'));
If you know of a method to test netcdf files without crashing, or if you understand what produces this error, any insight would be appreciated.
The standard way is to wrap the possibly-failing statement in a try/catch statement to intercept the thrown exception before interrupting the function execution, like this:
function [out1, out2, ...] = MY_FUNCTION(arg1, arg2, ...)
%//Initial code
try
Lon_data = ncread(FILEPATH,'Lon');
catch ME
warning('MY_FUNCTION:ncread', 'Could not load because <<%s>>',ME.message);
%//Do something to recover from error
%//Return from function if recover not possible
end;
Lon = nanmean(Lon_data);
%//Rest of the code
end
Please note that ... in the function signature above is not valid MATLAB syntax, but rather something that says "here are some inputs and outputs that I don't know how they are declared"; please substitute with your proper in/out declaration.

Matlab: How to catch warning

I am running some data processing work in MATLAB and solver uses BACKSLASH operator. Sometimes, I get warning like this:
Warning: Rank deficient, rank = 1390, tol = 1.335195e-010.
Warning: Rank deficient, rank = 1386, tol = 1.333217e-010.
I would like to catch those warnings.
I am trying to convert warning to error and then catch it as described here under title “Trapping warnings”:
http://undocumentedmatlab.com/blog/trapping-warnings-efficiently
In the example, following string has been used to convert warning to error:
s = warning('error', 'MATLAB:DELETE:Permission');
However, I am not sure what string to use for my case. I tried using
s = warning('error', 'Warning: Rank deficient’);
But, it did not work.
Any help would be appreciated.
Regards,
DK
You need to specify the warning identifier, not the warning text. You can find the identifier using the two-output form of lastwarn:
[msgstr, msgid] = lastwarn
In your case, I think the identifier you want is 'MATLAB:rankDeficientMatrix'.
You could try to use lastwarn as an alternative. After your division, call it and compare it with strcmp to the usual warning message, and if its the one you wnat you could manually throw the error you want with error.
As you suggested: you can reset lastwarn throwing an empty warning warning('')

Where is my mistake and how can i correct it?

I'm using this code for parameter estimation. it gives me an error in line given below. how can i correct this. thanks in advance.
while dcnorm>1E-6 & iter<10
f=a*(b.^(c.^t))-y;
Ji1=b.^(c.^t);
Ji2=a*(b^(c.^(t-1)))*(c.^t); %ERROR LINE
Ji3=a*(b^(c.^t))*ln(b)*t*(c.^(t-1));
J=[Ji1 Ji2 Ji3];
dc=-J\f; tahmin=tahmin+dc;
dcnorm=norm(dc); iter=iter+1;
a=tahmin(1); b=tahmin(2); c=tahmin(3);
D=[iter a b c norm(f) norm(dc)]
end
try (explicitly use element-wise operations throughout the expression):
Ji2=a.*(b.^(c.^(t-1))).*(c.^t);
My guess you'll need to modify the next line as well.