在学习OpenCV进行图像处理的过程中,常常会用到对指定位置像素的读取和修改,

最常用的就是cv::MAT.at<>()和cv::MAT.ptr<>(),下面我们就介绍OpenCV中这两种常用的对指定位置像素值的读取和修改操作。

一、at操作

at操作是一种直接简单的对单个像素的操作方式,用于获取图像矩阵某点的值或改变某点的值

对于灰度图读取和修改操作如下:
          uchar srcPixel_value = cv::Mat.at<uchar>(row, col);
          cv::Mat.at<uchar>(row, col) = srcPixel_value ; 


对于RGB彩色图像而言:

          cv::Vec3b rgb_value = cv::Mat.at<cv::Vec3b>(row, col);
          cv::Mat.at<cv::Vec3b>(row, col) = rgb_value ;

或者:

           image.at<cv::Vec3b>(row, col)[0] =  255;

           image.at<cv::Vec3b>(row, col)[1] =  255;

           image.at<cv::Vec3b>(row, col)[2] = 255;

例子:

  cv::Mat  image(3,10,CV_32FC3);
  int div = 4;
     for(int i=0; i<image.rows;i++)
     {
        for(int j=0; j<image.cols; j++)
        {
          image.at<Vec3f>(i,j)[0]=image.at<Vec3f>(i.j)[0] / div;
          image.at<Vec3f>(i,j)[1]=image.at<Vec3f>(i.j)[1] / div;
          image.at<Vec3f>(i,j)[2]=image.at<Vec3f>(i.j)[2] / div;
        }
     };

at操作虽然简单方便,但是这种操作是一种效率比较低的操作,尤其用其进行遍历操作时,效率会很低。因此一般不推荐使用。

 

二、ptr操作:

ptr操作时通过指针偏移的方式进行像素的查找、遍历和修改的,因此效率相对较高。

ptr<>()是按照行来访问数组的,指针指向某一行第一个元素的首地址。只要知道这一行的首地址,其余地址++就可以访问了。
对于灰度图的读取和修改操作如下:

uchar srcPixel_value = cv::Mat.ptr<uchar>(row)[col];
cv::Mat.ptr<uchar>(row)[col] = srcPixel_value ; 

对于RGB彩色图像而言:

cv::Vec3b rgb_value = Mat.ptr<cv::Vec3b>(row)[col];
cv::Mat.ptr<cv::Vec3b>(row)[col] = rgb_value ;

例子:

cv::Mat image (3,10,float);
int div = 4;
cv::Mat newimage(image.size(), image.type());
for(int i=0; i<image.rows; i++)
  {
      uchar *data=image.ptr<uchar>(i);
      uchar *outdata=newimage.ptr<uchar>(i);
     for(int j=0; j<image.cols; j++)
     {
       outdata[ j ]= data[ j ] /div;
     }
  };

cv::MAT.ptr<>()访问效率比较高,程序也比较安全,有越界判断。推荐使用。

GitHub 加速计划 / opencv31 / opencv
233
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
f5d6fe53 Docs(imgcodecs): clarify animation frame duration units #28176 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake Docs-only change. No code or tests affected. 9 小时前
748d6545 videoio(ios): fix NSInvalidArgumentException in VideoWriter release #28173 Summary Fixes a crash on iOS when calling `VideoWriter::release()` in OpenCV 4.12.0. Issue Fixes #28165 Detailed Description This PR resolves a regression where an `NSInvalidArgumentException` was thrown with the message ` -[NSAutoreleasePool retain]: Cannot retain an autorelease pool`. The crash was caused by the manual usage of `NSAutoreleasePool` in the `~CvVideoWriter_AVFoundation` destructor. The local pool variable was being captured by the completion handler block passed to `[mMovieWriter finishWritingWithCompletionHandler:]`. Since `NSAutoreleasePool` instances cannot be retained, capturing them in a block causes a crash. Changes: - Replaced manual `NSAutoreleasePool` allocation and draining with modern `@autoreleasepool` blocks in `CvVideoWriter_AVFoundation`. - applied this modernization to the Constructor, Destructor, and `write` methods. - Specifically in the destructor, an inner `@autoreleasepool` is now used inside the completion block, ensuring no pool object needs to be captured from the outer scope. 1 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