learning opencv3: 四:Mat

目錄

Creating an Array

The Most Important

Mat constructors

Static functions that create cv::Mat


Creating an Array

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;



int main(int argc, char** argv)
{

	cv::Mat m(3, 5, CV_32FC3, cv::Scalar(100, 0.0f, 1.240f));
	std::cout << m << std::endl;
	//getchar();
	system("pause");
		
	return 0;
}

// Set the values in the 1st channel to 1.0, the 2nd to 0.0, and the 3rd to 1.0 m.setTo( cv::Scalar( 1.0f, 0.0f, 1.0f ) );

The Most Important
 

It is critical to understand that the data in an array is not attached
rigidly to the array object. The cv::Mat object is really a header for
a data area, which—in principle—is an entirely separate thing. For
example, it is possible to assign one matrix n to another matrix m
(i.e., m=n). In this case, the data pointer inside of m will be changed
to point to the same data as n. The data pointed to previously by
the data element of m (if any) will be deallocated.4 At the same time,
the reference counter for the data area that they both now share
will be incremented. Last but not least, the members of m that char‐
acterize its data (such as rows, cols, and flags) will be updated to
accurately describe the data now pointed to by data in m. This all
results in a very convenient behavior, in which arrays can be
assigned to one another, and the work necessary to do this takes
place automatically behind the scenes to give the correct result.












In addition, some of these allow you to initialize the data, either by providing a
cv::Scalar (in which case, the entire array will be initialized to that value), or by
providing a pointer to an appropriate data block that can be used by the array. In this
latter case, you are essentially just creating a header to the existing data (i.e., no data
is copied; the data member is set to point to the data indicated by the data
argument).




Mat constructors
 

 

The subregion (also known as “region of interest”) constructors also come in three
flavors: one that takes a range of rows and a range of columns (this works only on a
two-dimensional matrix), one that uses a cv::Rect to specify a rectangular subregion
(which also works only on a two-dimensional matrix), and a final one that takes an
array of ranges. In this latter case, the number of valid ranges pointed to by the
pointer argument ranges must be equal to the number of dimensions of the array
mat.
 








#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char** argv)
{

	cv::Mat m(4, 5, CV_32FC3, cv::Scalar(100, 0.0f, 1.240f));
	std::cout << m << std::endl;
	std::cout << "*********************************" << std::endl;

	Mat subMat(m, Range(0, 2), Range(0, 3));
	std::cout << subMat << std::endl;
	std::cout << "*********************************" << std::endl;

	Mat roiMat(m, Rect(Point(0, 0), Point(2, 1)));
	std::cout << roiMat << std::endl;
	std::cout << "*********************************" << std::endl;
	//getchar();
	system("pause");
		
	return 0;
}

Static functions that create cv::Mat

The class cv::Mat also provides a number of static member functions to create certain kinds of commonly used arrays. 靜態函數

These include functions like zeros(), ones(), and eye(), which construct a matrix full of zeros, a matrix full of ones, or an identity matrix, respectively.

In the case of cv::Mat::eye() and cv::Mat::ones(), if the array created is multichannel, only the first channel will be set to 1.0, while the other channels will be 0.0.



 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章