linux에서 Opencl를 지원하는 Opencv forandroid를 컴파일합니다

5159 단어
주요 단계는 다른 사람이 이미 썼으니 이 편을 참고하세요.https://www.cnblogs.com/hrlnw/p/4720977.html
작업의 세부 사항은 첨부된 pdf:https://files.cnblogs.com/files/ahfuzhang/opencvwithopencl4androidndk-141129030940-conversion-gate02.pdf.zip
테스트에 사용할 코드는 다음과 같습니다.
//jpg2gary.cpp
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifndef P
#define P(format, ...)                                                    \
    do {                                                                  \
        printf("%s %s %d " format "
", __FILE__, __FUNCTION__, __LINE__, \ ##__VA_ARGS__); \ fflush(stdout); \ } while (0); #endif void cpu(const char* img, int times) { cv::Mat image = cv::imread(img, cv::IMREAD_UNCHANGED); cv::Mat out; struct timeval start, end; gettimeofday(&start, NULL); for (int i = 0; i < times; i++) { cv::cvtColor(image, out, cv::COLOR_BGR2GRAY); } gettimeofday(&end, NULL); P("run times:%d, spend:%d ms", times, (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000); } void opencl(const char* img, int times) { cv::Mat image = cv::imread(img, cv::IMREAD_UNCHANGED); //cv::UMat u_img = image.getUMat(cv::ACCESS_READ); cv::UMat u_img; image.copyTo(u_img); cv::UMat out; struct timeval start, end; gettimeofday(&start, NULL); for (int i = 0; i < times; i++) { cv::cvtColor(u_img, out, cv::COLOR_BGR2GRAY); } gettimeofday(&end, NULL); P("run times:%d, spend:%d ms", times, (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000); } int init_col(){ cv::ocl::setUseOpenCL(true); if (!cv::ocl::haveOpenCL()) { P("OpenCL is not available..."); return -1; } cv::ocl::Context context; if (!context.create(cv::ocl::Device::TYPE_GPU)) { P("Failed creating the context..."); return -1; } std::vector<:ocl::platforminfo> platform_info; cv::ocl::getPlatfomsInfo(platform_info); for (int i = 0; i < platform_info.size(); i++) { cv::ocl::PlatformInfo sdk = platform_info.at(i); for (int j = 0; j < sdk.deviceNumber(); j++) { cv::ocl::Device device; sdk.getDevice(device, j); std::cout << "

*********************
Device " << i + 1 << std::endl; std::cout << "Vendor ID: " << device.vendorID() << std::endl; std::cout << "Vendor name: " << device.vendorName() << std::endl; std::cout << "Name: " << device.name() << std::endl; std::cout << "Driver version: " << device.driverVersion() << std::endl; std::cout << "available: " << device.available() << std::endl; if (device.isAMD()) std::cout << "Is an AMD device" << std::endl; if (device.isIntel()) std::cout << "Is a Intel device" << std::endl; std::cout << "Global Memory size: " << device.globalMemSize() << std::endl; std::cout << "Memory cache size: " << device.globalMemCacheSize() << std::endl; std::cout << "Memory cache type: " << device.globalMemCacheType() << std::endl; std::cout << "Local Memory size: " << device.localMemSize() << std::endl; std::cout << "Local Memory type: " << device.localMemType() << std::endl; std::cout << "Max Clock frequency: " << device.maxClockFrequency() << std::endl; } } if (!cv::ocl::haveOpenCL()) { P("OpenCL is not available, again..."); return -1; } cv::ocl::Device(context.device(0)); return 0; } int main(int argc, char* argv[]) { if (argc < 3) { printf("usage:%s [times=1]
", argv[0]); return 0; } int times = 1; if (argc >= 4) { times = atoi(argv[3]); } if (strcmp(argv[2], "cpu") == 0) { cpu(argv[1], times); } else if (strcmp(argv[2], "opencl") == 0) { if (0!=init_col()){ return 1; } opencl(argv[1], times); } else { P("unknow cpu/opencl"); return 0; } return 1; }

  
xiaomi mix 2s, 퀄컴 스냅드래곤 845, GPU Adreno 630을 사용하여 1080*1443 사이즈의 그림을 cvtColor로 RGB를 그레이스케일로 변환합니다.1000회 연속: CPU 595ms OpenCL 96ms
6.2배 가속!
 
 
전재 대상:https://www.cnblogs.com/ahfuzhang/p/11071865.html

좋은 웹페이지 즐겨찾기