在本教程中,我们将比较Mat 类的forEach方法与 OpenCV 中访问和转换像素值的其他方法的性能。我们将展示forEach如何比简单地使用at方法甚至有效地使用指针算术快得多。
OpenCV 内部有一些隐藏的宝石,有时并不为人所知。这些隐藏的宝石之一是Mat 类的forEach方法,它利用计算机上的所有核心在每个像素上应用任何函数。
让我们首先定义一个函数ComplexThreshold。它接收 RGB 像素值并对其应用复杂的阈值。
- // Define a pixel
- typedef Point3_<uint8_t> Pixel;
-
- // A complicated threshold is defined so
- // a non-trivial amount of computation
- // is done at each pixel.
- void complicatedThreshold(Pixel &pixel)
- {
- if (pow(double(pixel.x)/10,2.5) > 100)
- {
- pixel.x = 255;
- pixel.y =