如何用指针访问opencv cv::Mat数据?ptr<uchar>()
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用指针访问opencv cv::Mat数据?ptr<uchar>()相关的知识,希望对你有一定的参考价值。
示例:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
Mat src, dst1;
src = imread("./test.jpg");
//if (src.empty())
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);
//通过指针访问像素数值
//(0)表示第0行首像素地址,(1)表示第1行首像素地址,(0,0)表示第0行首像素地址,(0,1)表示第0行1像素地址
//const uchar* firstRow = dst1.ptr<uchar>(0); //可以
//const uchar* firstRow = dst1.ptr<uchar>(0, 0); //可以
//const uchar* firstRow = dst1.ptr<uchar>(0, 0, 0); //不行
const uchar* firstRow = dst1.ptr<uchar>(0, 0) + 1;
printf("first pixel %p value:%d\\n", firstRow, *firstRow); //first pixel 000001FDC64700C1 value:0
const uchar* secondRow = dst1.ptr<uchar>(0, 0) + 2;
printf("second pixel %p value:%d\\n", secondRow, *secondRow); //second pixel 000001FDC64700C2 value:255
const uchar* thirdRow = dst1.ptr<uchar>(0, 0) + 3;
printf("third pixel %p value:%d\\n", thirdRow, *thirdRow); //third pixel 000001FDC64700C3 value:127
const uchar* forthRow = dst1.ptr<uchar>(0, 0) + 4;
printf("forth pixel %p value:%d\\n", forthRow, *forthRow); //forth pixel 000001FDC64700C4 value:0
waitKey(0);
return 0;
参考文章1:【opencv4】opencv视频教程 C++(opencv教程)3、矩阵的掩膜操作(filter2D)
以上是关于如何用指针访问opencv cv::Mat数据?ptr<uchar>()的主要内容,如果未能解决你的问题,请参考以下文章