#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