图像矩阵Mat和二维数组的互相转换

xiaoxiao2021-03-01  6

只要您配置了openCV库,那么直接拷贝下面这段代码,稍加修改读取图片路径即可马上运行。   

//2018.1.19_21:09 by Cooper Liu //Questions? Contact me: angelpoint@foxmail.com //本程序为C++工程,编译环境需要配置openCV #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> //#include<opencv2/opencv.hpp>//如果要包含所有库 using namespace cv; void main() { //这一段程序实现:图像img -> ptr[][] -> 图像img2 的传递过程,即图像->二维数组->图像 int i = 0, j = 0; Mat img = imread("F:\\photoCollection\\flowergirl.png", 0);//读取图像img。0表示转换为灰度图像读入 int row = img.rows; int col = img.cols; namedWindow("原图"); imshow("原图", img); Mat img2 = Mat(row, col, CV_8UC1);//图像img2:row*col大小 uchar **ptr = (uchar **)malloc(row*sizeof(uchar *));//二维数组ptr[][] for (i = 0; i < row; i++) ptr[i] = (uchar *)malloc(col*sizeof(uchar)); uchar *ptmp = NULL;//这是关键的指针!! for (i = 0; i < row; i++) { ptmp = img2.ptr<uchar>(i);//指针指向img2的第i行 for (j = 0; j < col; j++) { ptr[i][j] = img.at<uchar>(i, j);//img的矩阵数据传给二维数组ptr[][] ptmp[j] = ptr[i][j];//二维数组数据传给img2的第i行第j列 } } namedWindow("新图"); imshow("新图", img2); // 等待100000 ms后窗口自动关闭 waitKey(100000); }
转载请注明原文地址: https://www.6miu.com/read-3650130.html

最新回复(0)