OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 28 Jul 2018 11:05:02 -0500cv::cuda::meanStdDev for CV_32FC1http://answers.opencv.org/question/111874/cvcudameanstddev-for-cv_32fc1/Hello everyone!
I was wondering whether `cv::cuda::meanStdDev` function will be implemented for the `CV_32FC1` type in the near future or if it's not in the priority list at all.
I know I can download the image and use the host function (that's exactly my workaround now) but this is very slow and involves a download operation. I'm barely achieiving real-time.
Thanks a lot.mescarraThu, 10 Nov 2016 13:12:55 -0600http://answers.opencv.org/question/111874/Calculate Mean and StdDev for the whole RGB imagehttp://answers.opencv.org/question/196552/calculate-mean-and-stddev-for-the-whole-rgb-image/opencv provides convenient function such as `cv::mean` and `cv::meanStdDev` which return mean and standard deviation vectors with particular values for each channel of the supplied RGB image.
Is there any way/function that I might be missing, to calculate mean and standard deviation of a whole RGB image i.e. not per channel values? In other words, what I'm looking for is a way or a function that would return a single mean value for the whole RGB image and equally a single stddev value for the whole image.
It's easy to calculate the mean for the whole RGB image as I can simply calculate mean of the returned channel mean(s), however I can't do the same thing with standard deviation.
milosgajdosSat, 28 Jul 2018 11:05:02 -0500http://answers.opencv.org/question/196552/initialise a 4 channel matrix opencvhttp://answers.opencv.org/question/190989/initialise-a-4-channel-matrix-opencv/
I am trying to initialise a 4 channel matrix with row = 1 and columns = 5, but I am always getting an exception ` error: (-215) (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())` . Anyone knows what I am doing wrong.
cv::Mat_<char> samples(1,5,CV_8UC4);
samples.at<cv::Vec4b>(0,0) = { 1, 2 , 3 , 4 } ;
samples.at<cv::Vec4b>(0,1) = { 1, 2 , 3 , 4 } ;
samples.at<cv::Vec4b>(0,2) = { 1, 2 , 3 , 4 } ;
samples.at<cv::Vec4b>(0,3) = { 1, 2 , 3 , 4 } ;
samples.at<cv::Vec4b>(0,4) = { 1, 2 , 3 , 4 } ;
infocloggedMon, 07 May 2018 03:32:50 -0500http://answers.opencv.org/question/190989/meanStdDev seems to have a bug? Or am I using it wrong?http://answers.opencv.org/question/182951/meanstddev-seems-to-have-a-bug-or-am-i-using-it-wrong/When I use meanStdDev for an array with more than a million elements (m = 1228800x1 matrix, by reshaping a single channel 1280x960 image), I seem to get an overflow or something like that.
Code snippet:
m=cvarrToMat(img, true);
m=m.reshape(0,1); //makes m a 1228800x1 matrix
m.copyTo(m1);
meanStdDev(m1, meanframe1, stdframe1);
If I use a 128x96 matrix as an input, I get values like
meanframe(0)=3067.9
stdframe1(0)=107.072
which seem to be correct for a dark frame from a cmos camera.
But if I use a 1280x960 image as input under the same conditions, I get
meanframe(0)=-454.69
stdframe1(0)=14832.8
which is absolutely wrong - negative number for mean!
Additional info:
meanframe1=mean(m1);
gives the correct answer even for large matrices. So, meanStdDev along seems to be the culprit....
More additional info:
my own code for StdDev also gives correct results,
meanframe1=mean(m1);
double dstddev = 0.0;
double dmean = meanframe1(0); // only one channel
for (int i = 0; i < w*h; i++)
{
dstddev += (dmean - m1.at<ushort>(i)) * (dmean - m1.at<ushort>(i));
}
dstddev = sqrt(dstddev / (w*h));
std::cout<<"mean="<<meanframe1(0)<<std::endl;
std::cout<<"stddev="<<dstddev<<std::endl;hn_88Sun, 21 Jan 2018 20:11:22 -0600http://answers.opencv.org/question/182951/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/How to know the output array type of meanStdDev without looking at the source codehttp://answers.opencv.org/question/161509/how-to-know-the-output-array-type-of-meanstddev-without-looking-at-the-source-code/Hi All:
The API documentation of meanStdDev (C++: void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev, InputArray mask=noArray())) does not clearly indicate the OutputArray type, i.e., whether the output array is a CV_32S or CV_32F or CV_64F. Only by checking the source code (https://github.com/opencv/opencv/blob/master/modules/core/src/stat.cpp) I find out the rules for determining the output type:
> int type = _src.type(), depth =
> CV_MAT_DEPTH(type);
> ddepth = std::max(CV_32S, depth),
> sqddepth = std::max(CV_32F, depth),
> dtype = CV_MAKE_TYPE(ddepth, cn),
> sqdtype = CV_MAKETYPE(sqddepth, cn);
> // dtype and sqdtype refers to the output array type of mean and stddev
Without such important type info, I do not know how to properly access the output array (i.e. mean.at<T>(0)). I am wondering is there a easier way to look up such important info? In addition, what are the general rules regarding the input/output parameters types for OpenCV functions?
jacobdangTue, 20 Jun 2017 23:09:48 -0500http://answers.opencv.org/question/161509/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/Issue with meanStdDev() result and Mat channelshttp://answers.opencv.org/question/101340/issue-with-meanstddev-result-and-mat-channels/ **meanStdDev()** is not giving the correct calculation of mean and standard. I want it to give mean/stdev for each corresponding column. The code follows,
Mat_<int> C = (Mat_<int>(2,6) << 1, 0, 3, 1, 0, 5, 6, 2, 0, 4, 0, 9), E=Mat::zeros(9, 9, CV_8UC1);
Mat mn, stdev;
meanStdDev(C.reshape(C.cols),mn,stdev);
std::cout << C.reshape(C.cols) << endl<< mn << std::endl;
It gives the following results,
[1, 0, 3, 1, 0, 5;
6, 2, 0, 4, 0, 9]
[2.5;
1;
0;
0;
0;
0]
You can see that the calculated mean value is corrent only for the first 2 positionsdareletTue, 30 Aug 2016 07:46:17 -0500http://answers.opencv.org/question/101340/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/Get the median, added to mean and std valuehttp://answers.opencv.org/question/20522/get-the-median-added-to-mean-and-std-value/I'm using : Core.meanStdDev(hist, mean, stdev);
Is there a possibility to have median in the result of meanStdDev ?Pascal66Wed, 11 Sep 2013 04:36:40 -0500http://answers.opencv.org/question/20522/