opencv3 Mat类的成员函数forEach

#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
typedef Point3_<uint8_t> Pixel;
void complicatedThreshold(Pixel &pixel)
{
	if (pow(double(pixel.x) / 10, 2.5) > 100)
	{
		pixel.x = 255;
		pixel.y = 255;
		pixel.z = 255;
	}
	else
	{
		pixel.x = 0;
		pixel.y = 0;
		pixel.z = 0;
	}
}
struct Operator
{
	void operator ()(Pixel &pixel, const int * position) const
	{
		// Perform a simple threshold operation
		complicatedThreshold(pixel);
	}
};
int main()
{
	Mat image = imread("C:/Users/Administrator/Desktop/timg.jpg");
	resize(image, image, Size(1920, 1920));

	clock_t t1 = clock();
	for (int r = 0; r < image.rows; r++)
	{
		for (int c = 0; c < image.cols; c++)
		{
			Pixel pixel = image.at<Pixel>(r, c);
			complicatedThreshold(pixel);
			image.at<Pixel>(r, c) = pixel;
		}
	}
	clock_t t2 = clock();
	cout << t2 - t1 << "ms" << endl;

	/***********************************************/
	clock_t t3 = clock();
	Pixel* pixel = image.ptr<Pixel>(0, 0);
	const Pixel* endPixel = pixel + image.cols * image.rows;
	for (; pixel != endPixel; pixel++)
	{
		complicatedThreshold(*pixel);
	}
	clock_t t4 = clock();
	cout << t4 - t3 << "ms" << endl;
	/***********************************************/
	clock_t t5 = clock();
	image.forEach<Pixel>(Operator());
	clock_t t6 = clock();
	cout << t6 - t5 << "ms" << endl;
	/***********************************************/
	clock_t t7 = clock();
	image.forEach<Pixel>
		(
			[](Pixel &pixel, const int * position) -> void
	{
		complicatedThreshold(pixel);
	}
	);
	clock_t t8 = clock();
	cout << t8 - t7 << "ms" << endl;
	/***********************************************/

	waitKey();
	return 0;
}
对比之下,可以发现forEach的速度很快,因为其里面用了ParallelBody
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值