1. 程式人生 > >《opencv入門教程》(迭代器遍歷)

《opencv入門教程》(迭代器遍歷)

#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