OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 08 Jun 2018 10:34:08 -0500Local Mean and Variancehttp://answers.opencv.org/question/193393/local-mean-and-variance/I have an artificial flow field given by a homography transform. I want to compare this flow field to the results of an optical flow algorithm. I want to do a pixel-wise comparison in a statistical manner. It seems to me that I can best do this by comparing the optical flow value to the statistics of the local neighborhood of the homography flow. See [this paper](https://www.researchgate.net/profile/Thomas_Castelli3/publication/283462638_Moving_object_detection_for_unconstrained_low-altitude_aerial_videos_a_pose-independant_detector_based_on_Artificial_Flow/links/56390bb208aecf1d92a9bc69.pdf) for inspiration.
My question is, what OpenCV functions can I use to calculate the local mean and variance over some window?Der LuftmenschFri, 08 Jun 2018 10:34:08 -0500http://answers.opencv.org/question/193393/OpenCV Python GPU support ? or faster variance convolutionhttp://answers.opencv.org/question/165502/opencv-python-gpu-support-or-faster-variance-convolution/Hi,
I was wondering if the current OpenCV Python had GPU support yet ?
OR is there a faster way to calculate convolved variance ?
MaxFrom3DArray = numpy.amax(imgArray, axis=0) # where imgArray is a 3D array
Back2ImMax = Image.fromarray(MaxFrom3DArray, 'P')
Back2ImMax.save(os.path.join(MaxFromMulti, filename), "TIFF")
ForVariance = cv2.imread((MaxFromMulti + filename), cv2.IMREAD_UNCHANGED)
wlen = 40
def winVar(img, wlen):
wmean, wsqrmean = (cv2.boxFilter(x, -1, (wlen, wlen),
borderType=cv2.BORDER_REFLECT) for x in (img, img*img))
return wsqrmean - wmean*wmean
windowVar = winVar(ForVariance, wlen)
numpy.set_printoptions(threshold='nan')
print windowVar
This takes hours in Python, and ages using python multi-threading, with CPU cores maxed out.
It takes a fraction of a second and hardly any cpu usage when serialised in c sharp.
Doesn't something seem a bit off about that ?
Thanks in advance
TWPTimWebPhoenixTue, 11 Jul 2017 06:53:18 -0500http://answers.opencv.org/question/165502/normalize zero mean and unit variance opencv?http://answers.opencv.org/question/97160/normalize-zero-mean-and-unit-variance-opencv/ How can I normalize image with zero mean and unit variance? Thanks.Dinh ThapThu, 23 Jun 2016 05:34:44 -0500http://answers.opencv.org/question/97160/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/Is there a built-in function to calculate the variance of a cv::Mat ?http://answers.opencv.org/question/53985/is-there-a-built-in-function-to-calculate-the-variance-of-a-cvmat/ This is in order to normalize the cv::Mat before feeding it to an ANNDionysosWed, 28 Jan 2015 12:59:02 -0600http://answers.opencv.org/question/53985/Extracting a vector of pixel values across multiple frameshttp://answers.opencv.org/question/262/extracting-a-vector-of-pixel-values-across-multiple-frames/Hi -- I am very new to OpenCV, so please forgive my naivety. I'd like to examine how pixel values, at particular pixel locations, change over multiple frames. As a result, I am interested in reading in a vector of pixels across multiple images, rather than all pixels of an image. I can do this by reading in entire frames and storing the pixel values of interest to a separate vector, but is there is a more efficient and elegant approach using OpenCV?
Eventually I will be performing statistical analysis (mean, variance, etc.) of pixel intensity values in time. It appears that OpenCV has some nice functions for computing statistics of pixels in space (i.e. within an image), such as cvMean_StdDev, but it is not clear to me if OpenCV supports similar capability across multiple frames. In other words, for N frames of video, with each frame of dimensions W x H, does OpenCV have a quick and easy way to return a single W x H image representing the mean, std, or variance of each pixel over time? richWed, 11 Jul 2012 15:34:24 -0500http://answers.opencv.org/question/262/