OpenCV-learning process 3-Mat objects and related methods (how to initialize images, copy images, get pixel intensity values, etc.)

The OPENCV series of blogs mainly record the process of learning OPENCV and store the implemented code for subsequent review. The code contains the main remarks.

 How to use the Mat type

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char**argv) {
	
	//Mat1. 创建Mat对象,并且从文件中读取图片显示
	Mat src;
	src = imread("sample.jpg");

	if (src.empty()) {
		cout << "The image is empty,plz check the file route..." << endl;
		return -1;
	}
	namedWindow("Original_image", CV_WINDOW_AUTOSIZE);
	imshow("Original_image",src);

	//Mat2. 复制图像1: 创建Mat对象,复制src对象的尺寸,并且赋值给指定颜色(指定尺寸的纯色图)
	Mat dst;
	dst = Mat(src.size(), src.type());//设置图片尺寸,图片类型
	dst = Scalar(127,0,255);		  //设置纯色图片
	namedWindow("Test1", CV_WINDOW_AUTOSIZE);
	imshow("Empty_Image_test", dst);
	

	//Mat3. 复制图像2: 使用src.clone指令,复制已有图片到新建的图片中
	Mat dst = src.clone();
	namedWindow("Test1", CV_WINDOW_AUTOSIZE);
	imshow("Empty_Image_test", dst); 

	//Mat4. 复制图像3:  src.copyTo指令,将图片复制到dst中
	Mat dst;
	src.copyTo(dst);
	namedWindow("Test1", CV_WINDOW_AUTOSIZE);
	imshow("Empty_Image_test", dst); 


	//Mat5. 复制图像4: 在复制图像的同时,修改图像的格式
	Mat dst;
	cvtColor(src, dst, CV_BGR2GRAY);
	namedWindow("Test1", CV_WINDOW_AUTOSIZE);
	imshow("Empty_Image_test", dst); 
	printf("The input image channel is : %d \n",src.channels());
	printf("The output image channel is : %d \n", dst.channels());


	//Mat6. 显示图像信息(行,列,像素强度)
	Mat dst;
	cvtColor(src, dst, CV_BGR2GRAY);
	namedWindow("Test1", CV_WINDOW_AUTOSIZE);
	imshow("Empty_Image_test", dst);

	int cols = dst.cols;					//获取行和列的信息
	int rows = src.rows;
	printf("The row number of the image is : %d \n", cols);
	printf("The column number of the image is  : %d \n", rows);

	const uchar* firstrow_strangth = dst.ptr<uchar>(0);     //!!! 使用指针,取出第一行第一个元素的强度值
	printf("The strength is  : %d \n", *firstrow_strangth); //!!! 


	//Mat7.Mat 构造函数-创建一个用于测试的数值矩阵
	//重要!! 这个方法可以用于进行程序测试,运行处理程序,检查每个元素的强度是否与设定的一致; 之后再使用真实图片测试,这样比较直观。

	Mat M(3,3,CV_8UC3,Scalar(0,0,255)); //(3x3;每个通道8位,U表示无符号,C表示char类型,3表示通道数是3;每个像素的值是多少(0,0,255))
	cout << "M=" << endl << M << endl;

	//Mat8.Mat 定义一个小数组----掩膜,并用掩膜进行滤波-提高对比度
	Mat csrc;
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	filter2D(src, csrc, -1, kernel);
	namedWindow("csrc", CV_WINDOW_AUTOSIZE);
	imshow("csrc", csrc);

	//Mat9.Mat Trick: 创建一个与给定图片尺寸一致的图片:纯黑色
	//Mat dst = Mat::zeros(src.size(),src.type());
	Mat dst = Mat::eye(2,2,CV_8UC1);
	namedWindow("black", CV_WINDOW_AUTOSIZE);
	imshow("black", dst); 



	waitKey();
	return 0;
}

When the above code is running, you need to comment out other unnecessary content, and the running effect is as follows:

->How to use the Mat pointer to get a specific pixel value

Common form:
mat.ptr<type>(row)[col]

For the ptr function of Mat, it returns the template type pointer in <>, which points to the starting point of the row row in (),
usually the type in <> The element type of Mat should be the same,
and then use the pointer to access the element corresponding to the position of the col column. The

following is reproduced from: https://blog.csdn.net/github_35160620/article/details/51708659

single-channel
cv::Mat image = cv ::Mat(400, 600, CV_8UC1); //Defines a Mat variable image.
uchar * data00 = image.ptr<uchar>(0); //data00 is a pointer to the first element of the first line of image.
uchar * data10 = image.ptr<uchar>(1); //data10 is a pointer to the first element of the second row of the image.
uchar * data01 = image.ptr<uchar>(0)[1];//data01 is a pointer to the second element of the first row of the image.

Multi-channel
cv::Mat image = cv::Mat(400, 600, CV_8UC3); //width 400, length 600, 3-channel color image
cv::Vec3b * data000 = image.ptr<cv::Vec3b>(0 );
cv::Vec3b * data100 = image.ptr<cv::Vec3b>(1);
cv :: Vec3b * data001 = image.ptr <cv :: Vec3b> (0) [1];

cv :: Vec3b * data

Guess you like

Origin blog.csdn.net/weixin_42503785/article/details/113909033