OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 15 Oct 2019 20:26:26 -0500Unclear how calibrateCamera estimates stdDeviations (perhaps wrong)http://answers.opencv.org/question/219805/unclear-how-calibratecamera-estimates-stddeviations-perhaps-wrong/Hi, I'm researching camera calibration and uncertainty propagation. I'm trying to understand how calibrateCameraExtended estimates stdDeviationsIntrinsics and stdDeviationsExtrinsics. [The docs](https://docs.opencv.org/ref/master/d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d) say very little.
So I go straight to the source code. Check [this lines of code](https://github.com/opencv/opencv/blob/00f36a261b09d6d6a89d41fb6268bceecd2e0dfd/modules/calib3d/src/calibration.cpp#L1798) where I understand the calculation is made.
It starts with `sigma2` (the "deviation of the noise") calculated as `norm(allErrors, NORM_L2SQR) / (total - nparams_nz);`
which is just the formula for the [unbiased estimator of the variance](https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation). Ok so far.
And then it calculates each `s`-element of the vector of standard deviations `stdDevsM` by
stdDevsM.at<double>(s) = std::sqrt(JtJinv.at<double>(j,j) * sigma2);
Where `JtJinv` is the pseudo-inverse of the jacobian calculated a few lines above from `_JtJ` which in turn comes from the
LM solver invoked in [previous lines](https://github.com/opencv/opencv/blob/00f36a261b09d6d6a89d41fb6268bceecd2e0dfd/modules/calib3d/src/calibration.cpp#L1689).
**First question:** what exactly is `_JtJ`? I assume it must be the 1xN Jacobian of the projection error with respect to the parameters (there are N parameters). I've tried to trace the calculation of all the way to its origin, I got
[this far](https://github.com/opencv/opencv/blob/00f36a261b09d6d6a89d41fb6268bceecd2e0dfd/modules/calib3d/src/calibration.cpp#L809)
, but I'm not sure.
**Second question:** The moore-penrose of a 1xN matrix is a Nx1 matrix. So calling `JtJinv.at<double>(j,j)` with two indices `j` confuses me.
**Third question:** I couldn't make sense of the formula itself and I think It's wrong. The code has the comment
//see any papers about variance of the least squares estimator for
//detailed description of the variance estimation methods
But my understanding from simple [uncertainty propagation](https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Linear_combinations) for the case of uncorrelated parameters is that the Jacobian vector `_JtJ`, the variance of the projection error `sigma2` and the vector of parameters standard deviations `stdDevsM` should follow (in matlab-like pseudocode):
sigma2 = dot(_JtJ.^2, stdDevsM.^2)
Which is ill-conditioned, the simplest solution would be
stdDevsM = sqrt(sigma2) .*_JtJ ./ norm(_JtJ.^2)
Is my reasoning correct? and, where does the calculation implemented in OpenCV come from?
JamesBaxterTue, 15 Oct 2019 20:26:26 -0500http://answers.opencv.org/question/219805/Are there common values of standard deviation for Gaussian noise of an image?http://answers.opencv.org/question/210429/are-there-common-values-of-standard-deviation-for-gaussian-noise-of-an-image/ For example, it occurs to me that there might be a standard deviation value of Gaussian noise in which the human eye could no longer distinguish an object because the standard deviation is very high and many edges were removed because of that amount of noise. Or I think maybe there are values of standard deviation in which the human eye can better distinguish an object in an image because the model of vision of the eye is modeled in such a way that these values of standard deviation allow the eye to recognize or distinguish an object. Is there any research about this that I am commenting on? Are there "important" values of Gaussian noise standard deviation in an image? Thanks in advance
SRG-FTLMon, 18 Mar 2019 00:53:08 -0500http://answers.opencv.org/question/210429/How to calculate standard deviation on image with transparencyhttp://answers.opencv.org/question/178064/how-to-calculate-standard-deviation-on-image-with-transparency/ Hello everyone.
How do I calculate the standard deviation and mean on a certain image with transparency?
I tried loading an image and then calculate the stddev and mean but I'm getting unexpected results.
This is my image with transparency:
![image description](/upfiles/15103086116217038.png)
Now, I want to calulate this only on the red star. So how do I do this? Because OpenCV treats transparent as black.
Thanks.AndriezelFri, 10 Nov 2017 04:12:21 -0600http://answers.opencv.org/question/178064/How to calculate StdDev for RGB image?http://answers.opencv.org/question/177973/how-to-calculate-stddev-for-rgb-image/Hello,
I'm trying to get the mean and standard deviation on a RGB image.
I have used the example code and I have this:
cv::Mat image = cv::imread("C:/stddev.jpg");
cv::Scalar mean, stddev; //0:1st channel, 1:2nd channel and 2:3rd channel
meanStdDev(image, mean, stddev, cv::Mat());
std::cout << "Standard deviation channel B,G,R: " << stddev << std::endl;
std::cout << "Mean B,G,R: " << mean << std::endl;
I have used this picture: ![image description](/upfiles/1510215759846262.jpg)
And this is my output:
**Standard deviation channel B,G,R: [0,0,0,0]
Mean B,G,R: [1, 103, 168, 0]**
In Adobe Photoshop it gives me the same values for each seperate channel but when I select RGB (So for all channels I suppose) it gives me a standard deviation of 69.17 and a mean of 90.33:
![image description](https://i.gyazo.com/65d11a44719c91a3a97d588025c06900.png)
So how do I get those numbers instead of each channel in OpenCV?AndriezelThu, 09 Nov 2017 02:32:55 -0600http://answers.opencv.org/question/177973/Problem with meanStdDevhttp://answers.opencv.org/question/6501/problem-with-meanstddev/I seem to be having a problem trying to compute the mean and standard deviation of pixels in an image. Can someone look at my code and point out what I am doing wrong? I am unable to get correct results.
cv::Mat img;
cv::Mat mean;
cv::Mat stddev;
cv::meanStdDev ( img, mean, stddev );
uchar mean_pxl = mean.data[0];
uchar stddev_pxl = stddev.data[0];
I have also tried to get the data using `mean.at<uchar>(0)` without success.
Thanks for your help.unxnutFri, 25 Jan 2013 14:14:51 -0600http://answers.opencv.org/question/6501/Matrix Deviationhttp://answers.opencv.org/question/124500/matrix-deviation/
I want to make dev1,dev2,dev3 to array mat like ( dev[d] )but i can't.
Mat dev1,dev2,dev3;
dev1=Mat(align[0])-average;
dev2=Mat(align[1])-average;
dev3=Mat(align[2])-average;
is there anyone can help me? thank youbelanTue, 31 Jan 2017 08:48:05 -0600http://answers.opencv.org/question/124500/How cv::meanStdDev workshttp://answers.opencv.org/question/70070/how-cvmeanstddev-works/ Hi everyone,
I'm porting some parts of OpenCV code to an ActionScript/AIR project. I have to port the function meanStdDev, which, according to the documentation:
> calculates the mean and the standard deviation M of array elements independently for each channel (...) The calculated standard deviation is only the diagonal of the complete normalized covariance matrix.
I'm not very good at statistics. I know that the standard deviation is the square root of variance, which is calculated subtracting each element from the vector's mean and squaring it; finally, summing all results and dividing by the mean.
What I need to know is: how this function calculates the standard deviation? Does anybody could comment this function's code in detail? Here's the code I found on the official GitHub, under modules/core/src/stat.cpp. Thank you very much.
void cv::meanStdDev( InputArray _src, OutputArray _mean, OutputArray _sdv, InputArray _mask ) {
CV_OCL_RUN(OCL_PERFORMANCE_CHECK(_src.isUMat()) && _src.dims() <= 2,
ocl_meanStdDev(_src, _mean, _sdv, _mask))
Mat src = _src.getMat(), mask = _mask.getMat();
CV_Assert( mask.empty() || mask.type() == CV_8UC1 );
CV_IPP_RUN(IPP_VERSION_MAJOR >= 7, ipp_meanStdDev(src, _mean, _sdv, mask));
int k, cn = src.channels(), depth = src.depth();
SumSqrFunc func = getSumSqrTab(depth);
CV_Assert( func != 0 );
const Mat* arrays[] = {&src, &mask, 0};
uchar* ptrs[2];
NAryMatIterator it(arrays, ptrs);
int total = (int)it.size, blockSize = total, intSumBlockSize = 0;
int j, count = 0, nz0 = 0;
AutoBuffer<double> _buf(cn*4);
double *s = (double*)_buf, *sq = s + cn;
int *sbuf = (int*)s, *sqbuf = (int*)sq;
bool blockSum = depth <= CV_16S, blockSqSum = depth <= CV_8S;
size_t esz = 0;
for( k = 0; k < cn; k++ )
s[k] = sq[k] = 0;
if( blockSum )
{
intSumBlockSize = 1 << 15;
blockSize = std::min(blockSize, intSumBlockSize);
sbuf = (int*)(sq + cn);
if( blockSqSum )
sqbuf = sbuf + cn;
for( k = 0; k < cn; k++ )
sbuf[k] = sqbuf[k] = 0;
esz = src.elemSize();
}
for( size_t i = 0; i < it.nplanes; i++, ++it )
{
for( j = 0; j < total; j += blockSize )
{
int bsz = std::min(total - j, blockSize);
int nz = func( ptrs[0], ptrs[1], (uchar*)sbuf, (uchar*)sqbuf, bsz, cn );
count += nz;
nz0 += nz;
if( blockSum && (count + blockSize >= intSumBlockSize || (i+1 >= it.nplanes && j+bsz >= total)) )
{
for( k = 0; k < cn; k++ )
{
s[k] += sbuf[k];
sbuf[k] = 0;
}
if( blockSqSum )
{
for( k = 0; k < cn; k++ )
{
sq[k] += sqbuf[k];
sqbuf[k] = 0;
}
}
count = 0;
}
ptrs[0] += bsz*esz;
if( ptrs[1] )
ptrs[1] += bsz;
}
}
double scale = nz0 ? 1./nz0 : 0.;
for( k = 0; k < cn; k++ )
{
s[k] *= scale;
sq[k] = std::sqrt(std::max(sq[k]*scale - s[k]*s[k], 0.));
}
for( j = 0; j < 2; j++ )
{
const double* sptr = j == 0 ? s : sq;
_OutputArray _dst = j == 0 ? _mean : _sdv;
if( !_dst.needed() )
continue;
if( !_dst.fixedSize() )
_dst.create(cn, 1, CV_64F, -1, true);
Mat dst = _dst.getMat();
int dcn = (int)dst.total();
CV_Assert( dst.type() == CV_64F && dst.isContinuous() &&
(dst.cols == 1 || dst.rows == 1) && dcn >= cn );
double* dptr = dst.ptr<double>();
for( k = 0; k < cn; k++ )
dptr[k] = sptr[k];
for( ; k < dcn; k++ )
dptr[k] = 0;
} }
eldersoaresThu, 03 Sep 2015 10:16:10 -0500http://answers.opencv.org/question/70070/Normalized standard deviationhttp://answers.opencv.org/question/53124/normalized-standard-deviation/ What is the easiest way to calculate normalized standard deviation for a certain region of an image?kovand11Fri, 16 Jan 2015 02:39:31 -0600http://answers.opencv.org/question/53124/Standard deviation from discrete valueshttp://answers.opencv.org/question/26422/standard-deviation-from-discrete-values/I want to calculate standard deviation for every pixel in image, that I will get from web camera. So I will have many samples for every pixel.
Is possible to calculate the standard deviation of discrete values, without knowing the probabilities of the values (without keeping the last 100 frames into the memory)?
Spas HristovMon, 13 Jan 2014 03:18:37 -0600http://answers.opencv.org/question/26422/