opencv第三课 Mat对象使用
传送门:opencv第二课传送门
传送门:opencv第4课图像读写
目录
程序源码
今天学习Mat对象的使用,还是比较简单的:
先上代码吧:
// opencv_00003.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src;
src = imread("D:/image/2.jpg");
if (src.empty())
{
cout << "could not load image..." << endl;
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
//创建的第一个
Mat dst;
dst = Mat(src.size(),src.type());
dst = Scalar(127,0,255);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", dst);
//创建第二个
Mat img = src.clone();
//第二种方式
//src.copyTo(img);
namedWindow("output1", CV_WINDOW_AUTOSIZE);
imshow("output1", img);
Mat dstone;
cvtColor(src, dstone, CV_BGR2GRAY);
cout << "input image chanels: " << src.channels() << endl;
cout << "input image chanels: " << dstone.channels() << endl;
int col = dstone.cols;
int row = dstone.rows;
cout << "rows : " << row << " cols : " << col << endl;
const uchar * firstRow = dstone.ptr<uchar>(0);
cout << "first pixel value : " << *firstRow << endl;
Mat M(10,10,CV_8UC3,Scalar(0,0,255));
cout << "M =" << endl << M << endl;
namedWindow("output2", CV_WINDOW_AUTOSIZE);
imshow("output2", M);
Mat m1;
m1.create(src.size(), src.type());
m1 = Scalar(0,0,255);
Mat m2 = (Mat_<char>(3,3) << 0, -1, 0, -1, 5,-1, 0, -1, 0);
Mat m3;
int a = src.depth();
cout << "depth " << a << endl;
filter2D(src, m3, src.depth(), m2);
namedWindow("output3", CV_WINDOW_AUTOSIZE);
imshow("output3", m3);
waitKey(0);
return 0;
}
结果:
input image chanels: 3
input image chanels: 1
rows : 400 cols : 400
first pixel value :
M =
[ 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255]
depth 0
同时出了几个图,下面我们来分析一下;
代码分析:
Mat src;
src = imread("D:/image/2.jpg");
if (src.empty())
{
cout << "could not load image..." << endl;
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
最常规的一个读图片的 结果:
//创建的第一个
Mat dst;
dst = Mat(src.size(),src.type());
dst = Scalar(127,0,255);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", dst);
这里讲一下 Scalar(127,0,255) 复制像素的值,本图片是RGB形式,所以是3个参数。
//创建第二个
Mat img = src.clone();
//第二种方式
//src.copyTo(img);
namedWindow("output1", CV_WINDOW_AUTOSIZE);
imshow("output1", img);
本例子使用的是克隆的方式复制图片数据到Mat中,显示出来的图片与第一张相同;
Mat dstone;
cvtColor(src, dstone, CV_BGR2GRAY);
cout << "input image chanels: " << src.channels() << endl;
cout << "input image chanels: " << dstone.channels() << endl;
int col = dstone.cols;
int row = dstone.rows;
cout << "rows : " << row << " cols : " << col << endl;
const uchar * firstRow = dstone.ptr<uchar>(0);
cout << "first pixel value : " << (int)*firstRow << endl;
这部分分别求原图与变成灰色图的通道数,以及灰色图像的行与列,并且输出第一个数组的值:
at M(10,10,CV_8UC3,Scalar(0,0,255));
cout << "M =" << endl << M << endl;
namedWindow("output2", CV_WINDOW_AUTOSIZE);
imshow("output2", M);
这部分代码输出的是:
就是 10 * 10 的一个红色像素图像,这里M(10,10,CV_8UC3,Scalar(0,0,255));
其中前两个参数分别表示行(row)跟列(column)、第三个CV_8UC3中的8表示每个通道占8位、U表示无符号、C表示Char类型、3表示通道数目是3,第四个参数是向量表示初始化每个像素值是多少,向量长度对应通道数目一致。
Mat m1;
m1.create(src.size(), src.type());
m1 = Scalar(0,0,255);
Mat m2 = (Mat_<char>(3,3) << 0, -1, 0, -1, 5,-1, 0, -1, 0);
Mat m3;
int a = src.depth();
cout << "depth " << a << endl;
filter2D(src, m3, src.depth(), m2);
namedWindow("output3", CV_WINDOW_AUTOSIZE);
imshow("output3", m3);
waitKey(0);
这部分是通过create来创造一个Mat,filter2D(src, m3, src.depth(), m2) 这个是增加对比度的图像,上一课讲述过的:
是不是非常的简单,哈哈哈,你学会了吗。 喜欢的同学可以关注我的博客,一起学习,加油!