Skip to content

Commit 323bf75

Browse files
author
Peter Andreas Entschev
committed
Added tranform coordinates functionality
1 parent 9962b87 commit 323bf75

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

include/af/image.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ AFAPI array rotate(const array& in, const float theta, const bool crop=true, con
223223
*/
224224
AFAPI array transform(const array& in, const array& transform, const dim_t odim0 = 0, const dim_t odim1 = 0, const interpType method=AF_INTERP_NEAREST, const bool inverse=true);
225225

226+
/**
227+
C++ Interface for transforming coordinates
228+
229+
\param[in] tf is transformation matrix
230+
\param[in] d0 is the first input dimension
231+
\param[in] d1 is the second input dimension
232+
\return the transformed coordinates
233+
234+
\ingroup transform_func_coordinates
235+
*/
236+
AFAPI array transformCoordinates(const array& tf, const float d0, const float d1);
237+
226238
/**
227239
C++ Interface for translating an image
228240
@@ -853,6 +865,19 @@ extern "C" {
853865
const dim_t odim0, const dim_t odim1,
854866
const af_interp_type method, const bool inverse);
855867

868+
/**
869+
C Interface for transforming an image
870+
C++ Interface for transforming coordinates
871+
872+
\param[out] out the transformed coordinates
873+
\param[in] tf is transformation matrix
874+
\param[in] d0 is the first input dimension
875+
\param[in] d1 is the second input dimension
876+
877+
\ingroup transform_func_coordinates
878+
*/
879+
AFAPI af_err af_transform_coordinates(af_array *out, const af_array tf, const float d0, const float d1);
880+
856881
/**
857882
C Interface for rotating an image
858883
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*******************************************************
2+
* Copyright (c) 2015, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <af/dim4.hpp>
11+
#include <af/defines.h>
12+
#include <af/vision.h>
13+
#include <af/image.h>
14+
#include <af/arith.h>
15+
#include <af/blas.h>
16+
#include <af/data.h>
17+
#include <err_common.hpp>
18+
#include <backend.hpp>
19+
#include <handle.hpp>
20+
#include <convolve.hpp>
21+
#include <arith.hpp>
22+
23+
using af::dim4;
24+
using namespace detail;
25+
26+
template<typename T>
27+
static af_array transform_coordinates(const af_array& tf, const float d0, const float d1)
28+
{
29+
dim_t in_dims[2] = { 4, 3 };
30+
T h_in[4*3] = { (T)0, (T)0, (T)d1, (T)d1,
31+
(T)0, (T)d0, (T)d0, (T)0,
32+
(T)1, (T)1, (T)1, (T)1 };
33+
34+
af_array in = 0;
35+
af_array w = 0;
36+
af_array tmp = 0;
37+
af_array xt = 0;
38+
af_array yt = 0;
39+
af_array t = 0;
40+
41+
AF_CHECK(af_create_array(&in, h_in, 2, in_dims, (af_dtype) af::dtype_traits<T>::af_type));
42+
43+
af_array tfIdx = 0;
44+
af_index_t tfIndexs[2];
45+
tfIndexs[0].isSeq = true;
46+
tfIndexs[1].isSeq = true;
47+
tfIndexs[0].idx.seq = af_make_seq(0, 2, 1);
48+
tfIndexs[1].idx.seq = af_make_seq(2, 2, 1);
49+
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
50+
51+
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
52+
T h_w[4] = { 1, 1, 1, 1 };
53+
dim_t w_dims = 4;
54+
AF_CHECK(af_create_array(&w, h_w, 1, &w_dims, (af_dtype) af::dtype_traits<T>::af_type));
55+
AF_CHECK(af_div(&w, w, tmp, false));
56+
57+
tfIndexs[1].idx.seq = af_make_seq(0, 0, 1);
58+
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
59+
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
60+
AF_CHECK(af_mul(&xt, tmp, w, false));
61+
62+
tfIndexs[1].idx.seq = af_make_seq(1, 1, 1);
63+
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
64+
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
65+
AF_CHECK(af_mul(&yt, tmp, w, false));
66+
67+
AF_CHECK(af_join(&t, 1, xt, yt));
68+
69+
AF_CHECK(af_release_array(w));
70+
AF_CHECK(af_release_array(tmp));
71+
AF_CHECK(af_release_array(xt));
72+
AF_CHECK(af_release_array(yt));
73+
74+
return t;
75+
}
76+
77+
af_err af_transform_coordinates(af_array *out, const af_array tf, const float d0, const float d1)
78+
{
79+
try {
80+
ArrayInfo tfInfo = getInfo(tf);
81+
dim4 tfDims = tfInfo.dims();
82+
ARG_ASSERT(1, (tfDims[0]==3 && tfDims[1]==3 && tfDims.ndims()==2));
83+
84+
af_array output;
85+
af_dtype type = tfInfo.getType();
86+
switch(type) {
87+
case f32: output = transform_coordinates<float >(tf, d0, d1); break;
88+
case f64: output = transform_coordinates<double>(tf, d0, d1); break;
89+
default : TYPE_ERROR(1, type);
90+
}
91+
std::swap(*out, output);
92+
}
93+
CATCHALL;
94+
95+
return AF_SUCCESS;
96+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*******************************************************
2+
* Copyright (c) 2014, ArrayFire
3+
* All rights reserved.
4+
*
5+
* This file is distributed under 3-clause BSD license.
6+
* The complete license agreement can be obtained at:
7+
* http://arrayfire.com/licenses/BSD-3-Clause
8+
********************************************************/
9+
10+
#include <af/image.h>
11+
#include <af/array.h>
12+
#include "error.hpp"
13+
14+
namespace af
15+
{
16+
17+
array transformCoordinates(const array& tf, const float d0, const float d1)
18+
{
19+
af_array out = 0;
20+
AF_THROW(af_transform_coordinates(&out, tf.get(), d0, d1));
21+
return array(out);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)