第一种方法:
从global memory加载数据时coalesce
#include "cuda_runtime.h"
#include <cuda.h>
#include <time.h>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/cudawarping.hpp>
#include "opencv2/highgui.hpp"
#include <iostream>
#include <math.h>
#include <chrono>
// using namespace cv;
// using namespace std;
// enlarge k times in x and y direction
__global__ void inter_nearest_k(uchar3 *dataIn, uchar3 *dataOut, int imgHeight, int imgWidth, int k)
{
int xIndex = threadIdx.x + blockIdx.x * blockDim.x;
int yIndex = threadIdx.y + blockIdx.y * blockDim.y;
if(xIndex < imgWidth && yIndex < imgHeight)
{
uchar3 rgb = dataIn[yIndex * imgWidth + xIndex];
for(int j = 0; j < k; j++) // y direction
{
for(int i = 0; i < k; i++) // x direction
{
dataOut[(k * yIndex + j) * k * imgWidth + k * xIndex + i] = rgb;
}
}
}
}
int main(void)
{
int k = 11;
cv::Mat img_ori = cv::imread("lisfan-70.jpg");
int imgWidth = img_ori.cols;
int imgHeight = img_ori.rows;
std::cout << "img_ori.cols: " << img_ori.cols << std::endl; //列
std::cout << "img_ori.rows: " << img_ori.rows << std::endl; //行
cv::Mat img_resize_cpu;
auto start_cpu = std::chrono::steady_clock::now();
resize(img_ori, img_resize_cpu, cv::Size(k*img_ori.cols, k*img_ori.rows), 0, 0, cv::INTER_NEAREST);
auto end_cpu = std::chrono::steady_clock::now();
std:

本文介绍了两种使用CUDA优化OpenCV最近邻插值的方法,针对图像宽高分别放大k倍的情况。第一种方法着重于优化从全局内存加载数据时的coalescing,而第二种方法则是在写入全局内存时实现coalescing,由于数据量增大,后者的优化效果更显著。CMakeLists.txt文件也提供了相应的配置指导。
最低0.47元/天 解锁文章
5597

被折叠的 条评论
为什么被折叠?



