## 6/09/2016

### OpenCV Mat and Matrix operation examples

example code.

...
Mat Ma = Mat::eye(3, 3, CV_64FC1);
cout << Ma << endl;
double dm[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Mat Mb = Mat(3, 3, CV_64F, dm);
cout << Mb << endl;

//Matrix - matrix operations :
Mat Mc;
cv::add(Ma, Mb, Mc); // Ma+Mb   -> Mc
cout << Ma+Mb << endl;
cout << Mc << endl;
cv::subtract(Ma, Mb, Mc);      // Ma-Mb   -> Mc
cout << Ma - Mb << endl;
cout << Mc << endl;
Mc = Ma*Mb; //Ma*Mb;
cout << Mc << endl;

//Elementwise matrix operations :
cv::multiply(Ma, Mb, Mc);   // Ma.*Mb   -> Mc
cout << Mc << endl;
Mc = Ma.mul(Mb);
cout << Mc << endl;
cv::divide(Ma, Mb, Mc);      // Ma./Mb  -> Mc
cout << Mc << endl;
Mc = Ma + 10; //Ma + 10 = Mc
cout << Mc << endl;

//Vector products :
double va[] = { 1, 2, 3 };
double vb[] = { 0, 0, 1 };
double vc[3];

Mat Va(3, 1, CV_64FC1, va);
Mat Vb(3, 1, CV_64FC1, vb);
Mat Vc(3, 1, CV_64FC1, vc);

double res = Va.dot(Vb); // dot product:   Va . Vb -> res
Vc = Va.cross(Vb);    // cross product: Va x Vb -> Vc
cout << res << " " << Vc << endl;

//Single matrix operations :
Mc = Mb.t();      // transpose(Ma) -> Mb (cannot transpose onto self)
cout << Mc << endl;
cv::Scalar t = trace(Ma); // trace(Ma) -> t.val[0]
cout << t.val[0] << endl;
double d = determinant(Ma); // det(Ma) -> d
cout << d << endl;
Mc = Ma.inv();         // inv(Mb) -> Mc
invert(Ma, Mc);
cout << Mc << endl;

//Inhomogeneous linear system solver :
double dm2[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Mat A(3, 3, CV_64FC1, dm2);
Mat x(3, 1, CV_64FC1);
double vvb[] = { 14, 32, 52 };
Mat b(3, 1, CV_64FC1, vvb);
cv::solve(A, b, x, DECOMP_SVD); //// solve (Ax=b) for x
cout << x << endl;

//Eigen analysis(of a symmetric matrix) :
float f11[] = { 1, 0.446, -0.56, 0.446, 1, -0.239, -0.56, 0.239, 1 };
Mat data(3, 3, CV_32F, f11);
Mat value, vector;
eigen(data, value, vector);
cout << "Eigenvalues" << value << endl;
cout << "Eigenvectors" << endl;
cout << vector << endl;

//Singular value decomposition :
Mat w, u, v;
SVDecomp(data, w, u, v); // A = U W V^T
//The flags cause U and V to be returned transposed(does not work well without the transpose flags).
cout << w << endl;
cout << u << endl;
cout << v << endl;
...

### OpenCV image and video write

image write

..
vector< Mat> rgbMat(3);
cv::split(img, rgbMat);

namedWindow("img", 0); //make window
imshow("img", rgbMat[2]); //show
waitKey(0);

imwrite("r.jpg", rgbMat[2]);
imwrite("g.bmp", rgbMat[1]);
imwrite("b.tif", rgbMat[0]);

...

video write
http://study.marearts.com/2013/09/opencv-video-writer-example-source-code.html

### OpenCV Mat copyTo, Clone, ROI example code

OpenCV Mat copyTo, Clone, ROI example code

...

Rect r(img.cols / 4, img.rows / 4, img.cols / 4 * 2, img.rows / 4 * 2);

//clone #1
Mat img2 = img.clone();
bitwise_not(img2, img2);

//clone #2
Mat img5 = img(r).clone();

//copyTo #1
Mat cimg;
img.copyTo(cimg);

//copyTo #2
Mat cimg2;
img(r).copyTo(cimg2);

//copyTo #3
Mat cimg3( Size(img.cols*2, img.rows), img.type() );
cimg3.setTo(255);
img.copyTo(cimg3(Range::all(), Range(0, img.cols)));
img2.copyTo(cimg3(Range::all(), Range(img2.cols, img2.cols * 2)));

//set roi
Mat roi(img, r);
//invert color
bitwise_not(roi, roi);

namedWindow("img2", 0); //make window
imshow("img2", cimg); //show
namedWindow("img3", 0); //make window
imshow("img3", cimg2); //show
namedWindow("img4", 0); //make window
imshow("img4", cimg3); //show
namedWindow("img5", 0); //make window
imshow("img5", img5); //show
namedWindow("img", 0); //make window
imshow("img", img); //show

waitKey(0);

...

### OpenCV Pixel Access, at, ptr, data, iteration (example)

I once wrote the following article.
http://study.marearts.com/2014/04/opencv-study-mat-point-access-method.html

..
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int, char)
{
namedWindow("img", 1);

if (img.depth() == CV_8U)
printf("8bit unsigend\n");

img.

/*
cout << img.elemSize() << endl;
cout << img.channels() << endl;
cout << img.rows << endl;
cout << img.step << endl;
*/

//using data
//for (int i = img.rows / 10 * 7; i < img.rows / 10 * 8; i++) {

//for (int i = 0; i < img.rows ; i++) {

for (int i = img.rows / 10 * 7; i < img.rows / 10 * 8; i++) {
for (int j = 0; j < img.cols; j++) {

unsigned char r, g, b;

b = img.data[i * img.step + j * img.elemSize() + 0];
g = img.data[i * img.step + j * img.elemSize() + 1];
r = img.data[i * img.step + j * img.elemSize() + 2];

img.data[i * img.step + j * img.elemSize() + 0] = unsigned char(255 - b);
img.data[i * img.step + j * img.elemSize() + 1] = unsigned char(255 - g);
img.data[i * img.step + j * img.elemSize() + 2] = unsigned char(255 - r);

}
}

//using at
for (int i = img.rows / 10 * 2; i < img.rows / 10 * 3; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
//Vec3b means 'uchar 3ch'
unsigned char b = img.at< cv::Vec3b>(i, j)[0];
unsigned char g = img.at< cv::Vec3b>(i, j)[1];
unsigned char r = img.at< cv::Vec3b>(i, j)[2];

//printf("%d %d %d\n", b, g, r);

img.at< cv::Vec3b>(i, j)[0] = unsigned char(255 - b); //b
img.at< cv::Vec3b>(i, j)[1] = unsigned char(255 - g); //g
img.at< cv::Vec3b>(i, j)[2] = unsigned char(255 - r); //r
}
}

//using ptr
for (int i = img.rows / 10 * 5; i < img.rows / 10 * 6; i++) {

cv::Vec3b* ptr = img.ptr< cv::Vec3b >(i);

for (int j = 0; j < img.cols; j++) {

unsigned char b1 = (ptr[j][0]);
unsigned char g1 = (ptr[j][1]); //note!!
unsigned char r1 = (ptr[j][2]);

cv::Vec3b bgr = ptr[j];
unsigned char b2 = (bgr[0]);
unsigned char g2 = (bgr[1]); //note!!
unsigned char r2 = (bgr[2]);

ptr[j] = cv::Vec3b(255 - b1, 255 - g1, 255 - r1);

}
}

//using iteration
cv::MatIterator_< cv::Vec3b> itd = img.begin< cv::Vec3b>(), itd_end = img.end< cv::Vec3b>();

for (int i = 0; itd != itd_end; ++itd, ++i) {

cv::Vec3b bgr = (*itd);

(*itd)[0] = 255 - bgr[0];
(*itd)[1] = 255 - bgr[1];
(*itd)[2] = 255 - bgr[2];
}

imshow("img", img); //show
waitKey(0);

destroyAllWindows();

return 0;
}
..

input

output