|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved. |
| 6 | +// Third party copyrights are property of their respective owners. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +__kernel void NCC(__global const uchar *patch, |
| 11 | + __global const uchar *positiveSamples, |
| 12 | + __global const uchar *negativeSamples, |
| 13 | + __global float *ncc, |
| 14 | + int posNum, |
| 15 | + int negNum) |
| 16 | +{ |
| 17 | + int id = get_global_id(0); |
| 18 | + if (id >= 1000) return; |
| 19 | + bool posFlg; |
| 20 | + |
| 21 | + if (id < 500) |
| 22 | + posFlg = true; |
| 23 | + if (id >= 500) |
| 24 | + { |
| 25 | + //Negative index |
| 26 | + id = id - 500; |
| 27 | + posFlg = false; |
| 28 | + } |
| 29 | + |
| 30 | + //Variables |
| 31 | + int s1 = 0, s2 = 0, n1 = 0, n2 = 0, prod = 0; |
| 32 | + float sq1 = 0, sq2 = 0, ares = 0; |
| 33 | + int N = 225; |
| 34 | + //NCC with positive sample |
| 35 | + if (posFlg && id < posNum) |
| 36 | + { |
| 37 | + for (int i = 0; i < N; i++) |
| 38 | + { |
| 39 | + s1 += positiveSamples[id * N + i]; |
| 40 | + s2 += patch[i]; |
| 41 | + n1 += positiveSamples[id * N + i] * positiveSamples[id * N + i]; |
| 42 | + n2 += patch[i] * patch[i]; |
| 43 | + prod += positiveSamples[id * N + i] * patch[i]; |
| 44 | + } |
| 45 | + sq1 = sqrt(max(0.0, n1 - 1.0 * s1 * s1 / N)); |
| 46 | + sq2 = sqrt(max(0.0, n2 - 1.0 * s2 * s2 / N)); |
| 47 | + ares = (sq2 == 0) ? sq1 / fabs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2; |
| 48 | + ncc[id] = ares; |
| 49 | + } |
| 50 | + |
| 51 | + //NCC with negative sample |
| 52 | + if (!posFlg && id < negNum) |
| 53 | + { |
| 54 | + for (int i = 0; i < N; i++) |
| 55 | + { |
| 56 | + |
| 57 | + s1 += negativeSamples[id * N + i]; |
| 58 | + s2 += patch[i]; |
| 59 | + n1 += negativeSamples[id * N + i] * negativeSamples[id * N + i]; |
| 60 | + n2 += patch[i] * patch[i]; |
| 61 | + prod += negativeSamples[id * N + i] * patch[i]; |
| 62 | + } |
| 63 | + sq1 = sqrt(max(0.0, n1 - 1.0 * s1 * s1 / N)); |
| 64 | + sq2 = sqrt(max(0.0, n2 - 1.0 * s2 * s2 / N)); |
| 65 | + ares = (sq2 == 0) ? sq1 / fabs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2; |
| 66 | + ncc[id+500] = ares; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +__kernel void batchNCC(__global const uchar *patches, |
| 71 | + __global const uchar *positiveSamples, |
| 72 | + __global const uchar *negativeSamples, |
| 73 | + __global float *posNcc, |
| 74 | + __global float *negNcc, |
| 75 | + int posNum, |
| 76 | + int negNum, |
| 77 | + int patchNum) |
| 78 | +{ |
| 79 | + int id = get_global_id(0); |
| 80 | + bool posFlg; |
| 81 | + |
| 82 | + if (id < 500*patchNum) |
| 83 | + posFlg = true; |
| 84 | + if (id >= 500*patchNum) |
| 85 | + { |
| 86 | + //Negative index |
| 87 | + id = id - 500*patchNum; |
| 88 | + posFlg = false; |
| 89 | + } |
| 90 | + |
| 91 | + int modelSampleID = id % 500; |
| 92 | + int patchID = id / 500; |
| 93 | + |
| 94 | + //Variables |
| 95 | + int s1 = 0, s2 = 0, n1 = 0, n2 = 0, prod = 0; |
| 96 | + float sq1 = 0, sq2 = 0, ares = 0; |
| 97 | + int N = 225; |
| 98 | + |
| 99 | + //NCC with positive sample |
| 100 | + if (posFlg && modelSampleID < posNum) |
| 101 | + { |
| 102 | + for (int i = 0; i < N; i++) |
| 103 | + { |
| 104 | + s1 += positiveSamples[modelSampleID * N + i]; |
| 105 | + s2 += patches[patchID*N + i]; |
| 106 | + n1 += positiveSamples[modelSampleID * N + i] * positiveSamples[modelSampleID * N + i]; |
| 107 | + n2 += patches[patchID*N + i] * patches[patchID*N + i]; |
| 108 | + prod += positiveSamples[modelSampleID * N + i] * patches[patchID*N + i]; |
| 109 | + } |
| 110 | + sq1 = sqrt(max(0.0, n1 - 1.0 * s1 * s1 / N)); |
| 111 | + sq2 = sqrt(max(0.0, n2 - 1.0 * s2 * s2 / N)); |
| 112 | + ares = (sq2 == 0) ? sq1 / fabs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2; |
| 113 | + posNcc[id] = ares; |
| 114 | + } |
| 115 | + |
| 116 | + //NCC with negative sample |
| 117 | + if (!posFlg && modelSampleID < negNum) |
| 118 | + { |
| 119 | + for (int i = 0; i < N; i++) |
| 120 | + { |
| 121 | + |
| 122 | + s1 += negativeSamples[modelSampleID * N + i]; |
| 123 | + s2 += patches[patchID*N + i]; |
| 124 | + n1 += negativeSamples[modelSampleID * N + i] * negativeSamples[modelSampleID * N + i]; |
| 125 | + n2 += patches[patchID*N + i] * patches[patchID*N + i]; |
| 126 | + prod += negativeSamples[modelSampleID * N + i] * patches[patchID*N + i]; |
| 127 | + } |
| 128 | + sq1 = sqrt(max(0.0, n1 - 1.0 * s1 * s1 / N)); |
| 129 | + sq2 = sqrt(max(0.0, n2 - 1.0 * s2 * s2 / N)); |
| 130 | + ares = (sq2 == 0) ? sq1 / fabs(sq1) : (prod - s1 * s2 / N) / sq1 / sq2; |
| 131 | + negNcc[id] = ares; |
| 132 | + } |
| 133 | +} |
0 commit comments