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

来源:互联网 发布:js转化为字符串 编辑:程序博客网 时间:2024/06/08 07:20
#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


0 0
原创粉丝点击