opencv获取图像像素值的两种方法

在前面的一篇博文中的代码中已经用到了一种方法:

        for ( int i = 0; i < gray_diff->height; ++i ) {
      //遍历整个gray_diff图像,其imageData成员是该图像的首地址,widthStep是一行占的字节数
      //则这里就是指向了该图像的第i行
            uchar * pucPixel = (uchar*)gray_diff->imageData + i*gray_diff->widthStep;
            for ( int j = 0; j < gray_diff->width; ++j ) {   
       //pucPixel[j]即为该点像素值,可进行操作
            ...
       ...
            }
        }

最近在学习过程中又看到一种方法:

  for (int i = 0; i < img->height; ++i) {
        for (int j = 0; j < img->width; ++j) {
            CvScalar scalar = cvGet2D(img, i, j);
            //scalar.val[0]、scalar.val[1]scalar.val[2]分别代表该点的BGR值,可进行操作
       ...
       ...
cvSet2D(img, i, j, scalar); } }

这里的img是IplImage类型,最后别忘记cvSet2D()

这两种方法都能对图像中的像素点进行读取与操作并写入,就是不知道有什么异同呢?

原文地址:https://www.cnblogs.com/Malo_Fleur/p/5742179.html