在OpenCV中图像都是由Mat结构体中存放并管理的,哪要如何访问其图像中元素呢?
下面将介绍6种访问图像元素的方法。
使用at访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* *OpenCV2中Mat的at操作访问矩阵元素 * */ #include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; //三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴) //第一种方法 for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { image.at<Vec3b>(h , w)[0] = 255 ; image.at<Vec3b>(h , w)[1] = 0 ; image.at<Vec3b>(h , w)[2] = 0 ; } } imshow("color1" , image) ; //方法二 for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { Vec3b &bgr = image.at<Vec3b>(h , w) ; bgr.val[0] = 0 ; bgr.val[1] = 255 ; bgr.val[2] = 0 ; } } imshow("color2" , image) ; image = imread("forest.jpg" , 0) ; //单通道图像,at(y , x)索引是先行(y轴) , 后列(x轴) for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { image.at<uchar>(h , w) = 128 ; } } imshow("gray" , image) ; waitKey(0) ; return 0 ; } |
使用ptr访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/* *OpenCV2中Mat操作ptr访问矩阵元素 * */ #include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; //三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴) //第一种方法 for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { uchar *ptr = image.ptr<uchar>(h , w) ; ptr[0] = 255 ; ptr[1] = 0 ; ptr[2] = 0 ; } } imshow("color1" , image) ; //第二种方法 for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { Vec3b *ptr = image.ptr<Vec3b>(h , w) ; ptr->val[0] = 0 ; ptr->val[1] = 255 ; ptr->val[2] = 0 ; } } imshow("color2" , image) ; image = imread("forest.jpg" , 0) ; //单通道图像,at(y , x)索引是先行(y轴) , 后列(x轴) //第一种方法 for(int h = 0 ; h < image.rows ; ++ h) { uchar *ptr = image.ptr(h) ; for(int w = 0 ; w < image.cols / 2 ; ++ w) { ptr[w] = 128 ; } } imshow("gray1" , image) ; for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols / 2 ; ++ w) { uchar *ptr = image.ptr<uchar>(h , w) ; *ptr = 255 ; } } imshow("gray2" , image) ; waitKey(0) ; return 0 ; } |
迭代器访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* *OpenCV2中Mat的迭代器访问Mat元素 * */ #include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; //三通道图像 Mat_<Vec3b>::iterator it = image.begin<Vec3b>() ; Mat_<Vec3b>::iterator itend = image.end<Vec3b>() ; for(;it != itend ; ++ it) { (*it)[0] = 255 ; (*it)[1] = 0 ; (*it)[2] = 0 ; } imshow("color1" , image) ; //单通道图像 image = imread("forest.jpg" , 0) ; Mat_<uchar>::iterator it1 = image.begin<uchar>() ; Mat_<uchar>::iterator itend1 = image.end<uchar>() ; for (;it1 != itend1 ; ++ it1) { (*it1) = 128 ; } imshow("gray" , image) ; waitKey(0) ; return 0 ; } |
data操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* *Mat中的data操作 */ #include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; //三通道 uchar *data = image.data ; for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols/2 ; ++ w) { *data ++ = 128 ; *data ++ = 128 ; *data ++ = 128 ; } } imshow("data" , image) ; //单通道 image = imread("forest.jpg" , 0) ; imshow("image" , image) ; data = image.data ; for(int h = 0 ; h < image.rows ; ++ h) { for(int w = 0 ; w < image.cols/2 ; ++ w) { *data ++ = 128 ; } } imshow("data1" , image) ; waitKey(0) ; return 0 ; } |
row , col操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; for(int i = 0 ; i < 100 ; ++ i) { image.row(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i行数据 image.col(i).setTo(Scalar(0 , 0 , 0)) ;//设定第i列数据 } imshow("image" , image) ; waitKey(0) ; return 0 ; } |
高效访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <highgui.h> using namespace std ; using namespace cv ; int main() { Mat image = imread("forest.jpg") ; imshow("image" , image) ; //单通道多通道都适用 int nRows = image.rows ; int nCols = image.cols * image.channels() ; if(image.isContinuous()) { nCols = nRows * nCols ; nRows = 1 ; } for(int h = 0 ; h < nRows ; ++ h) { uchar *ptr = image.ptr<uchar>(h) ; for(int w = 0 ; w < nCols ; ++ w) { //ptr[w] = 128 ; *ptr ++ = 128 ; } } imshow("high" , image) ; waitKey(0) ; return 0 ; } |