mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-26 06:38:16 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -36,13 +36,14 @@
|
||||
/// \param[in] count vector size
|
||||
/// \param[out] sum result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void AddKernel(const float *op1, const float *op2, int count,
|
||||
float *sum) {
|
||||
const int pos = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
__global__ void AddKernel(const float *op1, const float *op2, int count, float *sum)
|
||||
{
|
||||
const int pos = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
|
||||
if (pos >= count) return;
|
||||
if (pos >= count)
|
||||
return;
|
||||
|
||||
sum[pos] = op1[pos] + op2[pos];
|
||||
sum[pos] = op1[pos] + op2[pos];
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -52,9 +53,10 @@ __global__ void AddKernel(const float *op1, const float *op2, int count,
|
||||
/// \param[in] count vector size
|
||||
/// \param[out] sum result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void Add(const float *op1, const float *op2, int count, float *sum) {
|
||||
dim3 threads(256);
|
||||
dim3 blocks(iDivUp(count, threads.x));
|
||||
static void Add(const float *op1, const float *op2, int count, float *sum)
|
||||
{
|
||||
dim3 threads(256);
|
||||
dim3 blocks(iDivUp(count, threads.x));
|
||||
|
||||
AddKernel<<<blocks, threads>>>(op1, op2, count, sum);
|
||||
AddKernel<<<blocks, threads>>>(op1, op2, count, sum);
|
||||
}
|
||||
|
||||
@@ -36,13 +36,12 @@
|
||||
// Common includes
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <memory.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <helper_cuda.h>
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Common constants
|
||||
@@ -54,23 +53,24 @@ const int StrideAlignment = 32;
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Align up n to the nearest multiple of m
|
||||
inline int iAlignUp(int n, int m = StrideAlignment) {
|
||||
int mod = n % m;
|
||||
inline int iAlignUp(int n, int m = StrideAlignment)
|
||||
{
|
||||
int mod = n % m;
|
||||
|
||||
if (mod)
|
||||
return n + m - mod;
|
||||
else
|
||||
return n;
|
||||
if (mod)
|
||||
return n + m - mod;
|
||||
else
|
||||
return n;
|
||||
}
|
||||
|
||||
// round up n/m
|
||||
inline int iDivUp(int n, int m) { return (n + m - 1) / m; }
|
||||
|
||||
// swap two values
|
||||
template <typename T>
|
||||
inline void Swap(T &a, T &b) {
|
||||
T t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
template <typename T> inline void Swap(T &a, T &b)
|
||||
{
|
||||
T t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -38,56 +38,62 @@
|
||||
/// \param[out] Iy y derivative
|
||||
/// \param[out] Iz temporal derivative
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void ComputeDerivativesKernel(int width, int height, int stride,
|
||||
float *Ix, float *Iy, float *Iz,
|
||||
__global__ void ComputeDerivativesKernel(int width,
|
||||
int height,
|
||||
int stride,
|
||||
float *Ix,
|
||||
float *Iy,
|
||||
float *Iz,
|
||||
cudaTextureObject_t texSource,
|
||||
cudaTextureObject_t texTarget) {
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
cudaTextureObject_t texTarget)
|
||||
{
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
const int pos = ix + iy * stride;
|
||||
const int pos = ix + iy * stride;
|
||||
|
||||
if (ix >= width || iy >= height) return;
|
||||
if (ix >= width || iy >= height)
|
||||
return;
|
||||
|
||||
float dx = 1.0f / (float)width;
|
||||
float dy = 1.0f / (float)height;
|
||||
float dx = 1.0f / (float)width;
|
||||
float dy = 1.0f / (float)height;
|
||||
|
||||
float x = ((float)ix + 0.5f) * dx;
|
||||
float y = ((float)iy + 0.5f) * dy;
|
||||
float x = ((float)ix + 0.5f) * dx;
|
||||
float y = ((float)iy + 0.5f) * dy;
|
||||
|
||||
float t0, t1;
|
||||
// x derivative
|
||||
t0 = tex2D<float>(texSource, x - 2.0f * dx, y);
|
||||
t0 -= tex2D<float>(texSource, x - 1.0f * dx, y) * 8.0f;
|
||||
t0 += tex2D<float>(texSource, x + 1.0f * dx, y) * 8.0f;
|
||||
t0 -= tex2D<float>(texSource, x + 2.0f * dx, y);
|
||||
t0 /= 12.0f;
|
||||
float t0, t1;
|
||||
// x derivative
|
||||
t0 = tex2D<float>(texSource, x - 2.0f * dx, y);
|
||||
t0 -= tex2D<float>(texSource, x - 1.0f * dx, y) * 8.0f;
|
||||
t0 += tex2D<float>(texSource, x + 1.0f * dx, y) * 8.0f;
|
||||
t0 -= tex2D<float>(texSource, x + 2.0f * dx, y);
|
||||
t0 /= 12.0f;
|
||||
|
||||
t1 = tex2D<float>(texTarget, x - 2.0f * dx, y);
|
||||
t1 -= tex2D<float>(texTarget, x - 1.0f * dx, y) * 8.0f;
|
||||
t1 += tex2D<float>(texTarget, x + 1.0f * dx, y) * 8.0f;
|
||||
t1 -= tex2D<float>(texTarget, x + 2.0f * dx, y);
|
||||
t1 /= 12.0f;
|
||||
t1 = tex2D<float>(texTarget, x - 2.0f * dx, y);
|
||||
t1 -= tex2D<float>(texTarget, x - 1.0f * dx, y) * 8.0f;
|
||||
t1 += tex2D<float>(texTarget, x + 1.0f * dx, y) * 8.0f;
|
||||
t1 -= tex2D<float>(texTarget, x + 2.0f * dx, y);
|
||||
t1 /= 12.0f;
|
||||
|
||||
Ix[pos] = (t0 + t1) * 0.5f;
|
||||
Ix[pos] = (t0 + t1) * 0.5f;
|
||||
|
||||
// t derivative
|
||||
Iz[pos] = tex2D<float>(texTarget, x, y) - tex2D<float>(texSource, x, y);
|
||||
// t derivative
|
||||
Iz[pos] = tex2D<float>(texTarget, x, y) - tex2D<float>(texSource, x, y);
|
||||
|
||||
// y derivative
|
||||
t0 = tex2D<float>(texSource, x, y - 2.0f * dy);
|
||||
t0 -= tex2D<float>(texSource, x, y - 1.0f * dy) * 8.0f;
|
||||
t0 += tex2D<float>(texSource, x, y + 1.0f * dy) * 8.0f;
|
||||
t0 -= tex2D<float>(texSource, x, y + 2.0f * dy);
|
||||
t0 /= 12.0f;
|
||||
// y derivative
|
||||
t0 = tex2D<float>(texSource, x, y - 2.0f * dy);
|
||||
t0 -= tex2D<float>(texSource, x, y - 1.0f * dy) * 8.0f;
|
||||
t0 += tex2D<float>(texSource, x, y + 1.0f * dy) * 8.0f;
|
||||
t0 -= tex2D<float>(texSource, x, y + 2.0f * dy);
|
||||
t0 /= 12.0f;
|
||||
|
||||
t1 = tex2D<float>(texTarget, x, y - 2.0f * dy);
|
||||
t1 -= tex2D<float>(texTarget, x, y - 1.0f * dy) * 8.0f;
|
||||
t1 += tex2D<float>(texTarget, x, y + 1.0f * dy) * 8.0f;
|
||||
t1 -= tex2D<float>(texTarget, x, y + 2.0f * dy);
|
||||
t1 /= 12.0f;
|
||||
t1 = tex2D<float>(texTarget, x, y - 2.0f * dy);
|
||||
t1 -= tex2D<float>(texTarget, x, y - 1.0f * dy) * 8.0f;
|
||||
t1 += tex2D<float>(texTarget, x, y + 1.0f * dy) * 8.0f;
|
||||
t1 -= tex2D<float>(texTarget, x, y + 2.0f * dy);
|
||||
t1 /= 12.0f;
|
||||
|
||||
Iy[pos] = (t0 + t1) * 0.5f;
|
||||
Iy[pos] = (t0 + t1) * 0.5f;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -102,43 +108,40 @@ __global__ void ComputeDerivativesKernel(int width, int height, int stride,
|
||||
/// \param[out] Iy y derivative
|
||||
/// \param[out] Iz temporal derivative
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void ComputeDerivatives(const float *I0, const float *I1, int w, int h,
|
||||
int s, float *Ix, float *Iy, float *Iz) {
|
||||
dim3 threads(32, 6);
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
static void ComputeDerivatives(const float *I0, const float *I1, int w, int h, int s, float *Ix, float *Iy, float *Iz)
|
||||
{
|
||||
dim3 threads(32, 6);
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
|
||||
cudaTextureObject_t texSource, texTarget;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
cudaTextureObject_t texSource, texTarget;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)I0;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)I0;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(
|
||||
cudaCreateTextureObject(&texSource, &texRes, &texDescr, NULL));
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)I1;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
checkCudaErrors(
|
||||
cudaCreateTextureObject(&texTarget, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texSource, &texRes, &texDescr, NULL));
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)I1;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
checkCudaErrors(cudaCreateTextureObject(&texTarget, &texRes, &texDescr, NULL));
|
||||
|
||||
ComputeDerivativesKernel<<<blocks, threads>>>(w, h, s, Ix, Iy, Iz, texSource,
|
||||
texTarget);
|
||||
ComputeDerivativesKernel<<<blocks, threads>>>(w, h, s, Ix, Iy, Iz, texSource, texTarget);
|
||||
}
|
||||
|
||||
@@ -36,25 +36,24 @@
|
||||
/// \param[in] stride image stride
|
||||
/// \param[out] out result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void DownscaleKernel(int width, int height, int stride, float *out,
|
||||
cudaTextureObject_t texFine) {
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
__global__ void DownscaleKernel(int width, int height, int stride, float *out, cudaTextureObject_t texFine)
|
||||
{
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
if (ix >= width || iy >= height) {
|
||||
return;
|
||||
}
|
||||
if (ix >= width || iy >= height) {
|
||||
return;
|
||||
}
|
||||
|
||||
float dx = 1.0f / (float)width;
|
||||
float dy = 1.0f / (float)height;
|
||||
float dx = 1.0f / (float)width;
|
||||
float dy = 1.0f / (float)height;
|
||||
|
||||
float x = ((float)ix + 0.5f) * dx;
|
||||
float y = ((float)iy + 0.5f) * dy;
|
||||
float x = ((float)ix + 0.5f) * dx;
|
||||
float y = ((float)iy + 0.5f) * dy;
|
||||
|
||||
out[ix + iy * stride] = 0.25f * (tex2D<float>(texFine, x - dx * 0.25f, y) +
|
||||
tex2D<float>(texFine, x + dx * 0.25f, y) +
|
||||
tex2D<float>(texFine, x, y - dy * 0.25f) +
|
||||
tex2D<float>(texFine, x, y + dy * 0.25f));
|
||||
out[ix + iy * stride] = 0.25f
|
||||
* (tex2D<float>(texFine, x - dx * 0.25f, y) + tex2D<float>(texFine, x + dx * 0.25f, y)
|
||||
+ tex2D<float>(texFine, x, y - dy * 0.25f) + tex2D<float>(texFine, x, y + dy * 0.25f));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -66,33 +65,33 @@ __global__ void DownscaleKernel(int width, int height, int stride, float *out,
|
||||
/// \param[in] stride image stride
|
||||
/// \param[out] out result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void Downscale(const float *src, int width, int height, int stride,
|
||||
int newWidth, int newHeight, int newStride, float *out) {
|
||||
dim3 threads(32, 8);
|
||||
dim3 blocks(iDivUp(newWidth, threads.x), iDivUp(newHeight, threads.y));
|
||||
static void
|
||||
Downscale(const float *src, int width, int height, int stride, int newWidth, int newHeight, int newStride, float *out)
|
||||
{
|
||||
dim3 threads(32, 8);
|
||||
dim3 blocks(iDivUp(newWidth, threads.x), iDivUp(newHeight, threads.y));
|
||||
|
||||
cudaTextureObject_t texFine;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
cudaTextureObject_t texFine;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = width;
|
||||
texRes.res.pitch2D.height = height;
|
||||
texRes.res.pitch2D.pitchInBytes = stride * sizeof(float);
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = width;
|
||||
texRes.res.pitch2D.height = height;
|
||||
texRes.res.pitch2D.pitchInBytes = stride * sizeof(float);
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texFine, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texFine, &texRes, &texDescr, NULL));
|
||||
|
||||
DownscaleKernel<<<blocks, threads>>>(newWidth, newHeight, newStride, out,
|
||||
texFine);
|
||||
DownscaleKernel<<<blocks, threads>>>(newWidth, newHeight, newStride, out, texFine);
|
||||
}
|
||||
|
||||
@@ -28,12 +28,12 @@
|
||||
#include "common.h"
|
||||
|
||||
// include kernels
|
||||
#include "addKernel.cuh"
|
||||
#include "derivativesKernel.cuh"
|
||||
#include "downscaleKernel.cuh"
|
||||
#include "solverKernel.cuh"
|
||||
#include "upscaleKernel.cuh"
|
||||
#include "warpingKernel.cuh"
|
||||
#include "derivativesKernel.cuh"
|
||||
#include "solverKernel.cuh"
|
||||
#include "addKernel.cuh"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \brief method logic
|
||||
@@ -51,166 +51,203 @@
|
||||
/// \param[out] u horizontal displacement
|
||||
/// \param[out] v vertical displacement
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void ComputeFlowCUDA(const float *I0, const float *I1, int width, int height,
|
||||
int stride, float alpha, int nLevels, int nWarpIters,
|
||||
int nSolverIters, float *u, float *v) {
|
||||
printf("Computing optical flow on GPU...\n");
|
||||
void ComputeFlowCUDA(const float *I0,
|
||||
const float *I1,
|
||||
int width,
|
||||
int height,
|
||||
int stride,
|
||||
float alpha,
|
||||
int nLevels,
|
||||
int nWarpIters,
|
||||
int nSolverIters,
|
||||
float *u,
|
||||
float *v)
|
||||
{
|
||||
printf("Computing optical flow on GPU...\n");
|
||||
|
||||
// pI0 and pI1 will hold device pointers
|
||||
const float **pI0 = new const float *[nLevels];
|
||||
const float **pI1 = new const float *[nLevels];
|
||||
// pI0 and pI1 will hold device pointers
|
||||
const float **pI0 = new const float *[nLevels];
|
||||
const float **pI1 = new const float *[nLevels];
|
||||
|
||||
int *pW = new int[nLevels];
|
||||
int *pH = new int[nLevels];
|
||||
int *pS = new int[nLevels];
|
||||
int *pW = new int[nLevels];
|
||||
int *pH = new int[nLevels];
|
||||
int *pS = new int[nLevels];
|
||||
|
||||
// device memory pointers
|
||||
float *d_tmp;
|
||||
float *d_du0;
|
||||
float *d_dv0;
|
||||
float *d_du1;
|
||||
float *d_dv1;
|
||||
// device memory pointers
|
||||
float *d_tmp;
|
||||
float *d_du0;
|
||||
float *d_dv0;
|
||||
float *d_du1;
|
||||
float *d_dv1;
|
||||
|
||||
float *d_Ix;
|
||||
float *d_Iy;
|
||||
float *d_Iz;
|
||||
float *d_Ix;
|
||||
float *d_Iy;
|
||||
float *d_Iz;
|
||||
|
||||
float *d_u;
|
||||
float *d_v;
|
||||
float *d_nu;
|
||||
float *d_nv;
|
||||
float *d_u;
|
||||
float *d_v;
|
||||
float *d_nu;
|
||||
float *d_nv;
|
||||
|
||||
const int dataSize = stride * height * sizeof(float);
|
||||
const int dataSize = stride * height * sizeof(float);
|
||||
|
||||
checkCudaErrors(cudaMalloc(&d_tmp, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_du0, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_dv0, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_du1, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_dv1, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_tmp, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_du0, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_dv0, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_du1, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_dv1, dataSize));
|
||||
|
||||
checkCudaErrors(cudaMalloc(&d_Ix, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_Iy, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_Iz, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_Ix, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_Iy, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_Iz, dataSize));
|
||||
|
||||
checkCudaErrors(cudaMalloc(&d_u, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_v, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_nu, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_nv, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_u, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_v, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_nu, dataSize));
|
||||
checkCudaErrors(cudaMalloc(&d_nv, dataSize));
|
||||
|
||||
// prepare pyramid
|
||||
// prepare pyramid
|
||||
|
||||
int currentLevel = nLevels - 1;
|
||||
// allocate GPU memory for input images
|
||||
checkCudaErrors(cudaMalloc(pI0 + currentLevel, dataSize));
|
||||
checkCudaErrors(cudaMalloc(pI1 + currentLevel, dataSize));
|
||||
int currentLevel = nLevels - 1;
|
||||
// allocate GPU memory for input images
|
||||
checkCudaErrors(cudaMalloc(pI0 + currentLevel, dataSize));
|
||||
checkCudaErrors(cudaMalloc(pI1 + currentLevel, dataSize));
|
||||
|
||||
checkCudaErrors(cudaMemcpy((void *)pI0[currentLevel], I0, dataSize,
|
||||
cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaMemcpy((void *)pI1[currentLevel], I1, dataSize,
|
||||
cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaMemcpy((void *)pI0[currentLevel], I0, dataSize, cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaMemcpy((void *)pI1[currentLevel], I1, dataSize, cudaMemcpyHostToDevice));
|
||||
|
||||
pW[currentLevel] = width;
|
||||
pH[currentLevel] = height;
|
||||
pS[currentLevel] = stride;
|
||||
pW[currentLevel] = width;
|
||||
pH[currentLevel] = height;
|
||||
pS[currentLevel] = stride;
|
||||
|
||||
for (; currentLevel > 0; --currentLevel) {
|
||||
int nw = pW[currentLevel] / 2;
|
||||
int nh = pH[currentLevel] / 2;
|
||||
int ns = iAlignUp(nw);
|
||||
for (; currentLevel > 0; --currentLevel) {
|
||||
int nw = pW[currentLevel] / 2;
|
||||
int nh = pH[currentLevel] / 2;
|
||||
int ns = iAlignUp(nw);
|
||||
|
||||
checkCudaErrors(
|
||||
cudaMalloc(pI0 + currentLevel - 1, ns * nh * sizeof(float)));
|
||||
checkCudaErrors(
|
||||
cudaMalloc(pI1 + currentLevel - 1, ns * nh * sizeof(float)));
|
||||
checkCudaErrors(cudaMalloc(pI0 + currentLevel - 1, ns * nh * sizeof(float)));
|
||||
checkCudaErrors(cudaMalloc(pI1 + currentLevel - 1, ns * nh * sizeof(float)));
|
||||
|
||||
Downscale(pI0[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], nw, nh, ns, (float *)pI0[currentLevel - 1]);
|
||||
Downscale(pI0[currentLevel],
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
nw,
|
||||
nh,
|
||||
ns,
|
||||
(float *)pI0[currentLevel - 1]);
|
||||
|
||||
Downscale(pI1[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], nw, nh, ns, (float *)pI1[currentLevel - 1]);
|
||||
Downscale(pI1[currentLevel],
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
nw,
|
||||
nh,
|
||||
ns,
|
||||
(float *)pI1[currentLevel - 1]);
|
||||
|
||||
pW[currentLevel - 1] = nw;
|
||||
pH[currentLevel - 1] = nh;
|
||||
pS[currentLevel - 1] = ns;
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMemset(d_u, 0, stride * height * sizeof(float)));
|
||||
checkCudaErrors(cudaMemset(d_v, 0, stride * height * sizeof(float)));
|
||||
|
||||
// compute flow
|
||||
for (; currentLevel < nLevels; ++currentLevel) {
|
||||
for (int warpIter = 0; warpIter < nWarpIters; ++warpIter) {
|
||||
checkCudaErrors(cudaMemset(d_du0, 0, dataSize));
|
||||
checkCudaErrors(cudaMemset(d_dv0, 0, dataSize));
|
||||
|
||||
checkCudaErrors(cudaMemset(d_du1, 0, dataSize));
|
||||
checkCudaErrors(cudaMemset(d_dv1, 0, dataSize));
|
||||
|
||||
// on current level we compute optical flow
|
||||
// between frame 0 and warped frame 1
|
||||
WarpImage(pI1[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], d_u, d_v, d_tmp);
|
||||
|
||||
ComputeDerivatives(pI0[currentLevel], d_tmp, pW[currentLevel],
|
||||
pH[currentLevel], pS[currentLevel], d_Ix, d_Iy, d_Iz);
|
||||
|
||||
for (int iter = 0; iter < nSolverIters; ++iter) {
|
||||
SolveForUpdate(d_du0, d_dv0, d_Ix, d_Iy, d_Iz, pW[currentLevel],
|
||||
pH[currentLevel], pS[currentLevel], alpha, d_du1, d_dv1);
|
||||
|
||||
Swap(d_du0, d_du1);
|
||||
Swap(d_dv0, d_dv1);
|
||||
}
|
||||
|
||||
// update u, v
|
||||
Add(d_u, d_du0, pH[currentLevel] * pS[currentLevel], d_u);
|
||||
Add(d_v, d_dv0, pH[currentLevel] * pS[currentLevel], d_v);
|
||||
pW[currentLevel - 1] = nw;
|
||||
pH[currentLevel - 1] = nh;
|
||||
pS[currentLevel - 1] = ns;
|
||||
}
|
||||
|
||||
if (currentLevel != nLevels - 1) {
|
||||
// prolongate solution
|
||||
float scaleX = (float)pW[currentLevel + 1] / (float)pW[currentLevel];
|
||||
checkCudaErrors(cudaMemset(d_u, 0, stride * height * sizeof(float)));
|
||||
checkCudaErrors(cudaMemset(d_v, 0, stride * height * sizeof(float)));
|
||||
|
||||
Upscale(d_u, pW[currentLevel], pH[currentLevel], pS[currentLevel],
|
||||
pW[currentLevel + 1], pH[currentLevel + 1], pS[currentLevel + 1],
|
||||
scaleX, d_nu);
|
||||
// compute flow
|
||||
for (; currentLevel < nLevels; ++currentLevel) {
|
||||
for (int warpIter = 0; warpIter < nWarpIters; ++warpIter) {
|
||||
checkCudaErrors(cudaMemset(d_du0, 0, dataSize));
|
||||
checkCudaErrors(cudaMemset(d_dv0, 0, dataSize));
|
||||
|
||||
float scaleY = (float)pH[currentLevel + 1] / (float)pH[currentLevel];
|
||||
checkCudaErrors(cudaMemset(d_du1, 0, dataSize));
|
||||
checkCudaErrors(cudaMemset(d_dv1, 0, dataSize));
|
||||
|
||||
Upscale(d_v, pW[currentLevel], pH[currentLevel], pS[currentLevel],
|
||||
pW[currentLevel + 1], pH[currentLevel + 1], pS[currentLevel + 1],
|
||||
scaleY, d_nv);
|
||||
// on current level we compute optical flow
|
||||
// between frame 0 and warped frame 1
|
||||
WarpImage(pI1[currentLevel], pW[currentLevel], pH[currentLevel], pS[currentLevel], d_u, d_v, d_tmp);
|
||||
|
||||
Swap(d_u, d_nu);
|
||||
Swap(d_v, d_nv);
|
||||
ComputeDerivatives(
|
||||
pI0[currentLevel], d_tmp, pW[currentLevel], pH[currentLevel], pS[currentLevel], d_Ix, d_Iy, d_Iz);
|
||||
|
||||
for (int iter = 0; iter < nSolverIters; ++iter) {
|
||||
SolveForUpdate(d_du0,
|
||||
d_dv0,
|
||||
d_Ix,
|
||||
d_Iy,
|
||||
d_Iz,
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
alpha,
|
||||
d_du1,
|
||||
d_dv1);
|
||||
|
||||
Swap(d_du0, d_du1);
|
||||
Swap(d_dv0, d_dv1);
|
||||
}
|
||||
|
||||
// update u, v
|
||||
Add(d_u, d_du0, pH[currentLevel] * pS[currentLevel], d_u);
|
||||
Add(d_v, d_dv0, pH[currentLevel] * pS[currentLevel], d_v);
|
||||
}
|
||||
|
||||
if (currentLevel != nLevels - 1) {
|
||||
// prolongate solution
|
||||
float scaleX = (float)pW[currentLevel + 1] / (float)pW[currentLevel];
|
||||
|
||||
Upscale(d_u,
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
pW[currentLevel + 1],
|
||||
pH[currentLevel + 1],
|
||||
pS[currentLevel + 1],
|
||||
scaleX,
|
||||
d_nu);
|
||||
|
||||
float scaleY = (float)pH[currentLevel + 1] / (float)pH[currentLevel];
|
||||
|
||||
Upscale(d_v,
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
pW[currentLevel + 1],
|
||||
pH[currentLevel + 1],
|
||||
pS[currentLevel + 1],
|
||||
scaleY,
|
||||
d_nv);
|
||||
|
||||
Swap(d_u, d_nu);
|
||||
Swap(d_v, d_nv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMemcpy(u, d_u, dataSize, cudaMemcpyDeviceToHost));
|
||||
checkCudaErrors(cudaMemcpy(v, d_v, dataSize, cudaMemcpyDeviceToHost));
|
||||
checkCudaErrors(cudaMemcpy(u, d_u, dataSize, cudaMemcpyDeviceToHost));
|
||||
checkCudaErrors(cudaMemcpy(v, d_v, dataSize, cudaMemcpyDeviceToHost));
|
||||
|
||||
// cleanup
|
||||
for (int i = 0; i < nLevels; ++i) {
|
||||
checkCudaErrors(cudaFree((void *)pI0[i]));
|
||||
checkCudaErrors(cudaFree((void *)pI1[i]));
|
||||
}
|
||||
// cleanup
|
||||
for (int i = 0; i < nLevels; ++i) {
|
||||
checkCudaErrors(cudaFree((void *)pI0[i]));
|
||||
checkCudaErrors(cudaFree((void *)pI1[i]));
|
||||
}
|
||||
|
||||
delete[] pI0;
|
||||
delete[] pI1;
|
||||
delete[] pW;
|
||||
delete[] pH;
|
||||
delete[] pS;
|
||||
delete[] pI0;
|
||||
delete[] pI1;
|
||||
delete[] pW;
|
||||
delete[] pH;
|
||||
delete[] pS;
|
||||
|
||||
checkCudaErrors(cudaFree(d_tmp));
|
||||
checkCudaErrors(cudaFree(d_du0));
|
||||
checkCudaErrors(cudaFree(d_dv0));
|
||||
checkCudaErrors(cudaFree(d_du1));
|
||||
checkCudaErrors(cudaFree(d_dv1));
|
||||
checkCudaErrors(cudaFree(d_Ix));
|
||||
checkCudaErrors(cudaFree(d_Iy));
|
||||
checkCudaErrors(cudaFree(d_Iz));
|
||||
checkCudaErrors(cudaFree(d_nu));
|
||||
checkCudaErrors(cudaFree(d_nv));
|
||||
checkCudaErrors(cudaFree(d_u));
|
||||
checkCudaErrors(cudaFree(d_v));
|
||||
checkCudaErrors(cudaFree(d_tmp));
|
||||
checkCudaErrors(cudaFree(d_du0));
|
||||
checkCudaErrors(cudaFree(d_dv0));
|
||||
checkCudaErrors(cudaFree(d_du1));
|
||||
checkCudaErrors(cudaFree(d_dv1));
|
||||
checkCudaErrors(cudaFree(d_Ix));
|
||||
checkCudaErrors(cudaFree(d_Iy));
|
||||
checkCudaErrors(cudaFree(d_Iz));
|
||||
checkCudaErrors(cudaFree(d_nu));
|
||||
checkCudaErrors(cudaFree(d_nv));
|
||||
checkCudaErrors(cudaFree(d_u));
|
||||
checkCudaErrors(cudaFree(d_v));
|
||||
}
|
||||
|
||||
@@ -28,16 +28,15 @@
|
||||
#ifndef FLOW_CUDA_H
|
||||
#define FLOW_CUDA_H
|
||||
|
||||
void ComputeFlowCUDA(
|
||||
const float *I0, // source frame
|
||||
const float *I1, // tracked frame
|
||||
int width, // frame width
|
||||
int height, // frame height
|
||||
int stride, // row access stride
|
||||
float alpha, // smoothness coefficient
|
||||
int nLevels, // number of levels in pyramid
|
||||
int nWarpIters, // number of warping iterations per pyramid level
|
||||
int nSolverIters, // number of solver iterations (for linear system)
|
||||
float *u, // output horizontal flow
|
||||
float *v); // output vertical flow
|
||||
void ComputeFlowCUDA(const float *I0, // source frame
|
||||
const float *I1, // tracked frame
|
||||
int width, // frame width
|
||||
int height, // frame height
|
||||
int stride, // row access stride
|
||||
float alpha, // smoothness coefficient
|
||||
int nLevels, // number of levels in pyramid
|
||||
int nWarpIters, // number of warping iterations per pyramid level
|
||||
int nSolverIters, // number of solver iterations (for linear system)
|
||||
float *u, // output horizontal flow
|
||||
float *v); // output vertical flow
|
||||
#endif
|
||||
|
||||
@@ -25,9 +25,10 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "flowGold.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \brief host texture fetch
|
||||
///
|
||||
@@ -41,42 +42,49 @@
|
||||
/// \param[in] y y coord of the point to fetch value at
|
||||
/// \return fetched value
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline float Tex2D(const float *t, int w, int h, int s, float x, float y) {
|
||||
// integer parts in floating point format
|
||||
float intPartX, intPartY;
|
||||
inline float Tex2D(const float *t, int w, int h, int s, float x, float y)
|
||||
{
|
||||
// integer parts in floating point format
|
||||
float intPartX, intPartY;
|
||||
|
||||
// get fractional parts of coordinates
|
||||
float dx = fabsf(modff(x, &intPartX));
|
||||
float dy = fabsf(modff(y, &intPartY));
|
||||
// get fractional parts of coordinates
|
||||
float dx = fabsf(modff(x, &intPartX));
|
||||
float dy = fabsf(modff(y, &intPartY));
|
||||
|
||||
// assume pixels are squares
|
||||
// one of the corners
|
||||
int ix0 = (int)intPartX;
|
||||
int iy0 = (int)intPartY;
|
||||
// assume pixels are squares
|
||||
// one of the corners
|
||||
int ix0 = (int)intPartX;
|
||||
int iy0 = (int)intPartY;
|
||||
|
||||
// mirror out-of-range position
|
||||
if (ix0 < 0) ix0 = abs(ix0 + 1);
|
||||
// mirror out-of-range position
|
||||
if (ix0 < 0)
|
||||
ix0 = abs(ix0 + 1);
|
||||
|
||||
if (iy0 < 0) iy0 = abs(iy0 + 1);
|
||||
if (iy0 < 0)
|
||||
iy0 = abs(iy0 + 1);
|
||||
|
||||
if (ix0 >= w) ix0 = w * 2 - ix0 - 1;
|
||||
if (ix0 >= w)
|
||||
ix0 = w * 2 - ix0 - 1;
|
||||
|
||||
if (iy0 >= h) iy0 = h * 2 - iy0 - 1;
|
||||
if (iy0 >= h)
|
||||
iy0 = h * 2 - iy0 - 1;
|
||||
|
||||
// corner which is opposite to (ix0, iy0)
|
||||
int ix1 = ix0 + 1;
|
||||
int iy1 = iy0 + 1;
|
||||
// corner which is opposite to (ix0, iy0)
|
||||
int ix1 = ix0 + 1;
|
||||
int iy1 = iy0 + 1;
|
||||
|
||||
if (ix1 >= w) ix1 = w * 2 - ix1 - 1;
|
||||
if (ix1 >= w)
|
||||
ix1 = w * 2 - ix1 - 1;
|
||||
|
||||
if (iy1 >= h) iy1 = h * 2 - iy1 - 1;
|
||||
if (iy1 >= h)
|
||||
iy1 = h * 2 - iy1 - 1;
|
||||
|
||||
float res = t[ix0 + iy0 * s] * (1.0f - dx) * (1.0f - dy);
|
||||
res += t[ix1 + iy0 * s] * dx * (1.0f - dy);
|
||||
res += t[ix0 + iy1 * s] * (1.0f - dx) * dy;
|
||||
res += t[ix1 + iy1 * s] * dx * dy;
|
||||
float res = t[ix0 + iy0 * s] * (1.0f - dx) * (1.0f - dy);
|
||||
res += t[ix1 + iy0 * s] * dx * (1.0f - dy);
|
||||
res += t[ix0 + iy1 * s] * (1.0f - dx) * dy;
|
||||
res += t[ix1 + iy1 * s] * dx * dy;
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -92,16 +100,21 @@ inline float Tex2D(const float *t, int w, int h, int s, float x, float y) {
|
||||
/// \param[in] y y coord of the point to fetch value at
|
||||
/// \return fetched value
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline float Tex2Di(const float *src, int w, int h, int s, int x, int y) {
|
||||
if (x < 0) x = abs(x + 1);
|
||||
inline float Tex2Di(const float *src, int w, int h, int s, int x, int y)
|
||||
{
|
||||
if (x < 0)
|
||||
x = abs(x + 1);
|
||||
|
||||
if (y < 0) y = abs(y + 1);
|
||||
if (y < 0)
|
||||
y = abs(y + 1);
|
||||
|
||||
if (x >= w) x = w * 2 - x - 1;
|
||||
if (x >= w)
|
||||
x = w * 2 - x - 1;
|
||||
|
||||
if (y >= h) y = h * 2 - y - 1;
|
||||
if (y >= h)
|
||||
y = h * 2 - y - 1;
|
||||
|
||||
return src[x + y * s];
|
||||
return src[x + y * s];
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -115,23 +128,24 @@ inline float Tex2Di(const float *src, int w, int h, int s, int x, int y) {
|
||||
/// \param[in] newStride image new stride
|
||||
/// \param[out] out downscaled image data
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void Downscale(const float *src, int width, int height, int stride,
|
||||
int newWidth, int newHeight, int newStride, float *out) {
|
||||
for (int i = 0; i < newHeight; ++i) {
|
||||
for (int j = 0; j < newWidth; ++j) {
|
||||
const int srcX = j * 2;
|
||||
const int srcY = i * 2;
|
||||
// average 4 neighbouring pixels
|
||||
float sum;
|
||||
sum = Tex2Di(src, width, height, stride, srcX + 0, srcY + 0);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 0, srcY + 1);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 1, srcY + 0);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 1, srcY + 1);
|
||||
// normalize
|
||||
sum *= 0.25f;
|
||||
out[j + i * newStride] = sum;
|
||||
static void
|
||||
Downscale(const float *src, int width, int height, int stride, int newWidth, int newHeight, int newStride, float *out)
|
||||
{
|
||||
for (int i = 0; i < newHeight; ++i) {
|
||||
for (int j = 0; j < newWidth; ++j) {
|
||||
const int srcX = j * 2;
|
||||
const int srcY = i * 2;
|
||||
// average 4 neighbouring pixels
|
||||
float sum;
|
||||
sum = Tex2Di(src, width, height, stride, srcX + 0, srcY + 0);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 0, srcY + 1);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 1, srcY + 0);
|
||||
sum += Tex2Di(src, width, height, stride, srcX + 1, srcY + 1);
|
||||
// normalize
|
||||
sum *= 0.25f;
|
||||
out[j + i * newStride] = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -146,18 +160,25 @@ static void Downscale(const float *src, int width, int height, int stride,
|
||||
/// \param[in] scale value scale factor (multiplier)
|
||||
/// \param[out] out upscaled field component
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void Upscale(const float *src, int width, int height, int stride,
|
||||
int newWidth, int newHeight, int newStride, float scale,
|
||||
float *out) {
|
||||
for (int i = 0; i < newHeight; ++i) {
|
||||
for (int j = 0; j < newWidth; ++j) {
|
||||
// position within smaller image
|
||||
float x = ((float)j - 0.5f) * 0.5f;
|
||||
float y = ((float)i - 0.5f) * 0.5f;
|
||||
static void Upscale(const float *src,
|
||||
int width,
|
||||
int height,
|
||||
int stride,
|
||||
int newWidth,
|
||||
int newHeight,
|
||||
int newStride,
|
||||
float scale,
|
||||
float *out)
|
||||
{
|
||||
for (int i = 0; i < newHeight; ++i) {
|
||||
for (int j = 0; j < newWidth; ++j) {
|
||||
// position within smaller image
|
||||
float x = ((float)j - 0.5f) * 0.5f;
|
||||
float y = ((float)i - 0.5f) * 0.5f;
|
||||
|
||||
out[j + i * newStride] = Tex2D(src, width, height, stride, x, y) * scale;
|
||||
out[j + i * newStride] = Tex2D(src, width, height, stride, x, y) * scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -176,18 +197,18 @@ static void Upscale(const float *src, int width, int height, int stride,
|
||||
/// \param[in] v vertical displacement
|
||||
/// \param[out] out warped image
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void WarpImage(const float *src, int w, int h, int s, const float *u,
|
||||
const float *v, float *out) {
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
// warped coords
|
||||
float x = (float)j + u[pos];
|
||||
float y = (float)i + v[pos];
|
||||
static void WarpImage(const float *src, int w, int h, int s, const float *u, const float *v, float *out)
|
||||
{
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
// warped coords
|
||||
float x = (float)j + u[pos];
|
||||
float y = (float)i + v[pos];
|
||||
|
||||
out[pos] = Tex2D(src, w, h, s, x, y);
|
||||
out[pos] = Tex2D(src, w, h, s, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -201,48 +222,48 @@ static void WarpImage(const float *src, int w, int h, int s, const float *u,
|
||||
/// \param[out] Iy y derivative
|
||||
/// \param[out] Iz temporal derivative
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void ComputeDerivatives(const float *I0, const float *I1, int w, int h,
|
||||
int s, float *Ix, float *Iy, float *Iz) {
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
float t0, t1;
|
||||
// derivative filter is (1, -8, 0, 8, -1)/12
|
||||
// x derivative
|
||||
t0 = Tex2Di(I0, w, h, s, j - 2, i);
|
||||
t0 -= Tex2Di(I0, w, h, s, j - 1, i) * 8.0f;
|
||||
t0 += Tex2Di(I0, w, h, s, j + 1, i) * 8.0f;
|
||||
t0 -= Tex2Di(I0, w, h, s, j + 2, i);
|
||||
t0 /= 12.0f;
|
||||
static void ComputeDerivatives(const float *I0, const float *I1, int w, int h, int s, float *Ix, float *Iy, float *Iz)
|
||||
{
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
float t0, t1;
|
||||
// derivative filter is (1, -8, 0, 8, -1)/12
|
||||
// x derivative
|
||||
t0 = Tex2Di(I0, w, h, s, j - 2, i);
|
||||
t0 -= Tex2Di(I0, w, h, s, j - 1, i) * 8.0f;
|
||||
t0 += Tex2Di(I0, w, h, s, j + 1, i) * 8.0f;
|
||||
t0 -= Tex2Di(I0, w, h, s, j + 2, i);
|
||||
t0 /= 12.0f;
|
||||
|
||||
t1 = Tex2Di(I1, w, h, s, j - 2, i);
|
||||
t1 -= Tex2Di(I1, w, h, s, j - 1, i) * 8.0f;
|
||||
t1 += Tex2Di(I1, w, h, s, j + 1, i) * 8.0f;
|
||||
t1 -= Tex2Di(I1, w, h, s, j + 2, i);
|
||||
t1 /= 12.0f;
|
||||
t1 = Tex2Di(I1, w, h, s, j - 2, i);
|
||||
t1 -= Tex2Di(I1, w, h, s, j - 1, i) * 8.0f;
|
||||
t1 += Tex2Di(I1, w, h, s, j + 1, i) * 8.0f;
|
||||
t1 -= Tex2Di(I1, w, h, s, j + 2, i);
|
||||
t1 /= 12.0f;
|
||||
|
||||
// spatial derivatives are averaged
|
||||
Ix[pos] = (t0 + t1) * 0.5f;
|
||||
// spatial derivatives are averaged
|
||||
Ix[pos] = (t0 + t1) * 0.5f;
|
||||
|
||||
// t derivative
|
||||
Iz[pos] = I1[pos] - I0[pos];
|
||||
// t derivative
|
||||
Iz[pos] = I1[pos] - I0[pos];
|
||||
|
||||
// y derivative
|
||||
t0 = Tex2Di(I0, w, h, s, j, i - 2);
|
||||
t0 -= Tex2Di(I0, w, h, s, j, i - 1) * 8.0f;
|
||||
t0 += Tex2Di(I0, w, h, s, j, i + 1) * 8.0f;
|
||||
t0 -= Tex2Di(I0, w, h, s, j, i + 2);
|
||||
t0 /= 12.0f;
|
||||
// y derivative
|
||||
t0 = Tex2Di(I0, w, h, s, j, i - 2);
|
||||
t0 -= Tex2Di(I0, w, h, s, j, i - 1) * 8.0f;
|
||||
t0 += Tex2Di(I0, w, h, s, j, i + 1) * 8.0f;
|
||||
t0 -= Tex2Di(I0, w, h, s, j, i + 2);
|
||||
t0 /= 12.0f;
|
||||
|
||||
t1 = Tex2Di(I1, w, h, s, j, i - 2);
|
||||
t1 -= Tex2Di(I1, w, h, s, j, i - 1) * 8.0f;
|
||||
t1 += Tex2Di(I1, w, h, s, j, i + 1) * 8.0f;
|
||||
t1 -= Tex2Di(I1, w, h, s, j, i + 2);
|
||||
t1 /= 12.0f;
|
||||
t1 = Tex2Di(I1, w, h, s, j, i - 2);
|
||||
t1 -= Tex2Di(I1, w, h, s, j, i - 1) * 8.0f;
|
||||
t1 += Tex2Di(I1, w, h, s, j, i + 1) * 8.0f;
|
||||
t1 -= Tex2Di(I1, w, h, s, j, i + 2);
|
||||
t1 /= 12.0f;
|
||||
|
||||
Iy[pos] = (t0 + t1) * 0.5f;
|
||||
Iy[pos] = (t0 + t1) * 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -261,45 +282,53 @@ static void ComputeDerivatives(const float *I0, const float *I1, int w, int h,
|
||||
/// \param[out] du1 new horizontal displacement approximation
|
||||
/// \param[out] dv1 new vertical displacement approximation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void SolveForUpdate(const float *du0, const float *dv0, const float *Ix,
|
||||
const float *Iy, const float *Iz, int w, int h,
|
||||
int s, float alpha, float *du1, float *dv1) {
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
int left, right, up, down;
|
||||
static void SolveForUpdate(const float *du0,
|
||||
const float *dv0,
|
||||
const float *Ix,
|
||||
const float *Iy,
|
||||
const float *Iz,
|
||||
int w,
|
||||
int h,
|
||||
int s,
|
||||
float alpha,
|
||||
float *du1,
|
||||
float *dv1)
|
||||
{
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
int left, right, up, down;
|
||||
|
||||
// handle borders
|
||||
if (j != 0)
|
||||
left = pos - 1;
|
||||
else
|
||||
left = pos;
|
||||
// handle borders
|
||||
if (j != 0)
|
||||
left = pos - 1;
|
||||
else
|
||||
left = pos;
|
||||
|
||||
if (j != w - 1)
|
||||
right = pos + 1;
|
||||
else
|
||||
right = pos;
|
||||
if (j != w - 1)
|
||||
right = pos + 1;
|
||||
else
|
||||
right = pos;
|
||||
|
||||
if (i != 0)
|
||||
down = pos - s;
|
||||
else
|
||||
down = pos;
|
||||
if (i != 0)
|
||||
down = pos - s;
|
||||
else
|
||||
down = pos;
|
||||
|
||||
if (i != h - 1)
|
||||
up = pos + s;
|
||||
else
|
||||
up = pos;
|
||||
if (i != h - 1)
|
||||
up = pos + s;
|
||||
else
|
||||
up = pos;
|
||||
|
||||
float sumU = (du0[left] + du0[right] + du0[up] + du0[down]) * 0.25f;
|
||||
float sumV = (dv0[left] + dv0[right] + dv0[up] + dv0[down]) * 0.25f;
|
||||
float sumU = (du0[left] + du0[right] + du0[up] + du0[down]) * 0.25f;
|
||||
float sumV = (dv0[left] + dv0[right] + dv0[up] + dv0[down]) * 0.25f;
|
||||
|
||||
float frac = (Ix[pos] * sumU + Iy[pos] * sumV + Iz[pos]) /
|
||||
(Ix[pos] * Ix[pos] + Iy[pos] * Iy[pos] + alpha);
|
||||
float frac = (Ix[pos] * sumU + Iy[pos] * sumV + Iz[pos]) / (Ix[pos] * Ix[pos] + Iy[pos] * Iy[pos] + alpha);
|
||||
|
||||
du1[pos] = sumU - Ix[pos] * frac;
|
||||
dv1[pos] = sumV - Iy[pos] * frac;
|
||||
du1[pos] = sumU - Ix[pos] * frac;
|
||||
dv1[pos] = sumV - Iy[pos] * frac;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -318,145 +347,177 @@ static void SolveForUpdate(const float *du0, const float *dv0, const float *Ix,
|
||||
/// \param[out] u horizontal displacement
|
||||
/// \param[out] v vertical displacement
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void ComputeFlowGold(const float *I0, const float *I1, int width, int height,
|
||||
int stride, float alpha, int nLevels, int nWarpIters,
|
||||
int nSolverIters, float *u, float *v) {
|
||||
printf("Computing optical flow on CPU...\n");
|
||||
void ComputeFlowGold(const float *I0,
|
||||
const float *I1,
|
||||
int width,
|
||||
int height,
|
||||
int stride,
|
||||
float alpha,
|
||||
int nLevels,
|
||||
int nWarpIters,
|
||||
int nSolverIters,
|
||||
float *u,
|
||||
float *v)
|
||||
{
|
||||
printf("Computing optical flow on CPU...\n");
|
||||
|
||||
float *u0 = u;
|
||||
float *v0 = v;
|
||||
float *u0 = u;
|
||||
float *v0 = v;
|
||||
|
||||
const float **pI0 = new const float *[nLevels];
|
||||
const float **pI1 = new const float *[nLevels];
|
||||
const float **pI0 = new const float *[nLevels];
|
||||
const float **pI1 = new const float *[nLevels];
|
||||
|
||||
int *pW = new int[nLevels];
|
||||
int *pH = new int[nLevels];
|
||||
int *pS = new int[nLevels];
|
||||
int *pW = new int[nLevels];
|
||||
int *pH = new int[nLevels];
|
||||
int *pS = new int[nLevels];
|
||||
|
||||
const int pixelCountAligned = height * stride;
|
||||
const int pixelCountAligned = height * stride;
|
||||
|
||||
float *tmp = new float[pixelCountAligned];
|
||||
float *du0 = new float[pixelCountAligned];
|
||||
float *dv0 = new float[pixelCountAligned];
|
||||
float *du1 = new float[pixelCountAligned];
|
||||
float *dv1 = new float[pixelCountAligned];
|
||||
float *Ix = new float[pixelCountAligned];
|
||||
float *Iy = new float[pixelCountAligned];
|
||||
float *Iz = new float[pixelCountAligned];
|
||||
float *nu = new float[pixelCountAligned];
|
||||
float *nv = new float[pixelCountAligned];
|
||||
float *tmp = new float[pixelCountAligned];
|
||||
float *du0 = new float[pixelCountAligned];
|
||||
float *dv0 = new float[pixelCountAligned];
|
||||
float *du1 = new float[pixelCountAligned];
|
||||
float *dv1 = new float[pixelCountAligned];
|
||||
float *Ix = new float[pixelCountAligned];
|
||||
float *Iy = new float[pixelCountAligned];
|
||||
float *Iz = new float[pixelCountAligned];
|
||||
float *nu = new float[pixelCountAligned];
|
||||
float *nv = new float[pixelCountAligned];
|
||||
|
||||
// prepare pyramid
|
||||
int currentLevel = nLevels - 1;
|
||||
pI0[currentLevel] = I0;
|
||||
pI1[currentLevel] = I1;
|
||||
// prepare pyramid
|
||||
int currentLevel = nLevels - 1;
|
||||
pI0[currentLevel] = I0;
|
||||
pI1[currentLevel] = I1;
|
||||
|
||||
pW[currentLevel] = width;
|
||||
pH[currentLevel] = height;
|
||||
pS[currentLevel] = stride;
|
||||
pW[currentLevel] = width;
|
||||
pH[currentLevel] = height;
|
||||
pS[currentLevel] = stride;
|
||||
|
||||
for (; currentLevel > 0; --currentLevel) {
|
||||
int nw = pW[currentLevel] / 2;
|
||||
int nh = pH[currentLevel] / 2;
|
||||
int ns = iAlignUp(nw);
|
||||
pI0[currentLevel - 1] = new float[ns * nh];
|
||||
pI1[currentLevel - 1] = new float[ns * nh];
|
||||
for (; currentLevel > 0; --currentLevel) {
|
||||
int nw = pW[currentLevel] / 2;
|
||||
int nh = pH[currentLevel] / 2;
|
||||
int ns = iAlignUp(nw);
|
||||
pI0[currentLevel - 1] = new float[ns * nh];
|
||||
pI1[currentLevel - 1] = new float[ns * nh];
|
||||
|
||||
Downscale(pI0[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], nw, nh, ns, (float *)pI0[currentLevel - 1]);
|
||||
Downscale(pI0[currentLevel],
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
nw,
|
||||
nh,
|
||||
ns,
|
||||
(float *)pI0[currentLevel - 1]);
|
||||
|
||||
Downscale(pI1[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], nw, nh, ns, (float *)pI1[currentLevel - 1]);
|
||||
Downscale(pI1[currentLevel],
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
nw,
|
||||
nh,
|
||||
ns,
|
||||
(float *)pI1[currentLevel - 1]);
|
||||
|
||||
pW[currentLevel - 1] = nw;
|
||||
pH[currentLevel - 1] = nh;
|
||||
pS[currentLevel - 1] = ns;
|
||||
}
|
||||
|
||||
// initial approximation
|
||||
memset(u, 0, stride * height * sizeof(float));
|
||||
memset(v, 0, stride * height * sizeof(float));
|
||||
|
||||
// compute flow
|
||||
for (; currentLevel < nLevels; ++currentLevel) {
|
||||
for (int warpIter = 0; warpIter < nWarpIters; ++warpIter) {
|
||||
memset(du0, 0, pixelCountAligned * sizeof(float));
|
||||
memset(dv0, 0, pixelCountAligned * sizeof(float));
|
||||
|
||||
memset(du1, 0, pixelCountAligned * sizeof(float));
|
||||
memset(dv1, 0, pixelCountAligned * sizeof(float));
|
||||
|
||||
WarpImage(pI1[currentLevel], pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], u, v, tmp);
|
||||
|
||||
// on current level we compute optical flow
|
||||
// between frame 0 and warped frame 1
|
||||
ComputeDerivatives(pI0[currentLevel], tmp, pW[currentLevel],
|
||||
pH[currentLevel], pS[currentLevel], Ix, Iy, Iz);
|
||||
|
||||
for (int iter = 0; iter < nSolverIters; ++iter) {
|
||||
SolveForUpdate(du0, dv0, Ix, Iy, Iz, pW[currentLevel], pH[currentLevel],
|
||||
pS[currentLevel], alpha, du1, dv1);
|
||||
Swap(du0, du1);
|
||||
Swap(dv0, dv1);
|
||||
}
|
||||
|
||||
// update u, v
|
||||
for (int i = 0; i < pH[currentLevel] * pS[currentLevel]; ++i) {
|
||||
u[i] += du0[i];
|
||||
v[i] += dv0[i];
|
||||
}
|
||||
} // end for (int warpIter = 0; warpIter < nWarpIters; ++warpIter)
|
||||
|
||||
if (currentLevel != nLevels - 1) {
|
||||
// prolongate solution
|
||||
float scaleX = (float)pW[currentLevel + 1] / (float)pW[currentLevel];
|
||||
|
||||
Upscale(u, pW[currentLevel], pH[currentLevel], pS[currentLevel],
|
||||
pW[currentLevel + 1], pH[currentLevel + 1], pS[currentLevel + 1],
|
||||
scaleX, nu);
|
||||
|
||||
float scaleY = (float)pH[currentLevel + 1] / (float)pH[currentLevel];
|
||||
|
||||
Upscale(v, pW[currentLevel], pH[currentLevel], pS[currentLevel],
|
||||
pW[currentLevel + 1], pH[currentLevel + 1], pS[currentLevel + 1],
|
||||
scaleY, nv);
|
||||
|
||||
Swap(u, nu);
|
||||
Swap(v, nv);
|
||||
pW[currentLevel - 1] = nw;
|
||||
pH[currentLevel - 1] = nh;
|
||||
pS[currentLevel - 1] = ns;
|
||||
}
|
||||
} // end for (; currentLevel < nLevels; ++currentLevel)
|
||||
|
||||
if (u != u0) {
|
||||
// solution is not in the specified array
|
||||
// copy
|
||||
memcpy(u0, u, pixelCountAligned * sizeof(float));
|
||||
memcpy(v0, v, pixelCountAligned * sizeof(float));
|
||||
Swap(u, nu);
|
||||
Swap(v, nv);
|
||||
}
|
||||
// initial approximation
|
||||
memset(u, 0, stride * height * sizeof(float));
|
||||
memset(v, 0, stride * height * sizeof(float));
|
||||
|
||||
// cleanup
|
||||
// last level is not being freed here
|
||||
// because it refers to input images
|
||||
for (int i = 0; i < nLevels - 1; ++i) {
|
||||
delete[] pI0[i];
|
||||
delete[] pI1[i];
|
||||
}
|
||||
// compute flow
|
||||
for (; currentLevel < nLevels; ++currentLevel) {
|
||||
for (int warpIter = 0; warpIter < nWarpIters; ++warpIter) {
|
||||
memset(du0, 0, pixelCountAligned * sizeof(float));
|
||||
memset(dv0, 0, pixelCountAligned * sizeof(float));
|
||||
|
||||
delete[] pI0;
|
||||
delete[] pI1;
|
||||
delete[] pW;
|
||||
delete[] pH;
|
||||
delete[] pS;
|
||||
delete[] tmp;
|
||||
delete[] du0;
|
||||
delete[] dv0;
|
||||
delete[] du1;
|
||||
delete[] dv1;
|
||||
delete[] Ix;
|
||||
delete[] Iy;
|
||||
delete[] Iz;
|
||||
delete[] nu;
|
||||
delete[] nv;
|
||||
memset(du1, 0, pixelCountAligned * sizeof(float));
|
||||
memset(dv1, 0, pixelCountAligned * sizeof(float));
|
||||
|
||||
WarpImage(pI1[currentLevel], pW[currentLevel], pH[currentLevel], pS[currentLevel], u, v, tmp);
|
||||
|
||||
// on current level we compute optical flow
|
||||
// between frame 0 and warped frame 1
|
||||
ComputeDerivatives(
|
||||
pI0[currentLevel], tmp, pW[currentLevel], pH[currentLevel], pS[currentLevel], Ix, Iy, Iz);
|
||||
|
||||
for (int iter = 0; iter < nSolverIters; ++iter) {
|
||||
SolveForUpdate(
|
||||
du0, dv0, Ix, Iy, Iz, pW[currentLevel], pH[currentLevel], pS[currentLevel], alpha, du1, dv1);
|
||||
Swap(du0, du1);
|
||||
Swap(dv0, dv1);
|
||||
}
|
||||
|
||||
// update u, v
|
||||
for (int i = 0; i < pH[currentLevel] * pS[currentLevel]; ++i) {
|
||||
u[i] += du0[i];
|
||||
v[i] += dv0[i];
|
||||
}
|
||||
} // end for (int warpIter = 0; warpIter < nWarpIters; ++warpIter)
|
||||
|
||||
if (currentLevel != nLevels - 1) {
|
||||
// prolongate solution
|
||||
float scaleX = (float)pW[currentLevel + 1] / (float)pW[currentLevel];
|
||||
|
||||
Upscale(u,
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
pW[currentLevel + 1],
|
||||
pH[currentLevel + 1],
|
||||
pS[currentLevel + 1],
|
||||
scaleX,
|
||||
nu);
|
||||
|
||||
float scaleY = (float)pH[currentLevel + 1] / (float)pH[currentLevel];
|
||||
|
||||
Upscale(v,
|
||||
pW[currentLevel],
|
||||
pH[currentLevel],
|
||||
pS[currentLevel],
|
||||
pW[currentLevel + 1],
|
||||
pH[currentLevel + 1],
|
||||
pS[currentLevel + 1],
|
||||
scaleY,
|
||||
nv);
|
||||
|
||||
Swap(u, nu);
|
||||
Swap(v, nv);
|
||||
}
|
||||
} // end for (; currentLevel < nLevels; ++currentLevel)
|
||||
|
||||
if (u != u0) {
|
||||
// solution is not in the specified array
|
||||
// copy
|
||||
memcpy(u0, u, pixelCountAligned * sizeof(float));
|
||||
memcpy(v0, v, pixelCountAligned * sizeof(float));
|
||||
Swap(u, nu);
|
||||
Swap(v, nv);
|
||||
}
|
||||
|
||||
// cleanup
|
||||
// last level is not being freed here
|
||||
// because it refers to input images
|
||||
for (int i = 0; i < nLevels - 1; ++i) {
|
||||
delete[] pI0[i];
|
||||
delete[] pI1[i];
|
||||
}
|
||||
|
||||
delete[] pI0;
|
||||
delete[] pI1;
|
||||
delete[] pW;
|
||||
delete[] pH;
|
||||
delete[] pS;
|
||||
delete[] tmp;
|
||||
delete[] du0;
|
||||
delete[] dv0;
|
||||
delete[] du1;
|
||||
delete[] dv1;
|
||||
delete[] Ix;
|
||||
delete[] Iy;
|
||||
delete[] Iz;
|
||||
delete[] nu;
|
||||
delete[] nv;
|
||||
}
|
||||
|
||||
@@ -28,17 +28,16 @@
|
||||
#ifndef FLOW_GOLD_H
|
||||
#define FLOW_GOLD_H
|
||||
|
||||
void ComputeFlowGold(
|
||||
const float *I0, // source frame
|
||||
const float *I1, // tracked frame
|
||||
int width, // frame width
|
||||
int height, // frame height
|
||||
int stride, // row access stride
|
||||
float alpha, // smoothness coefficient
|
||||
int nLevels, // number of levels in pyramid
|
||||
int nWarpIters, // number of warping iterations per pyramid level
|
||||
int nIters, // number of solver iterations (for linear system)
|
||||
float *u, // output horizontal flow
|
||||
float *v); // output vertical flow
|
||||
void ComputeFlowGold(const float *I0, // source frame
|
||||
const float *I1, // tracked frame
|
||||
int width, // frame width
|
||||
int height, // frame height
|
||||
int stride, // row access stride
|
||||
float alpha, // smoothness coefficient
|
||||
int nLevels, // number of levels in pyramid
|
||||
int nWarpIters, // number of warping iterations per pyramid level
|
||||
int nIters, // number of solver iterations (for linear system)
|
||||
float *u, // output horizontal flow
|
||||
float *v); // output vertical flow
|
||||
|
||||
#endif
|
||||
|
||||
@@ -31,12 +31,11 @@ const static char *const sSDKsample = "HSOpticalFlow";
|
||||
const float THRESHOLD = 0.05f;
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_functions.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "flowGold.h"
|
||||
#include "flowCUDA.h"
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include "flowGold.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// \brief save optical flow in format described on vision.middlebury.edu/flow
|
||||
@@ -47,30 +46,30 @@ const float THRESHOLD = 0.05f;
|
||||
/// \param[in] u horizontal displacement
|
||||
/// \param[in] v vertical displacement
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void WriteFloFile(const char *name, int w, int h, int s, const float *u,
|
||||
const float *v) {
|
||||
FILE *stream;
|
||||
stream = fopen(name, "wb");
|
||||
void WriteFloFile(const char *name, int w, int h, int s, const float *u, const float *v)
|
||||
{
|
||||
FILE *stream;
|
||||
stream = fopen(name, "wb");
|
||||
|
||||
if (stream == 0) {
|
||||
printf("Could not save flow to \"%s\"\n", name);
|
||||
return;
|
||||
}
|
||||
|
||||
float data = 202021.25f;
|
||||
fwrite(&data, sizeof(float), 1, stream);
|
||||
fwrite(&w, sizeof(w), 1, stream);
|
||||
fwrite(&h, sizeof(h), 1, stream);
|
||||
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
fwrite(u + pos, sizeof(float), 1, stream);
|
||||
fwrite(v + pos, sizeof(float), 1, stream);
|
||||
if (stream == 0) {
|
||||
printf("Could not save flow to \"%s\"\n", name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
float data = 202021.25f;
|
||||
fwrite(&data, sizeof(float), 1, stream);
|
||||
fwrite(&w, sizeof(w), 1, stream);
|
||||
fwrite(&h, sizeof(h), 1, stream);
|
||||
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
const int pos = j + i * s;
|
||||
fwrite(u + pos, sizeof(float), 1, stream);
|
||||
fwrite(v + pos, sizeof(float), 1, stream);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -85,41 +84,41 @@ void WriteFloFile(const char *name, int w, int h, int s, const float *u,
|
||||
/// \param[in] exePath executable file path
|
||||
/// \return true if image is successfully loaded or false otherwise
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
bool LoadImageAsFP32(float *&img_data, int &img_w, int &img_h, int &img_s,
|
||||
const char *name, const char *exePath) {
|
||||
printf("Loading \"%s\" ...\n", name);
|
||||
char *name_ = sdkFindFilePath(name, exePath);
|
||||
bool LoadImageAsFP32(float *&img_data, int &img_w, int &img_h, int &img_s, const char *name, const char *exePath)
|
||||
{
|
||||
printf("Loading \"%s\" ...\n", name);
|
||||
char *name_ = sdkFindFilePath(name, exePath);
|
||||
|
||||
if (!name_) {
|
||||
printf("File not found\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned char *data = 0;
|
||||
unsigned int w = 0, h = 0;
|
||||
bool result = sdkLoadPPM4ub(name_, &data, &w, &h);
|
||||
|
||||
if (result == false) {
|
||||
printf("Invalid file format\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
img_w = w;
|
||||
img_h = h;
|
||||
img_s = iAlignUp(img_w);
|
||||
|
||||
img_data = new float[img_s * h];
|
||||
|
||||
// source is 4 channel image
|
||||
const int widthStep = 4 * img_w;
|
||||
|
||||
for (int i = 0; i < img_h; ++i) {
|
||||
for (int j = 0; j < img_w; ++j) {
|
||||
img_data[j + i * img_s] = ((float)data[j * 4 + i * widthStep]) / 255.0f;
|
||||
if (!name_) {
|
||||
printf("File not found\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
unsigned char *data = 0;
|
||||
unsigned int w = 0, h = 0;
|
||||
bool result = sdkLoadPPM4ub(name_, &data, &w, &h);
|
||||
|
||||
if (result == false) {
|
||||
printf("Invalid file format\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
img_w = w;
|
||||
img_h = h;
|
||||
img_s = iAlignUp(img_w);
|
||||
|
||||
img_data = new float[img_s * h];
|
||||
|
||||
// source is 4 channel image
|
||||
const int widthStep = 4 * img_w;
|
||||
|
||||
for (int i = 0; i < img_h; ++i) {
|
||||
for (int j = 0; j < img_w; ++j) {
|
||||
img_data[j + i * img_s] = ((float)data[j * 4 + i * widthStep]) / 255.0f;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -133,105 +132,108 @@ bool LoadImageAsFP32(float *&img_data, int &img_w, int &img_h, int &img_s,
|
||||
/// \param[in] h_v vertical displacement
|
||||
/// \return true if discrepancy is lower than a given threshold
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
bool CompareWithGold(int width, int height, int stride, const float *h_uGold,
|
||||
const float *h_vGold, const float *h_u, const float *h_v) {
|
||||
float error = 0.0f;
|
||||
bool CompareWithGold(int width,
|
||||
int height,
|
||||
int stride,
|
||||
const float *h_uGold,
|
||||
const float *h_vGold,
|
||||
const float *h_u,
|
||||
const float *h_v)
|
||||
{
|
||||
float error = 0.0f;
|
||||
|
||||
for (int i = 0; i < height; ++i) {
|
||||
for (int j = 0; j < width; ++j) {
|
||||
const int pos = j + i * stride;
|
||||
error += fabsf(h_u[pos] - h_uGold[pos]) + fabsf(h_v[pos] - h_vGold[pos]);
|
||||
for (int i = 0; i < height; ++i) {
|
||||
for (int j = 0; j < width; ++j) {
|
||||
const int pos = j + i * stride;
|
||||
error += fabsf(h_u[pos] - h_uGold[pos]) + fabsf(h_v[pos] - h_vGold[pos]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error /= (float)(width * height);
|
||||
error /= (float)(width * height);
|
||||
|
||||
printf("L1 error : %.6f\n", error);
|
||||
printf("L1 error : %.6f\n", error);
|
||||
|
||||
return (error < THRESHOLD);
|
||||
return (error < THRESHOLD);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
/// application entry point
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv) {
|
||||
// welcome message
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// welcome message
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
|
||||
// pick GPU
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
// pick GPU
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
// find images
|
||||
const char *const sourceFrameName = "frame10.ppm";
|
||||
const char *const targetFrameName = "frame11.ppm";
|
||||
// find images
|
||||
const char *const sourceFrameName = "frame10.ppm";
|
||||
const char *const targetFrameName = "frame11.ppm";
|
||||
|
||||
// image dimensions
|
||||
int width;
|
||||
int height;
|
||||
// row access stride
|
||||
int stride;
|
||||
// image dimensions
|
||||
int width;
|
||||
int height;
|
||||
// row access stride
|
||||
int stride;
|
||||
|
||||
// flow is computed from source image to target image
|
||||
float *h_source; // source image, host memory
|
||||
float *h_target; // target image, host memory
|
||||
// flow is computed from source image to target image
|
||||
float *h_source; // source image, host memory
|
||||
float *h_target; // target image, host memory
|
||||
|
||||
// load image from file
|
||||
if (!LoadImageAsFP32(h_source, width, height, stride, sourceFrameName,
|
||||
argv[0])) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// load image from file
|
||||
if (!LoadImageAsFP32(h_source, width, height, stride, sourceFrameName, argv[0])) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!LoadImageAsFP32(h_target, width, height, stride, targetFrameName,
|
||||
argv[0])) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!LoadImageAsFP32(h_target, width, height, stride, targetFrameName, argv[0])) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// allocate host memory for CPU results
|
||||
float *h_uGold = new float[stride * height];
|
||||
float *h_vGold = new float[stride * height];
|
||||
// allocate host memory for CPU results
|
||||
float *h_uGold = new float[stride * height];
|
||||
float *h_vGold = new float[stride * height];
|
||||
|
||||
// allocate host memory for GPU results
|
||||
float *h_u = new float[stride * height];
|
||||
float *h_v = new float[stride * height];
|
||||
// allocate host memory for GPU results
|
||||
float *h_u = new float[stride * height];
|
||||
float *h_v = new float[stride * height];
|
||||
|
||||
// smoothness
|
||||
// if image brightness is not within [0,1]
|
||||
// this paramter should be scaled appropriately
|
||||
const float alpha = 0.2f;
|
||||
// smoothness
|
||||
// if image brightness is not within [0,1]
|
||||
// this paramter should be scaled appropriately
|
||||
const float alpha = 0.2f;
|
||||
|
||||
// number of pyramid levels
|
||||
const int nLevels = 5;
|
||||
// number of pyramid levels
|
||||
const int nLevels = 5;
|
||||
|
||||
// number of solver iterations on each level
|
||||
const int nSolverIters = 500;
|
||||
// number of solver iterations on each level
|
||||
const int nSolverIters = 500;
|
||||
|
||||
// number of warping iterations
|
||||
const int nWarpIters = 3;
|
||||
// number of warping iterations
|
||||
const int nWarpIters = 3;
|
||||
|
||||
ComputeFlowGold(h_source, h_target, width, height, stride, alpha, nLevels,
|
||||
nWarpIters, nSolverIters, h_uGold, h_vGold);
|
||||
ComputeFlowGold(
|
||||
h_source, h_target, width, height, stride, alpha, nLevels, nWarpIters, nSolverIters, h_uGold, h_vGold);
|
||||
|
||||
ComputeFlowCUDA(h_source, h_target, width, height, stride, alpha, nLevels,
|
||||
nWarpIters, nSolverIters, h_u, h_v);
|
||||
ComputeFlowCUDA(h_source, h_target, width, height, stride, alpha, nLevels, nWarpIters, nSolverIters, h_u, h_v);
|
||||
|
||||
// compare results (L1 norm)
|
||||
bool status =
|
||||
CompareWithGold(width, height, stride, h_uGold, h_vGold, h_u, h_v);
|
||||
// compare results (L1 norm)
|
||||
bool status = CompareWithGold(width, height, stride, h_uGold, h_vGold, h_u, h_v);
|
||||
|
||||
WriteFloFile("FlowGPU.flo", width, height, stride, h_u, h_v);
|
||||
WriteFloFile("FlowGPU.flo", width, height, stride, h_u, h_v);
|
||||
|
||||
WriteFloFile("FlowCPU.flo", width, height, stride, h_uGold, h_vGold);
|
||||
WriteFloFile("FlowCPU.flo", width, height, stride, h_uGold, h_vGold);
|
||||
|
||||
// free resources
|
||||
delete[] h_uGold;
|
||||
delete[] h_vGold;
|
||||
// free resources
|
||||
delete[] h_uGold;
|
||||
delete[] h_vGold;
|
||||
|
||||
delete[] h_u;
|
||||
delete[] h_v;
|
||||
delete[] h_u;
|
||||
delete[] h_v;
|
||||
|
||||
delete[] h_source;
|
||||
delete[] h_target;
|
||||
delete[] h_source;
|
||||
delete[] h_target;
|
||||
|
||||
// report self-test status
|
||||
exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
// report self-test status
|
||||
exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -48,108 +49,117 @@ namespace cg = cooperative_groups;
|
||||
/// \param[out] dv1 new vertical displacement approximation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <int bx, int by>
|
||||
__global__ void JacobiIteration(const float *du0, const float *dv0,
|
||||
const float *Ix, const float *Iy,
|
||||
const float *Iz, int w, int h, int s,
|
||||
float alpha, float *du1, float *dv1) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__global__ void JacobiIteration(const float *du0,
|
||||
const float *dv0,
|
||||
const float *Ix,
|
||||
const float *Iy,
|
||||
const float *Iz,
|
||||
int w,
|
||||
int h,
|
||||
int s,
|
||||
float alpha,
|
||||
float *du1,
|
||||
float *dv1)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
|
||||
volatile __shared__ float du[(bx + 2) * (by + 2)];
|
||||
volatile __shared__ float dv[(bx + 2) * (by + 2)];
|
||||
volatile __shared__ float du[(bx + 2) * (by + 2)];
|
||||
volatile __shared__ float dv[(bx + 2) * (by + 2)];
|
||||
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
// position within global memory array
|
||||
const int pos = min(ix, w - 1) + min(iy, h - 1) * s;
|
||||
// position within global memory array
|
||||
const int pos = min(ix, w - 1) + min(iy, h - 1) * s;
|
||||
|
||||
// position within shared memory array
|
||||
const int shMemPos = threadIdx.x + 1 + (threadIdx.y + 1) * (bx + 2);
|
||||
// position within shared memory array
|
||||
const int shMemPos = threadIdx.x + 1 + (threadIdx.y + 1) * (bx + 2);
|
||||
|
||||
// Load data to shared memory.
|
||||
// load tile being processed
|
||||
du[shMemPos] = du0[pos];
|
||||
dv[shMemPos] = dv0[pos];
|
||||
// Load data to shared memory.
|
||||
// load tile being processed
|
||||
du[shMemPos] = du0[pos];
|
||||
dv[shMemPos] = dv0[pos];
|
||||
|
||||
// load necessary neighbouring elements
|
||||
// We clamp out-of-range coordinates.
|
||||
// It is equivalent to mirroring
|
||||
// because we access data only one step away from borders.
|
||||
if (threadIdx.y == 0) {
|
||||
// beginning of the tile
|
||||
const int bsx = blockIdx.x * blockDim.x;
|
||||
const int bsy = blockIdx.y * blockDim.y;
|
||||
// element position within matrix
|
||||
int x, y;
|
||||
// element position within linear array
|
||||
// gm - global memory
|
||||
// sm - shared memory
|
||||
int gmPos, smPos;
|
||||
// load necessary neighbouring elements
|
||||
// We clamp out-of-range coordinates.
|
||||
// It is equivalent to mirroring
|
||||
// because we access data only one step away from borders.
|
||||
if (threadIdx.y == 0) {
|
||||
// beginning of the tile
|
||||
const int bsx = blockIdx.x * blockDim.x;
|
||||
const int bsy = blockIdx.y * blockDim.y;
|
||||
// element position within matrix
|
||||
int x, y;
|
||||
// element position within linear array
|
||||
// gm - global memory
|
||||
// sm - shared memory
|
||||
int gmPos, smPos;
|
||||
|
||||
x = min(bsx + threadIdx.x, w - 1);
|
||||
// row just below the tile
|
||||
y = max(bsy - 1, 0);
|
||||
gmPos = y * s + x;
|
||||
smPos = threadIdx.x + 1;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
x = min(bsx + threadIdx.x, w - 1);
|
||||
// row just below the tile
|
||||
y = max(bsy - 1, 0);
|
||||
gmPos = y * s + x;
|
||||
smPos = threadIdx.x + 1;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
|
||||
// row above the tile
|
||||
y = min(bsy + by, h - 1);
|
||||
smPos += (by + 1) * (bx + 2);
|
||||
gmPos = y * s + x;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
} else if (threadIdx.y == 1) {
|
||||
// beginning of the tile
|
||||
const int bsx = blockIdx.x * blockDim.x;
|
||||
const int bsy = blockIdx.y * blockDim.y;
|
||||
// element position within matrix
|
||||
int x, y;
|
||||
// element position within linear array
|
||||
// gm - global memory
|
||||
// sm - shared memory
|
||||
int gmPos, smPos;
|
||||
|
||||
y = min(bsy + threadIdx.x, h - 1);
|
||||
// column to the left
|
||||
x = max(bsx - 1, 0);
|
||||
smPos = bx + 2 + threadIdx.x * (bx + 2);
|
||||
gmPos = x + y * s;
|
||||
|
||||
// check if we are within tile
|
||||
if (threadIdx.x < by) {
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
// column to the right
|
||||
x = min(bsx + bx, w - 1);
|
||||
gmPos = y * s + x;
|
||||
smPos += bx + 1;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
// row above the tile
|
||||
y = min(bsy + by, h - 1);
|
||||
smPos += (by + 1) * (bx + 2);
|
||||
gmPos = y * s + x;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
}
|
||||
}
|
||||
else if (threadIdx.y == 1) {
|
||||
// beginning of the tile
|
||||
const int bsx = blockIdx.x * blockDim.x;
|
||||
const int bsy = blockIdx.y * blockDim.y;
|
||||
// element position within matrix
|
||||
int x, y;
|
||||
// element position within linear array
|
||||
// gm - global memory
|
||||
// sm - shared memory
|
||||
int gmPos, smPos;
|
||||
|
||||
cg::sync(cta);
|
||||
y = min(bsy + threadIdx.x, h - 1);
|
||||
// column to the left
|
||||
x = max(bsx - 1, 0);
|
||||
smPos = bx + 2 + threadIdx.x * (bx + 2);
|
||||
gmPos = x + y * s;
|
||||
|
||||
if (ix >= w || iy >= h) return;
|
||||
// check if we are within tile
|
||||
if (threadIdx.x < by) {
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
// column to the right
|
||||
x = min(bsx + bx, w - 1);
|
||||
gmPos = y * s + x;
|
||||
smPos += bx + 1;
|
||||
du[smPos] = du0[gmPos];
|
||||
dv[smPos] = dv0[gmPos];
|
||||
}
|
||||
}
|
||||
|
||||
// now all necessary data are loaded to shared memory
|
||||
int left, right, up, down;
|
||||
left = shMemPos - 1;
|
||||
right = shMemPos + 1;
|
||||
up = shMemPos + bx + 2;
|
||||
down = shMemPos - bx - 2;
|
||||
cg::sync(cta);
|
||||
|
||||
float sumU = (du[left] + du[right] + du[up] + du[down]) * 0.25f;
|
||||
float sumV = (dv[left] + dv[right] + dv[up] + dv[down]) * 0.25f;
|
||||
if (ix >= w || iy >= h)
|
||||
return;
|
||||
|
||||
float frac = (Ix[pos] * sumU + Iy[pos] * sumV + Iz[pos]) /
|
||||
(Ix[pos] * Ix[pos] + Iy[pos] * Iy[pos] + alpha);
|
||||
// now all necessary data are loaded to shared memory
|
||||
int left, right, up, down;
|
||||
left = shMemPos - 1;
|
||||
right = shMemPos + 1;
|
||||
up = shMemPos + bx + 2;
|
||||
down = shMemPos - bx - 2;
|
||||
|
||||
du1[pos] = sumU - Ix[pos] * frac;
|
||||
dv1[pos] = sumV - Iy[pos] * frac;
|
||||
float sumU = (du[left] + du[right] + du[up] + du[down]) * 0.25f;
|
||||
float sumV = (dv[left] + dv[right] + dv[up] + dv[down]) * 0.25f;
|
||||
|
||||
float frac = (Ix[pos] * sumU + Iy[pos] * sumV + Iz[pos]) / (Ix[pos] * Ix[pos] + Iy[pos] * Iy[pos] + alpha);
|
||||
|
||||
du1[pos] = sumU - Ix[pos] * frac;
|
||||
dv1[pos] = sumV - Iy[pos] * frac;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -168,14 +178,22 @@ __global__ void JacobiIteration(const float *du0, const float *dv0,
|
||||
/// \param[out] du1 new horizontal displacement approximation
|
||||
/// \param[out] dv1 new vertical displacement approximation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void SolveForUpdate(const float *du0, const float *dv0, const float *Ix,
|
||||
const float *Iy, const float *Iz, int w, int h,
|
||||
int s, float alpha, float *du1, float *dv1) {
|
||||
// CTA size
|
||||
dim3 threads(32, 6);
|
||||
// grid size
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
static void SolveForUpdate(const float *du0,
|
||||
const float *dv0,
|
||||
const float *Ix,
|
||||
const float *Iy,
|
||||
const float *Iz,
|
||||
int w,
|
||||
int h,
|
||||
int s,
|
||||
float alpha,
|
||||
float *du1,
|
||||
float *dv1)
|
||||
{
|
||||
// CTA size
|
||||
dim3 threads(32, 6);
|
||||
// grid size
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
|
||||
JacobiIteration<32, 6><<<blocks, threads>>>(du0, dv0, Ix, Iy, Iz, w, h, s,
|
||||
alpha, du1, dv1);
|
||||
JacobiIteration<32, 6><<<blocks, threads>>>(du0, dv0, Ix, Iy, Iz, w, h, s, alpha, du1, dv1);
|
||||
}
|
||||
|
||||
@@ -35,19 +35,20 @@
|
||||
/// \param[in] scale scale factor (multiplier)
|
||||
/// \param[out] out result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void UpscaleKernel(int width, int height, int stride, float scale,
|
||||
float *out, cudaTextureObject_t texCoarse) {
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
__global__ void UpscaleKernel(int width, int height, int stride, float scale, float *out, cudaTextureObject_t texCoarse)
|
||||
{
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
if (ix >= width || iy >= height) return;
|
||||
if (ix >= width || iy >= height)
|
||||
return;
|
||||
|
||||
float x = ((float)ix + 0.5f) / (float)width;
|
||||
float y = ((float)iy + 0.5f) / (float)height;
|
||||
float x = ((float)ix + 0.5f) / (float)width;
|
||||
float y = ((float)iy + 0.5f) / (float)height;
|
||||
|
||||
// exploit hardware interpolation
|
||||
// and scale interpolated vector to match next pyramid level resolution
|
||||
out[ix + iy * stride] = tex2D<float>(texCoarse, x, y) * scale;
|
||||
// exploit hardware interpolation
|
||||
// and scale interpolated vector to match next pyramid level resolution
|
||||
out[ix + iy * stride] = tex2D<float>(texCoarse, x, y) * scale;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -62,35 +63,40 @@ __global__ void UpscaleKernel(int width, int height, int stride, float scale,
|
||||
/// \param[in] scale value scale factor (multiplier)
|
||||
/// \param[out] out upscaled field component
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void Upscale(const float *src, int width, int height, int stride,
|
||||
int newWidth, int newHeight, int newStride, float scale,
|
||||
float *out) {
|
||||
dim3 threads(32, 8);
|
||||
dim3 blocks(iDivUp(newWidth, threads.x), iDivUp(newHeight, threads.y));
|
||||
static void Upscale(const float *src,
|
||||
int width,
|
||||
int height,
|
||||
int stride,
|
||||
int newWidth,
|
||||
int newHeight,
|
||||
int newStride,
|
||||
float scale,
|
||||
float *out)
|
||||
{
|
||||
dim3 threads(32, 8);
|
||||
dim3 blocks(iDivUp(newWidth, threads.x), iDivUp(newHeight, threads.y));
|
||||
|
||||
cudaTextureObject_t texCoarse;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
cudaTextureObject_t texCoarse;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = width;
|
||||
texRes.res.pitch2D.height = height;
|
||||
texRes.res.pitch2D.pitchInBytes = stride * sizeof(float);
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = width;
|
||||
texRes.res.pitch2D.height = height;
|
||||
texRes.res.pitch2D.pitchInBytes = stride * sizeof(float);
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(
|
||||
cudaCreateTextureObject(&texCoarse, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texCoarse, &texRes, &texDescr, NULL));
|
||||
|
||||
UpscaleKernel<<<blocks, threads>>>(newWidth, newHeight, newStride, scale, out,
|
||||
texCoarse);
|
||||
UpscaleKernel<<<blocks, threads>>>(newWidth, newHeight, newStride, scale, out, texCoarse);
|
||||
}
|
||||
|
||||
@@ -36,20 +36,26 @@
|
||||
/// \param[in] v vertical displacement
|
||||
/// \param[out] out result
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void WarpingKernel(int width, int height, int stride, const float *u,
|
||||
const float *v, float *out,
|
||||
cudaTextureObject_t texToWarp) {
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
__global__ void WarpingKernel(int width,
|
||||
int height,
|
||||
int stride,
|
||||
const float *u,
|
||||
const float *v,
|
||||
float *out,
|
||||
cudaTextureObject_t texToWarp)
|
||||
{
|
||||
const int ix = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
const int iy = threadIdx.y + blockIdx.y * blockDim.y;
|
||||
|
||||
const int pos = ix + iy * stride;
|
||||
const int pos = ix + iy * stride;
|
||||
|
||||
if (ix >= width || iy >= height) return;
|
||||
if (ix >= width || iy >= height)
|
||||
return;
|
||||
|
||||
float x = ((float)ix + u[pos] + 0.5f) / (float)width;
|
||||
float y = ((float)iy + v[pos] + 0.5f) / (float)height;
|
||||
float x = ((float)ix + u[pos] + 0.5f) / (float)width;
|
||||
float y = ((float)iy + v[pos] + 0.5f) / (float)height;
|
||||
|
||||
out[pos] = tex2D<float>(texToWarp, x, y);
|
||||
out[pos] = tex2D<float>(texToWarp, x, y);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -68,33 +74,32 @@ __global__ void WarpingKernel(int width, int height, int stride, const float *u,
|
||||
/// \param[in] v vertical displacement
|
||||
/// \param[out] out warped image
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static void WarpImage(const float *src, int w, int h, int s, const float *u,
|
||||
const float *v, float *out) {
|
||||
dim3 threads(32, 6);
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
static void WarpImage(const float *src, int w, int h, int s, const float *u, const float *v, float *out)
|
||||
{
|
||||
dim3 threads(32, 6);
|
||||
dim3 blocks(iDivUp(w, threads.x), iDivUp(h, threads.y));
|
||||
|
||||
cudaTextureObject_t texToWarp;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
cudaTextureObject_t texToWarp;
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
texRes.resType = cudaResourceTypePitch2D;
|
||||
texRes.res.pitch2D.devPtr = (void *)src;
|
||||
texRes.res.pitch2D.desc = cudaCreateChannelDesc<float>();
|
||||
texRes.res.pitch2D.width = w;
|
||||
texRes.res.pitch2D.height = h;
|
||||
texRes.res.pitch2D.pitchInBytes = s * sizeof(float);
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = true;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeMirror;
|
||||
texDescr.addressMode[1] = cudaAddressModeMirror;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(
|
||||
cudaCreateTextureObject(&texToWarp, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texToWarp, &texRes, &texDescr, NULL));
|
||||
|
||||
WarpingKernel<<<blocks, threads>>>(w, h, s, u, v, out, texToWarp);
|
||||
WarpingKernel<<<blocks, threads>>>(w, h, s, u, v, out, texToWarp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user