重點在於 shader code 必須是固定的,如果 filter kernel 一直改變,估計在 runtime 重新 compile shader 的時間會成為瓶頸。
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
#include <time.h>
using namespace cv;
using namespace cv::ocl;
int main(int argc, char** argv)
{
setUseOpenCL(1);
printf("useCL %s\n", useOpenCL()?"true":"false");
Mat img = imread(argv[1]);
UMat mImgSource, mImgSource2, mImgResult, mImgResult2, mImgResult3;
img.copyTo(mImgSource);
int ksize = 200;
RNG rng;
while (true) {
clock_t t1 = clock();
add(mImgSource, UMat(mImgSource.size(), mImgSource.type(), Scalar( rng.uniform(0,50), rng.uniform(0,50), rng.uniform(0,50), rng.uniform(0,50) )), mImgSource2);
boxFilter(mImgSource2, mImgResult, -1, Size(ksize, ksize));
boxFilter(mImgResult, mImgResult2, -1, Size(ksize, ksize));
resize(mImgResult2, mImgResult3, Size(), 0.5, 0.5);
clock_t t2 = clock();
printf("%d %d\n", ksize, (int)(t2-t1));
imshow("img", mImgResult3);
int key = waitKey(1);
if (key > 0) break;
}
}


