Tip, How to count number of '0' in element of Matrix(Mat)?

As same with Matlab, we can use inequality ">,<,==,>=,<=".

Firstly, we check equal to '0' or '>0', the result is output to '255' if satisfied.
Divide by 255 then elements have left '0' or '1'
And sum all of the element, then we can get the number of zero.



Source code is here..

Mat a = Mat(5, 5, CV_8UC1);
randn(a, 0, 1);

Mat b = (a == 0) / 255;
Mat c = (a > 0) / 255;

cout << "Input matrix matrix a = " << endl;
cout << a << endl;

cout << "number of 0 = " << sum(b)[0];
cout << ", number of over 0 = " << sum(c)[0] << endl << endl;


cout << "matrix b = " << endl;
cout << b << endl;
cout << "matrix c = " << endl;
cout << c << endl;

opencv lecture 4-7 source code, noise generation

OpenCV Lecture page is here
http://cvlecture.marearts.com/2017/03/opencv-lecture-4-7-noise-generation.html


<code start>

<code end>



#OpenCV, #OpenCVLecture, #randn, #randu, #minMaxIdxthreshold


opencv randn(...) example

opencv randn is like in matlab.

The randn function make values of normal distribution random

in matlab
randn is usage like this..

randn()
>> 0.4663

randn(10,1)'
>>   -0.1465    1.0143    0.4669    1.5750   -1.1900    0.2689   -0.2967   -0.4877    0.5671    0.5632

to use mean 5, variance 3
5+3*rand(10,1)
>> 6.2932   12.5907    6.6214    1.6941    4.8522    3.1484    6.1745    4.5230    5.2183    5.6888


OK, now consider case of OpenCV
We will make mean 10 and variance 2 normal distribution random values and fill in 2x10 matrix.

example 1)
randn
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i &lt; 10; ++i)
{
  cout << matrix2xN.at<float>(0, i) << " ";
  cout << matrix2xN.at<float>(1, i) << endl;
}
..

example 2)
randn and randu
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i < 10; ++i)
{
    cout << matrix2xN.at< float>(0, i) << " ";
    cout << matrix2xN.at< float>(1, i) << endl;
}

//gaussian generation example
Mat Gnoise = Mat(5, 5, CV_8SC1);
randn(Gnoise, 5, 10); //mean, variance
cout << Gnoise << endl;
//
Mat Unoise = Mat(5, 5, CV_8SC1);
randu(Unoise, 5, 10); //low, high
cout << Unoise << endl;


//noise adapt
Mat Gaussian_noise = Mat(img.size(), img.type());
double mean = 0;
double std = 10;
randn(Gaussian_noise, mean, std); //mean, std
Mat colorNoise = img + Gaussian_noise;
..