linux에서 Opencl를 지원하는 Opencv forandroid를 컴파일합니다
작업의 세부 사항은 첨부된 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.