summaryrefslogtreecommitdiffstats
path: root/src/Core/regularisers_CPU/PD_TV_core.c
blob: 534091ba38c091462f1401681bb34963f1eb4bcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * This work is part of the Core Imaging Library developed by
 * Visual Analytics and Imaging System Group of the Science Technology
 * Facilities Council, STFC
 *
 * Copyright 2019 Daniil Kazantsev
 * Copyright 2019 Srikanth Nagella, Edoardo Pasca
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "PD_TV_core.h"

/* C-OMP implementation of Primal-Dual TV [1] by Chambolle Pock denoising/regularization model (2D/3D case)
 *
 * Input Parameters:
 * 1. Noisy image/volume
 * 2. lambdaPar - regularization parameter
 * 3. Number of iterations
 * 4. eplsilon: tolerance constant
 * 5. lipschitz_const: convergence related parameter
 * 6. TV-type: methodTV - 'iso' (0) or 'l1' (1)
 * 7. nonneg: 'nonnegativity (0 is OFF by default, 1 is ON)
 * 8. tau: time marching parameter

 * Output:
 * [1] TV - Filtered/regularized image/volume
 * [2] Information vector which contains [iteration no., reached tolerance]
 *
 * [1] Antonin Chambolle, Thomas Pock. "A First-Order Primal-Dual Algorithm for Convex Problems with Applications to Imaging", 2010
 */

float PDTV_CPU_main(float *Input, float *U, float *infovector, float lambdaPar, int iterationsNumb, float epsil, float lipschitz_const, int methodTV, int nonneg, float tau, int dimX, int dimY, int dimZ)
{
    int ll;
    long j, DimTotal;
    float re, re1, sigma, theta, lt;
    re = 0.0f; re1 = 0.0f;
    int count = 0;

    //tau = 1.0/powf(lipschitz_const,0.5);
    //sigma = 1.0/powf(lipschitz_const,0.5);
    sigma = 1.0/(lipschitz_const*tau);
    theta = 1.0f;
    lt = tau/lambdaPar;
    ll = 0;


    DimTotal = (long)(dimX*dimY*dimZ);

    copyIm(Input, U, (long)(dimX), (long)(dimY), (long)(dimZ));

    if (dimZ <= 1) {
        /*2D case */
        float *U_old=NULL, *P1=NULL, *P2=NULL;

        U_old = calloc(DimTotal, sizeof(float));
        P1 = calloc(DimTotal, sizeof(float));
        P2 = calloc(DimTotal, sizeof(float));

        /* begin iterations */
        for(ll=0; ll<iterationsNumb; ll++) {

            /* computing the the dual P variable */
            DualP2D(U, P1, P2, (long)(dimX), (long)(dimY), sigma);

            /* apply nonnegativity */
            if (nonneg == 1) for(j=0; j<DimTotal; j++) {if (U[j] < 0.0f) U[j] = 0.0f;}

            /* projection step */
            Proj_func2D(P1, P2, methodTV, DimTotal);

            /* copy U to U_old */
            copyIm(U, U_old, (long)(dimX), (long)(dimY), 1l);

            /* calculate divergence */
            DivProj2D(U, Input, P1, P2,(long)(dimX), (long)(dimY), lt, tau);

            /* check early stopping criteria */
            if ((epsil != 0.0f)  && (ll % 5 == 0)) {
                re = 0.0f; re1 = 0.0f;
                for(j=0; j<DimTotal; j++)
                {
                    re += powf(U[j] - U_old[j],2);
                    re1 += powf(U[j],2);
                }
                re = sqrtf(re)/sqrtf(re1);
                if (re < epsil)  count++;
                if (count > 3) break;
            }
            /*get updated solution*/

            getX(U, U_old, theta, DimTotal);
        }
        free(P1); free(P2); free(U_old);
    }
    else {
          /*3D case*/
        float *U_old=NULL, *P1=NULL, *P2=NULL, *P3=NULL;
        U_old = calloc(DimTotal, sizeof(float));
        P1 = calloc(DimTotal, sizeof(float));
        P2 = calloc(DimTotal, sizeof(float));
        P3 = calloc(DimTotal, sizeof(float));

        /* begin iterations */
        for(ll=0; ll<iterationsNumb; ll++) {

         /* computing the the dual P variable */
            DualP3D(U, P1, P2, P3, (long)(dimX), (long)(dimY),  (long)(dimZ), sigma);

            /* apply nonnegativity */
            if (nonneg == 1) for(j=0; j<DimTotal; j++) {if (U[j] < 0.0f) U[j] = 0.0f;}

            /* projection step */
            Proj_func3D(P1, P2, P3, methodTV, DimTotal);

            /* copy U to U_old */
            copyIm(U, U_old, (long)(dimX), (long)(dimY), (long)(dimZ));

            DivProj3D(U, Input, P1, P2, P3, (long)(dimX), (long)(dimY), (long)(dimZ), lt, tau);

            /* check early stopping criteria */
            if ((epsil != 0.0f)  && (ll % 5 == 0)) {
                re = 0.0f; re1 = 0.0f;
                for(j=0; j<DimTotal; j++)
                {
                    re += powf(U[j] - U_old[j],2);
                    re1 += powf(U[j],2);
                }
                re = sqrtf(re)/sqrtf(re1);
                if (re < epsil)  count++;
                if (count > 3) break;
            }
            /*get updated solution*/

            getX(U, U_old, theta, DimTotal);
        }
        free(P1); free(P2); free(P3); free(U_old);
    }
    /*adding info into info_vector */
    infovector[0] = (float)(ll);  /*iterations number (if stopped earlier based on tolerance)*/
    infovector[1] = re;  /* reached tolerance */

    return 0;
}

