上一讲:【opencv4】opencv视频教程 C++(opencv教程)3、矩阵的掩膜操作(filter2D)
[opencv_C++] 入门强推!!!【B站最全】
Mat对象
Mat对象与IplImage对象
Mat对象构造函数与常用方法
Mat对象使用(浅复制与深复制)
Mat对象使用-四个要点
Mat对象创建
定义小数组
演示代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[]) {
Mat src, dst1, dst2;
src = imread("./test.jpg");
if (!src.data){
printf("could not load image...\n");
return -1;
}
namedWindow("input img");
imshow("input img", src);
dst1 = Mat(src.size(), src.type());
dst1 = Scalar(127, 0, 255);
namedWindow("output img1");
imshow("output img1", dst1);
const uchar* firstRow = dst1.ptr<uchar>(0, 0) + 1;
printf("first pixel %p value:%d\n", firstRow, *firstRow);
const uchar* secondRow = dst1.ptr<uchar>(0, 0) + 2;
printf("second pixel %p value:%d\n", secondRow, *secondRow);
const uchar* thirdRow = dst1.ptr<uchar>(0, 0) + 3;
printf("third pixel %p value:%d\n", thirdRow, *thirdRow);
const uchar* forthRow = dst1.ptr<uchar>(0, 0) + 4;
printf("forth pixel %p value:%d\n", forthRow, *forthRow);
cvtColor(src,dst2, COLOR_BGR2GRAY);
namedWindow("output img2");
imshow("output img2", dst2);
Mat M(100, 100, CV_8UC1, Scalar(127));
namedWindow("output img3");
imshow("output img3", M);
Mat m1;
m1.create(src.size(), src.type());
m1 = Scalar(0, 0, 255);
namedWindow("output img4");
imshow("output img4", m1);
Mat z;
z = Mat::eye(2, 3, CV_8UC1);
cout << "z=" << endl << z << endl;
Mat csrc;
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, csrc, -1, kernel);
namedWindow("output img5");
imshow("output img5", csrc);
waitKey(0);
return 0;
}
VS编译运行结果:
|