opencv中Mat格式的数据点访问at

来源:互联网 发布:mac版本ug 编辑:程序博客网 时间:2024/06/07 23:36


发现Mat类中的at方法对于获取图像矩阵某点的RGB值或者改变某点的值很方便,对于单通道的图像,则可以使用:

[cpp] view plain copy
  1. image.at<uchar>(i, j)

来获取或改变该点的值,而RGB通道的则可以使用:

[cpp] view plain copy
  1. image.at<Vec3b>(i, j)[0]  
  2. image.at<Vec3b>(i, j)[1]  
  3. image.at<Vec3b>(i, j)[2]

来分别获取B、G、R三个通道的对应的值。下边的代码实现对图像加椒盐噪声:

[cpp] view plain copy
  1. #include<opencv2\opencv.hpp>  
  2. using namespace cv;  
  3. using namespace std;  
  4.   
  5. void salt_noise(Mat image, int time)  
  6. {  
  7.     for (int k = 0; k < time; k++)//time is the number of the noise you add  
  8.     {  
  9.         int i = rand() % image.rows;  
  10.         int j = rand() % image.cols;  
  11.         if (image.channels() == 1)//single channel  
  12.         {  
  13.             image.at<uchar>(i, j) = rand() % 255;  
  14.         }  
  15.         else if (image.channels() == 3)//RGB channel  
  16.         {  
  17.             image.at<Vec3b>(i, j)[0] = rand() % 255;  
  18.             image.at<Vec3b>(i, j)[1] = rand() % 255;  
  19.             image.at<Vec3b>(i, j)[2] = rand() % 255;  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. int main(void)  
  25. {  
  26.     Mat image = imread("..\\lena.bmp", 0);  
  27.     if (image.empty())  
  28.     {  
  29.         cout << "load image error" << endl;  
  30.         return -1;  
  31.     }  
  32.     salt_noise(image, 3000);  
  33.     namedWindow("image", 1);  
  34.     imshow("image", image);  
  35.   
  36.     waitKey();  
  37.     return 0;  
  38. }

不过貌似用at取值或改变值来做比较耗时,当然我们还可以使用Mat的模板子类Mat_<T>,,对于单通道的具体使用:

[cpp] view plain copy
  1. Mat_<uchar> img = image;  
  2. img(i, j) = rand() % 255;
 对于RGB通道的使用:

[cpp] view plain copy
  1. Mat_<Vec3b> img = image;  
  2. img(i, j)[0] = rand() % 255;  
  3. img(i, j)[1] = rand() % 255;  
  4. mg(i, j)[2] = rand() % 255;

还可以用指针的方法遍历每一像素:(耗时较小)

[cpp] view plain copy
  1. void colorReduce(Mat image, int div = 64)  
  2. {  
  3.     int nrow = image.rows;  
  4.     int ncol = image.cols*image.channels();  
  5.     for (int i = 0; i < nrow; i++)  
  6.     {  
  7.         uchar* data = image.ptr<uchar>(i);//get the address of row i;  
  8.         for (int j = 0; j < ncol; j++)  
  9.         {  
  10.             data[i] = (data[i] / div)*div ;  
  11.         }  
  12.     }  
  13. }

0 0
原创粉丝点击