opencv中Mat究竟是什么?

opencv中Mat究竟是什么?(试着运行一下两个代码)

首先看opencv是怎么显示一张图的 :

//#include "stdafx.h"

#include 

#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	// Read the image file
	Mat image1 = imread("hero.jpg");//注意,图片需要在绝对路径

	if (image1.empty()) // Check for failure
	{
		cout << "Could not open or find the image" << endl;
		system("pause"); //wait for any key press
		return -1;
	}
	//Mat image1 (10,30,CV_8UC3,Scalar(155,175,131));//画一个高10 宽30的窗口,8*3bit的3通道图像,Scalar是定义颜色的函数;

	//cout << "image1 = " << endl << " " << image1 << endl << endl;//同时输出Mat所代表的矩阵
	String windowName1 = "My HelloWorld Window"; //Name of the window
	
	namedWindow(windowName1); // Create a window

	imshow(windowName1, image1); // Show our image inside the created window.

	waitKey(0); // Wait for any keystroke in the window

	destroyWindow(windowName1); //destroy the created window

	return 0;
}

以下是重点,代码就改了一点点:

//#include "stdafx.h"

#include 

#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	// Read the image file
	//Mat image1 = imread("hero.jpg");//注意,图片需要在绝对路径

	//if (image1.empty()) // Check for failure
	//{
	//	cout << "Could not open or find the image" << endl;
	//	system("pause"); //wait for any key press
	//	return -1;
	//}
	Mat image1 (10,30,CV_8UC3,Scalar(155,175,131));//画一个高10 宽30的窗口,8*3bit的3通道图像,Scalar是定义颜色的函数;

	cout << "image1 = " << endl << " " << image1 << endl << endl;//同时输出Mat所代表的矩阵
	String windowName1 = "My HelloWorld Window"; //Name of the window
	
	namedWindow(windowName1); // Create a window

	imshow(windowName1, image1); // Show our image inside the created window.

	waitKey(0); // Wait for any keystroke in the window

	destroyWindow(windowName1); //destroy the created window

	return 0;
}

把这两个代码在VS中运行一遍,你会发现,Mat是一个矩阵,数字都是像素值。

PS.代码中图片位置要更改成你们自己的路径,运行完是不是豁然开朗,hhh

你可能感兴趣的:(编程,OpenCV,opencv,C++)