如何用指针访问opencv cv::Mat数据?ptr<uchar>()
生活随笔
收集整理的這篇文章主要介紹了
如何用指针访问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:0const uchar* secondRow = dst1.ptr<uchar>(0, 0) + 2;printf("second pixel %p value:%d\n", secondRow, *secondRow); //second pixel 000001FDC64700C2 value:255const uchar* thirdRow = dst1.ptr<uchar>(0, 0) + 3;printf("third pixel %p value:%d\n", thirdRow, *thirdRow); //third pixel 000001FDC64700C3 value:127const uchar* forthRow = dst1.ptr<uchar>(0, 0) + 4;printf("forth pixel %p value:%d\n", forthRow, *forthRow); //forth pixel 000001FDC64700C4 value:0waitKey(0);return 0; }參考文章1:【opencv4】opencv視頻教程 C++(opencv教程)3、矩陣的掩膜操作(filter2D)
參考文章2:訪問cv::Mat中的數據時遇到的指針類型問題
總結
以上是生活随笔為你收集整理的如何用指针访问opencv cv::Mat数据?ptr<uchar>()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【opencv4】opencv教程 C+
- 下一篇: 【opencv4】opencv视频教程