Showing posts with label Mat. Show all posts
Showing posts with label Mat. Show all posts

1/12/2023

Opencv Mat <-> UMat , convert Mat to UMat, UMat to Mat

Mat to UMat 

c++

UMat umat;
mat.copyTo(umat);

Python

UMat umat = mat.getUMat( flag );


UMat to Mat

C++

Mat mat = umat.getMat( flag );

Python

mat = cv2.UMat.get(umat)

3/22/2018

Extract the value of interest rows and copy that to new Mat

Simple example code.

using push_back
#gistcode

#end code

using concate
#start code

#end code


9/24/2017

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;

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;
...


2/09/2015

OpenCV, Mat operation example of sum(sqrt( A.-B) )

Simple Mat operation using opencv.
But it always not remember well.





vector< float> v1;
 vector< float> v2;
 for(int i=0; i<5; ++i)
 {
  v1.push_back(i*2);
  v2.push_back(i*3);
 }


 Mat AA(v1.size(),1,CV_32FC1); 
 //copy vector to mat  
 memcpy(AA.data,v1.data(),v1.size()*sizeof(float));
 //create Mat  
 Mat BB(v2.size(),1,CV_32FC1); 
 //copy vector to mat  
 memcpy(BB.data,v2.data(),v2.size()*sizeof(float));

 cout << "AA: " << AA << endl;
 cout << "BB: " << BB << endl << endl;


 Mat CC = AA-BB;
 cout << "CC = AA-BB : " << CC << endl << endl;
 CC = CC.mul(CC);
 cout << "CC.*CC or CC^2 : " << CC << endl << endl;
 cv::sqrt(CC, CC);
 cout << "sqrt(CC.) : " << CC << endl << endl;
 cv::Scalar rr = cv::sum(CC);
 float rrr = rr(0);
 cout << "sum(CC.) : " << rrr << endl << endl;


4/22/2014

(OpenCV Study) Mat point access method, get pixel

1. At approach

Mat image (ROW, COL, CV_TYPE);
image.at (WANT_ROW, WANT_COL);

- ROW: Row
- COL: Column
- CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels)
- DATA_TYPE: Mat creation data type ( for example : float, usigned char)
- WANT_ROW: access to the desired row
- WANT_COL: access to the desired column

[Advantage]: Access after validation progress , so safe and accurate approach .
[Disadvantage]:  most slow in 3 ways.

2. Ptr approach
Mat image (ROW, COL, CV_TYPE);
image.ptr (WANT_ROW, WANT_COL); (This access is changed to the Point)

- ROW: Row
- COL: Column- CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels)
- DATA_TYPE: Mat creation data type ( for example : float, usigned char)
- WANT_ROW: access to the desired row
- WANT_COL: access to the desired column

[Advantage]: Access is faster than first way.
[Disadvantage]: direct access to the data , but way slower than third way .



3. Data approach
Mat image (ROW, COL, CV_TYPE);
DATA_TYPE * data = (DATA_TYPE *) image.data;data [WANT_ROW * image.cols + WANT_COL]

- ROW: Row
- COL: Column
- CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels)
- DATA_TYPE: Mat creation data type ( for example : float, usigned char)
- WANT_ROW: access to the desired row
- WANT_COL: access to the desired column

[Advantage]: very fast.
[Disadvantage]: not check validation ,  it is hard to know inadequate access.


This is sample code.
http://study.marearts.com/2016/06/opencv-pixel-access-at-ptr-data.html

4/17/2014

OpenCV Study, Merging to extended Mat from 2 Mat (example source code)

Example of merging to extended Mat from 2 Mat

For example,
A=[1 2 3; 4 5 6];
B=[7 8 9; 3 2 1];
C=[A; B];  < - how to make this merging Mat??

refer to this example source code.

---

Mat A(3, 10, CV_32F);
 Mat B(4, 10, CV_32F);

 int cnt=0;
 for(int i=0; i< A.rows; ++i)
 {
  for(int j=0; j< A.cols; ++j)
  {
   A.at< float>(i,j) = float(i*j);
  }
 }

 cout << "A" << endl;
 cout << A << endl << endl;
 
 for(int i=0; i< B.rows; ++i)
 {
  for(int j=0; j< B.cols; ++j)
  {
   B.at< float>(i,j) = float(i*j)*10;
  }
 }

 cout << "B" << endl;
 cout << B << endl << endl;
 


 Mat C(A.rows + B.rows, A.cols, CV_32F);
 memcpy(C.data, A.data, sizeof(float) * A.cols * A.rows );
 int startP = sizeof(float) * A.cols * A.rows;
 memcpy(&(C.data[ startP ]), B.data, sizeof(float) * B.cols * B.rows );

 cout << "C = [A; B]" << endl;
 cout << C << endl << endl;


...
The result of example source code..
 

1/07/2014

(OpenCV, Mat class) Image buffer(point) copy to Mat

when you want to copy "unsigned char * pData" image buffer to Mat class in OpenCV.

This source is simple example code.

Mat InImg(height, width, CV_8UC1);
memcpy(InImg.data, pData, sizeof(unsigned char)*width*height);