您的位置:首页 > 运维架构

《opencv入门教程》(迭代器遍历)

2015-07-08 20:27 357 查看
#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
Mat grayim(600,800,CV_8UC1);
Mat colorim(600, 800, CV_8UC3);

MatIterator_<uchar> graybegin, grayend;
MatIterator_<Vec3b> colorbegin, colorend;

for (graybegin = grayim.begin<uchar>(), grayend = grayim.end<uchar>();
graybegin != grayend; ++graybegin){

*graybegin = rand() % 255;
}

//遍历所有的像素,设置像素值

for (colorbegin = colorim.begin<Vec3b>(), colorend = colorim.end<Vec3b>();
colorbegin != colorend; ++colorbegin){

(*colorbegin)[0] = rand() % 255;
(*colorbegin)[1] = rand() % 255;
(*colorbegin)[2] = rand() % 255;

}

imshow("image1", grayim);
imshow("image2", colorim);
waitKey(0);

}

注意迭代器绑定的时候:
graybegin = grayim.begin<uchar>(), grayend = grayim.end<uchar>()
<pre name="code" class="cpp">colorbegin = colorim.begin<Vec3b>(), colorend = colorim.end<Vec3b>()




注意是有类型说明的,或者uchar或者Vec3b
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: