OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 28 Aug 2018 13:25:09 -0500cv::mean() and cv::calcCovarMatrix() returns different meanhttp://answers.opencv.org/question/198239/cvmean-and-cvcalccovarmatrix-returns-different-mean/I have two pictures in grayscale of the same size MxN, img1 and img2. When I use function cv::mean() on these pictures, it returns one values. After that, I produce two column vectors of these pictures and get a matrix of size MNx2, samples. Then i use function cv:: calcCovarMatrix(). This function should return covariation matrix and matrix of mean values on each columns. But values of mean matrix of function cv:: calcCovarMatrix(). are differ from values returned from cv::mean() function.
Here's my code.
Mat img1 = imread( "sample.jpeg", 0 );
Mat img2 = imread( "sample3.jpeg", 0 );
cout<<mean(img1)<<endl;
cout<<mean(img2)<<endl;
results are:
[127.303, 0, 0, 0]
[161.606, 0, 0, 0]
after that I use function cv::calcCovarMatrix():
int all_pixels = img1.cols*img1.rows;
Mat_<float> samples;
samples.create(all_pixels, 2);
int counter = 0;
for(int i = 0; i < img1.rows; i++){
for(int j = 0; j < img1.cols; j++){
samples.at<float>(0,counter) = img1.at<uchar>(i,j);
samples.at<float>(1,counter) = img2.at<uchar>(i,j);
counter++;
}
}
Mat cov, mu;
calcCovarMatrix(samples, cov, mu, COVAR_NORMAL | COVAR_ROWS);
cout << "mu: " << endl;
cout << mu << endl;
it returns :
mu:
[63.65198211624441, 63.65911574764034]
Why these values are not the same as in cv::mean()?
I tried another way to copy data, and not I have different values of covariances and mean for different ways of copying data
When I used this new way to copy data, I get different results of covariance.
double av1 = mean(img1)[0];
double av2 = mean(img2)[0];
cout<<"mean1 = "<<av1<<", mean2 = "<<av2<<endl;
gives me:
mean1 = 127.303, mean2 = 161.606
and then I have two sample arrays, samplesOld and samplesNew.
Old way of copying data:
int all_pixels = img1.cols*img1.rows;
cout<<"all_pixels = "<<all_pixels<<endl;
Mat_<float> samplesOld;
samplesOld.create(all_pixels, 2);
int counter = 0;
for(int i = 0; i < img1.rows; i++){
for(int j = 0; j < img1.cols; j++){
samplesOld.at<float>(0,counter) = (img1.at<uchar>(i,j));
samplesOld.at<float>(1,counter) = (img2.at<uchar>(i,j) );
counter++;
}
}
New way of copying data:
Mat samplesNew;
samplesNew.push_back(img1.reshape(1,1)); // img1, flattened, goes on the 1st row
samplesNew.push_back(img2.reshape(1,1)); // img2 on the 2nd, etc.
samplesNew.convertTo(samplesNew, CV_32F);
transpose(samplesNew,samplesNew);
After that I have new arrays for covariances of old and new array samples arrays
Mat covOld, muOld;
cv::calcCovarMatrix(samplesOld, covOld, muOld, COVAR_NORMAL | COVAR_ROWS);
Mat covNew, muNew;
cv::calcCovarMatrix(samplesNew, covNew, muNew, COVAR_NORMAL | COVAR_ROWS);
covOld = covOld / (samplesOld.rows - 1);
covNew = covNew / (samplesNew.rows - 1);
cout << "\ncovOld: " << endl;
cout << covOld << endl;
cout << "covNew: " << endl;
cout << covNew << endl;
This gives me
covOld:
[7215.396208855592, 7157.354438304864;
7157.354438304864, 7215.581732969795]
covNew:
[6327.103578936067, 1127.485398832948;
1127.485398832948, 5500.445588341399]
and this
cout << "\nmuOld: " << endl;
cout << muOld << endl;
cout << "muNew: " << endl;
cout << muNew << endl;
gives me values:
muOld:
[63.65198211624441, 63.65911574764034]
muNew:
[127.3026130153999, 161.606000993542]
Here in muNew values are same as returned by mean() function. But I don't understand, why on my arrays samplesNew and samplesOld which are of same size calcCovarMatrix() returns different values?AiyyaaTue, 28 Aug 2018 13:25:09 -0500http://answers.opencv.org/question/198239/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/Local 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/calculate intensity for region of interesthttp://answers.opencv.org/question/159424/calculate-intensity-for-region-of-interest/ hi.
good time.I have a gray scale image with a bounding box in it.I want to calculate intensity for bounding box.the resolution of my picture is **720*750** and my rectangle is:
**rectangle(400,210,70,50)**.do you have any idea to calculate the intensity for this region of my image?Thanks a lot for you're help.louis89Mon, 12 Jun 2017 05:45:20 -0500http://answers.opencv.org/question/159424/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/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/Get mean coordinates of active pixelshttp://answers.opencv.org/question/94879/get-mean-coordinates-of-active-pixels/
I would like to retrieve the mean coordinates of pixels in a binary image/mat which are not 0. Is there an existing way of doing this in OpenCV?
Regards,
RupertperceptualrobotsWed, 25 May 2016 12:59:28 -0500http://answers.opencv.org/question/94879/Issues by performing non-local means filter operation on 16bit imagehttp://answers.opencv.org/question/92522/issues-by-performing-non-local-means-filter-operation-on-16bit-image/ Dear great OpenCV community,
I have some issues by processing 16bit Images through the cv::fastNlMeansDenoising Image filter. I have read in the documentation that it is possible if you set the normType = NORM_L1
So i have tried it but nothing happens. The Images still look like the origin Images. So i thought maybe it is a issue with the depth of the Images. Therefore i have integrated in my program a down sampling of the Image before the filtering Operation (16bit -> 8bit) and after the filtering Operation is performed i integrated a up sampling of the Images (8bit -> 16bit). And now the supprising result was that it works with 8bit data perfectly and with 16bit data it seems like the filtering Operation isn't performed because the processed Images look exactly like the original Images
This is my code which i use for the filtering Operation. The Settings of the filter become inserted by the user of my program
//this works perfectly
cv::Mat inputIm;
cv::Mat outputIm;
filterInputCvImage.convertTo(inputIm, CV_8U, 1 / 256.);
cv::fastNlMeansDenoising(inputIm, outputIm, h, templateWindowSize, searchWindowSize, cv::NORM_L1);
outputIm.convertTo(filterOutputImage, CV_16U, 256.0);
//this does not work
cv::fastNlMeansDenoising(filterInputImage, filterOutputImage, h, templateWindowSize, searchWindowSize, cv::NORM_L1);
NewDeveloperTue, 12 Apr 2016 04:01:43 -0500http://answers.opencv.org/question/92522/OpenCV mean function crashes with mask created from OpenCV threshold function?http://answers.opencv.org/question/92493/opencv-mean-function-crashes-with-mask-created-from-opencv-threshold-function/ I'm very new to OpenCV, so I guess I'm making some silly mistake.
First I'm creating an empty `cv::mat` matrix, `mask`, of `CV_8U` type and filling it with zeros. Then I'm filling the matrix with either 0's or 255's by checking the values in `CV_32FC1` type `cv::mat` matrix `croppedDifferenceImage` by the use of `cv::threshold`. Then I'm using `mask` as a parameter for `cv::mean` function.
cv::Mat mask = cv::Mat(croppedDifferenceImage.rows, croppedDifferenceImage.cols, CV_8U, cv::Scalar(0));
cv::threshold(croppedDifferenceImage, mask, 3.2, 255, CV_THRESH_BINARY);
double mean = cv::mean(croppedDifferenceImage, mask)[0];
But I keep having a crash with the message:
/build/buildd/opencv-2.4.8+dfsg1/modules/core/src/stat.cpp:565: error: (-215) mask.empty() || mask.type() == CV_8U in function mean
What am I doing wrong?the_naive_Mon, 11 Apr 2016 13:10:51 -0500http://answers.opencv.org/question/92493/Accuracy in cv::mean function between opencv 2.4.9 and opencv 3.1.0http://answers.opencv.org/question/91527/accuracy-in-cvmean-function-between-opencv-249-and-opencv-310/ Hello,
I just changed my software which was using opencv 2.4.9 version to opencv 3.1.0
and I am having a very very little differences from cv::mean function.
cv::Scalar aux1 = cv::mean(img_32);
std::cout << "MEAN: "<< aux1.val[0] << std::endl;
Output with opencv 2.4.9:
From ORIGINAL [img_32]: [169,168,208] // Some pixel values from img_32
MEAN: 0.439712
Output with opencv 3.1.0:
From UPDATED [img_32]: [169,168,208] // Some pixel values from img_32
MEAN: 0.439713
I checked rigorously if there were differences in the image....
Can anyone help me?? Any idea??
Thanks in advance
ArritmicThu, 31 Mar 2016 13:35:48 -0500http://answers.opencv.org/question/91527/mean prediction in a imagehttp://answers.opencv.org/question/87065/mean-prediction-in-a-image/Hi everyone.
I just want to ask what means mean prediction in a image, to improve the quality of its. I tried to found some information on internet, but I'm not sure of what to do.
From wikipedia, I understand the Mean-Prediction in a JPEG image is the value of 2 blocks + the variance of the block. The problem is i dont understand what I need to do for improve the quality of a JPEG image.
Is more a theorical question, but I really need help to understand what to do when a compression value affects the quality of a original image, and how to applie it. Is there a function on OPENCV that could help me?
Thank you.
http://stats.stackexchange.com/questions/20741/mean-squared-error-vs-mean-squared-prediction-error This one is for the error.Cabezon667Mon, 08 Feb 2016 14:08:19 -0600http://answers.opencv.org/question/87065/Calculating mean value of pixels of many imageshttp://answers.opencv.org/question/84069/calculating-mean-value-of-pixels-of-many-images/ I have some images that I want to calculate mean value of each pixel of all images. Lets say I have 5 image of size 3x3. I read all pixels of each image and put them in row of a Mat object so at the end I have a 5x15 Mat. Using c++ code it is easy (I have a `Vector<Vector<double>>`)
std::for_each(data->begin(), data->end(),
[&](const std::vector<uchar>& row)
{
std::transform(row.begin(), row.end(), colSums.begin(), colSums.begin(),
[](double d1, double d2) { return d1 + d2; });
});
for (unsigned int i = 0; i < elementsCount; i++) {
colSums[i] /= dataCount;
}
but how I can use opencv itself to do this? I want sum of each column divided by number of images to form a new Mat. I tried the code below but it does not work gives only a 1x1 mat!!!
void BayesianClassifier::createAggregateFromTrainingVector(pr::training_vector tv)
{
//building a Mat object from vector pointer
cv::Mat mat(tv.size(), tv.at(0).size(), CV_8UC1);
int rows = mat.rows;
int cols = mat.cols;
for (int r = 0; r < rows; ++r) {
uchar *pInput = mat.ptr<uchar>(r);
for (int c = 0; c < cols; ++c) {
*pInput = tv.at(r)[c];
++pInput;
}
}
mData = mat;
cv::meanStdDev(mData, mMatMean, mMatVariance);
}
**UPDATE**
I feel that I didn't clarify enough. Imagine the 6x6 matrix below, where each row represents a 2x3 image, and for simplification lets imagine all images are the same! (I call this Mat `mData`)
{ 1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6 }
I want to get the mean value of each pixel, so at the end I want to show a 3x3 image of the mean values. For this, I need to calculate mean of each column. For the simple case above, for column 1 it will be:
SUM OF COL 1 => 1 + 1 + 1 + 1 + 1 + 1 = 6
MEAN OF COL 1 => 6/6 = 1
Doing this for all column will result in:
{ 1, 2, 3, 4, 5, 6 }
and converting this single row into 2x3 image:
{ 1, 2, 3
4, 5, 6 }
**So my question is, if I do the following code:**
Mat cov, mean;
cv::calcCovarMatrix(mData, cov, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);
The `mean` will not be a 1x6 `{ 1, 2, 3, 4, 5, 6 }` matrix like what I theoretically calculated above?
Sean87Fri, 15 Jan 2016 15:06:59 -0600http://answers.opencv.org/question/84069/median and mean filtering.http://answers.opencv.org/question/54744/median-and-mean-filtering/ Hello and thanks for your help
Looking through the documentation I found the function *medianblur*
it seems to apply the median of a sector of an image defined by a square of some size.
My first question is: Is there something similar but for mean not median??
--
my second question is , what if afterwards I want to apply some operation to my original image and to the mean image. (substracting, multiplying by some scalar, adding) . Can I do this just with +.* /. as if images were just simple matrices... (which they are, no???)
Thanks a thousandKansaiRobotMon, 09 Feb 2015 01:51:16 -0600http://answers.opencv.org/question/54744/Expectation Maximization using HSVhttp://answers.opencv.org/question/30495/expectation-maximization-using-hsv/I have a working program which uses the EM class, trained on RGB values to classify pixels in an image. Is it possible to do the same thing using HSV values? The problem is that hue is an angle. Thus, [the mean cannot be calculated in the usual way](https://en.wikipedia.org/wiki/Mean_of_circular_quantities).ajplockyerMon, 24 Mar 2014 12:56:31 -0500http://answers.opencv.org/question/30495/openCV 2.4.5. and Submatriceshttp://answers.opencv.org/question/14950/opencv-245-and-submatrices/Hello everybody,
I wanted to ask for some help as I encounter some difficulties trying to work with submatrices auf an Image. Here is what I want to achieve:
Having a picture auf 10000 x 10000 pixels I want to calculate the mean of single 4x4 blocks in that picture and then substract the mean from the 16 pixel values in the block.
What is the best way to access those 4x4 submatrices and process them.
Thank you very much for your help!
best regards,
MaxdjtschkeMon, 10 Jun 2013 08:52:10 -0500http://answers.opencv.org/question/14950/