本文共 1629 字,大约阅读时间需要 5 分钟。
#include#include #include using namespace cv;using namespace std;int main() { // 加载图像 Mat src = imread("E:\\vs2015\\opencvstudy\\2.jpg", 1); if (!src.data) { cout << "could not load image!" << endl; return -1; } // 初始化方法一 Mat dst; dst = Mat(src.size(), src.type()); dst = Scalar(127, 0, 255); imshow("outputImage", dst); // 初始化方法二 Mat dst_clone = src.clone(); imshow("clone_outputImage", dst_clone); // 初始化方法三 Mat dst_copy; src.copyTo(dst_copy); imshow("copyTo_outputImage", dst_copy); // 获取图像信息 Mat gray; cvtColor(src, gray, CV_BGR2GRAY); printf("input image channel:%d\n", src.channels()); printf("output image channel:%d\n", gray.channels()); int cols = gray.cols; int rows = gray.rows; cout << "cols:" << cols << endl; cout << "rows:" << rows << endl; const uchar* firstRow = gray.ptr (0); printf("firstRow pixel:%d", *firstRow); // 初始化方法四 Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255)); cout << "M:" << endl << M << endl; // 初始化方法五 Mat M2(100, 100, CV_8UC1, Scalar(127)); imshow("Mat2_初始化", M2); // 初始化方法六 Mat M3; M3.create(src.size(), src.type()); M3 = Scalar(0, 0, 255); imshow("Mat3_初始化", M3); // 创建小数组方法 Mat M4; M4 = Mat_ (3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0; filter2D(src, dst, -1, M4); imshow("dst", dst); // 初始化零矩阵 Mat M5 = Mat::zeros(src.size(), src.type()); Mat M6 = Mat::zeros(2, 2, src.type()); Mat M7 = Mat::eye(2, 2, src.type()); cout << M6 << endl; cout << M7 << endl; waitKey(0); return 0;}
转载地址:http://amsfk.baihongyu.com/