#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
opencv3 Mat类的成员函数forEach
最新推荐文章于 2024-06-27 16:07:35 发布