opencv笔记3-图像对象的创建与赋值

图像对象的创建与赋值

#include
#include

using namespace std;
using namespace cv;

//图像对象的创建与赋值
void mat_creation_demo(Mat& image)
{
	Mat c1, c2;
	c1 = image.clone();//克隆
	image.copyTo(c2);//拷贝

	//创建空白图像
	Mat c3 = Mat::zeros(Size(8, 8), CV_8UC1);//CV_8UC1的1表示的是通道数
	//宽度:8    高度:8     通道数:1
	cout << "宽度:" << c3.cols << "  高度:" << c3.rows << "  通道数:" << c3.channels() << endl;
	c3 = 127;
	cout << c3 << endl;
	

	Mat c4 = Mat::zeros(Size(512, 512), CV_8UC3);
	//宽度:512    高度:512   通道数:3
	cout << "宽度:" << c4.cols << "  高度:" << c4.rows << "  通道数:" << c4.channels() << endl;
	c4 =Scalar (255,0,0);//给3个通道赋值
	//cout << c4 << endl;
	imshow("image2", c4);//显示图像

	//赋值操作:改变c5的同时c4也会被更改
	Mat c5 = c4;
	c5 = Scalar(0, 255, 0);
	imshow("image2", c4);//显示图像

	//克隆:改变c6不会更改c4
	Mat c6 = c4.clone();
	c6 = Scalar(255, 255, 0);
	imshow("image2", c4);//显示图像

}

int main()
{
	string path = "C:\\Users\\四明\\Pictures\\QQ图片20200613203609.jpg";
	Mat img = imread(path);
	if (img.empty())
	{
		cout << "加载图片失败" << endl;
		return -1;
	}
	namedWindow("image", WINDOW_FREERATIO);
	imshow("image", img);
	mat_creation_demo(img);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

你可能感兴趣的:(opencv笔记,笔记,c++,opencv)