/*****************************************************************/
/************************2D-case related Functions */
/*****************************************************************/

/*Calculating dual variable (using forward differences)*/
float DualP2D(float *U, float *P1, float *P2, long dimX, long dimY, float sigma)
{
     long i,j,index;
     #pragma omp parallel for shared(U,P1,P2) private(index,i,j)
     for(j=0; j<dimY; j++) {
       for(i=0; i<dimX; i++) {
          index = j*dimX+i;
          /* symmetric boundary conditions (Neuman) */
          if (i == dimX-1) P1[index] += sigma*(U[j*dimX+(i-1)] - U[index]);
          else P1[index] += sigma*(U[j*dimX+(i+1)] - U[index]);
          if (j == dimY-1) P2[index] += sigma*(U[(j-1)*dimX+i] - U[index]);
          else  P2[index] += sigma*(U[(j+1)*dimX+i] - U[index]);
        }}
     return 1;
}

/* Divergence for P dual */
float DivProj2D(float *U, float *Input, float *P1, float *P2, long dimX, long dimY, float lt, float tau)
{
  long i,j,index;
  float P_v1, P_v2, div_var;
  #pragma omp parallel for shared(U,Input,P1,P2) private(index, i, j, P_v1, P_v2, div_var)
  for(j=0; j<dimY; j++) {
    for(i=0; i<dimX; i++) {
            index = j*dimX+i;
            /* symmetric boundary conditions (Neuman) */
            if (i == 0) P_v1 = -P1[index];
            else P_v1 = -(P1[index] - P1[j*dimX+(i-1)]);
            if (j == 0) P_v2 = -P2[index];
            else  P_v2 = -(P2[index] - P2[(j-1)*dimX+i]);
            div_var = P_v1 + P_v2;
            U[index] = (U[index] - tau*div_var + lt*Input[index])/(1.0 + lt);
          }}
  return *U;
}

/*get the updated solution*/
float getX(float *U, float *U_old, float theta, long DimTotal)
{
    long i;
    #pragma omp parallel for shared(U,U_old) private(i)
    for(i=0; i<DimTotal; i++) {
          U[i] +=  theta*(U[i] - U_old[i]);
          }
    return *U;
}


/*****************************************************************/
/************************3D-case related Functions */
/*****************************************************************/
/*Calculating dual variable (using forward differences)*/
float DualP3D(float *U, float *P1, float *P2, float *P3, long dimX, long dimY, long dimZ, float sigma)
{
     long i,j,k,index;
     #pragma omp parallel for shared(U,P1,P2,P3) private(index,i,j,k)
     for(k=0; k<dimZ; k++) {
         for(j=0; j<dimY; j++) {
           for(i=0; i<dimX; i++) {
          index = (dimX*dimY)*k + j*dimX+i;
          /* symmetric boundary conditions (Neuman) */
          if (i == dimX-1) P1[index] += sigma*(U[(dimX*dimY)*k + j*dimX+(i-1)] - U[index]);
          else P1[index] += sigma*(U[(dimX*dimY)*k + j*dimX+(i+1)] - U[index]);
          if (j == dimY-1) P2[index] += sigma*(U[(dimX*dimY)*k + (j-1)*dimX+i] - U[index]);
          else  P2[index] += sigma*(U[(dimX*dimY)*k + (j+1)*dimX+i] - U[index]);
          if (k == dimZ-1) P3[index] += sigma*(U[(dimX*dimY)*(k-1) + j*dimX+i] - U[index]);
          else  P3[index] += sigma*(U[(dimX*dimY)*(k+1) + j*dimX+i] - U[index]);
        }}}
     return 1;
}

/* Divergence for P dual */
float DivProj3D(float *U, float *Input, float *P1, float *P2, float *P3, long dimX, long dimY, long dimZ, float lt, float tau)
{
  long i,j,k,index;
  float P_v1, P_v2, P_v3, div_var;
  #pragma omp parallel for shared(U,Input,P1,P2) private(index, i, j, k, P_v1, P_v2, P_v3, div_var)
  for(k=0; k<dimZ; k++) {
      for(j=0; j<dimY; j++) {
        for(i=0; i<dimX; i++) {
            index = (dimX*dimY)*k + j*dimX+i;
            /* symmetric boundary conditions (Neuman) */
            if (i == 0) P_v1 = -P1[index];
            else P_v1 = -(P1[index] - P1[(dimX*dimY)*k + j*dimX+(i-1)]);
            if (j == 0) P_v2 = -P2[index];
            else  P_v2 = -(P2[index] - P2[(dimX*dimY)*k + (j-1)*dimX+i]);
            if (k == 0) P_v3 = -P3[index];
            else  P_v3 = -(P3[index] - P3[(dimX*dimY)*(k-1) + j*dimX+i]);
            div_var = P_v1 + P_v2 + P_v3;
            U[index] = (U[index] - tau*div_var + lt*Input[index])/(1.0 + lt);
   }}}
  return *U;
}