OpenCV中6种访问Mat元素的方法
生活随笔
收集整理的這篇文章主要介紹了
OpenCV中6种访问Mat元素的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Mat中不管是以at訪問還是ptr訪問,都是行優(yōu)先 ,先Y軸后X軸(即先行后列)
1、使用at訪問
/* *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 ; }2、使用ptr訪問
/* *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 ; }3、迭代器訪問
/* *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 ; }4、data操作
/* *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 ; }5、row , col操作
#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)) ;//設(shè)定第i行數(shù)據(jù)image.col(i).setTo(Scalar(0 , 0 , 0)) ;//設(shè)定第i列數(shù)據(jù)}imshow("image" , image) ;waitKey(0) ;return 0 ; }6、高效訪問
#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 ; }轉(zhuǎn)自:http://lib.csdn.net/article/opencv/28174
總結(jié)
以上是生活随笔為你收集整理的OpenCV中6种访问Mat元素的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fatal error C1900: I
- 下一篇: VS2013+opencv2.4.9配置