18 double maxIntensity = 0.0;
21 double intensityVal = 0.0;
22 for (
int k = 0;
k <
Nz;
k++)
30 intensityVal = realVal*realVal + imagVal*imagVal;
31 if (intensityVal > maxIntensity)
33 maxIntensity = intensityVal;
39 string fnamestr = fname;
40 int checktype =
static_cast<int>(fnamestr.rfind(
"."));
43 for (
int k = 0;
k <
Nz;
k++)
52 intensityVal = realVal*realVal + imagVal*imagVal;
53 intensityData[i*
_cfgSig.
cols + j] = (
uchar)(pow(intensityVal / maxIntensity,gamma)*255.0);
56 sprintf(str,
"_%.2d",
k);
57 fnamestr.insert(checktype, str);
87 auto during_time = ((std::chrono::duration<Real>)(end_time - start_time)).count();
89 LOG(
"Implement time : %.5lf sec\n", during_time);
97 int nr = input.size(
_X);
98 int nc = input.size(
_Y);
99 matrix<Real> divp(nr, nc);
102 matrix<Real> z(nr, nc);
111 matrix<Real> denom(nr, nc);
113 for (
int iter = 0; iter < iters; iter++)
115 for (
int i = 0; i < nr; i++)
117 for (
int j = 0; j < nc; j++)
119 z(i,j) = divp(i, j) - input(i, j)*lam;
124 for (
int i = 0; i < nr - 1; i++)
126 for (
int j = 0; j < nc - 1; j++)
128 z1(i, j) = z(i, j + 1) - z(i, j);
129 z2(i, j) = z(i + 1, j) - z(i, j);
130 denom(i, j) = 1 + dt*sqrt(z1(i, j)*z1(i, j) + z2(i, j)*z2(i, j));
133 for (
int i = 0; i < nr-1; i++)
136 z2(i, nc-1) = z(i + 1, nc-1) - z(i, nc-1);
137 denom(i, nc-1) = 1 + dt*sqrt(z1(i, nc-1)*z1(i, nc-1) + z2(i, nc-1)*z2(i, nc-1));
139 for (
int j = 0; j < nc - 1; j++)
141 z1(nr-1, j) = z(nr-1, j+1) - z(nr-1, j);
143 denom(nr-1, j) = 1 + dt*sqrt(z1(nr-1, j)*z1(nr-1, j) + z2(nr-1, j)*z2(nr-1, j));
145 denom(nr-1, nc-1) = 1.0;
146 z1(nr - 1, nc - 1) = 0.0;
147 z2(nr - 1, nc - 1) = 0.0;
151 for (
int i = 0; i < nr ; i++)
153 for (
int j = 0; j < nc; j++)
155 p1(i, j) = (p1(i, j) + dt*z1(i, j)) / denom(i, j);
156 p2(i, j) = (p2(i, j) + dt*z2(i, j)) / denom(i, j);
161 for (
int i = 1; i < nr; i++)
163 for (
int j = 1; j < nc; j++)
165 divp(i, j) = p1(i, j) - p1(i, j - 1) + p2(i, j) - p2(i - 1, j);
168 for (
int i = 1; i < nr; i++)
170 divp(i, 0) = p2(i, 0) - p2(i-1, 0);
172 for (
int j = 1; j < nc; j++)
174 divp(0, j) = p1(0, j) - p1(0, j - 1);
181 for (
int i = 0; i < input.size[
_X]; i++)
183 for (
int j = 0; j < input.size[
_Y]; j++)
185 output(i, j) = input(i,j) - divp(i, j)/lam;
193 double sqrtsum = 0.0;
194 int nr = input.size[
_X];
195 int nc = input.size[
_Y];
197 for (
int i = 0; i < nr-1; i++)
199 for (
int j = 0; j < nc-1; j++)
201 sqrtsum += sqrt((input(i,j)-input(i+1,j))*(input(i,j)-input(i+1,j)) + (input(i,j)-input(i,j+1))*(input(i,j)-input(i,j+1)));
204 for (
int i = 0; i < nr-1; i++)
206 sqrtsum += sqrt(pow(input(i, nc-1) - input(i + 1, nc-1), 2) + pow(input(i, nc-1) - input(i, 0), 2));
208 for (
int j = 0; j < nc - 1; j++)
210 sqrtsum += sqrt(pow(input(nr-1, j) - input(0, j), 2) + pow(input(nr-1, j) - input(nr-1, j+1), 2));
212 sqrtsum += sqrt(pow(input(nr-1, nc-1) - input(0, nc-1), 2) + pow(input(nr-1, nc-1) - input(nr-1, 0), 2));
217 void ophSigCH::c2ri(matrix<Complex<Real>>& complexinput, matrix<Real>& realimagoutput)
219 for (
int i = 0; i < complexinput.size[
_X]; i++)
221 for (
int j = 0; j < complexinput.size[
_Y]; j++)
223 realimagoutput(i, j) = complexinput(i, j)[
_RE];
224 realimagoutput(i + complexinput.size[
_X], j) = complexinput(i, j)[
_IM];
230 void ophSigCH::ri2c(matrix<Real>& realimaginput, matrix<Complex<Real>>& complexoutput)
232 for (
int i = 0; i < complexoutput.size[
_X]; i++)
234 for (
int j = 0; j < complexoutput.size[
_Y]; j++)
236 complexoutput(i, j)[
_RE] = realimaginput(i, j);
237 complexoutput(i, j)[
_IM] = realimaginput(i + complexoutput.size[
_X], j);
242 void ophSigCH::volume2plane(matrix<Real>& realimagvolumeinput, vector<Real> z, matrix<Real>& realimagplaneoutput)
245 int nr = realimagvolumeinput.size(
_X) >> 1;
246 int nc = realimagvolumeinput.size(
_Y) / nz;
248 matrix<Complex<Real>> complexTemp(nr,nc);
249 matrix<Complex<Real>> complexAccum(nr, nc);
250 complexAccum.zeros();
252 for (
int k = 0;
k < nz;
k++)
255 for (
int i = 0; i < nr; i++)
257 for (
int j = 0; j < nc; j++)
259 complexTemp(i, j)[
_RE] = realimagvolumeinput(i, j + temp);
260 complexTemp(i, j)[
_IM] = realimagvolumeinput(i + nr, j + temp);
264 complexAccum = complexAccum + result;
266 c2ri(complexAccum, realimagplaneoutput);
270 void ophSigCH::plane2volume(matrix<Real>& realimagplaneinput, vector<Real> z, matrix<Real>& realimagplaneoutput)
272 int nr = realimagplaneinput.size(
_X)/2;
273 int nc = realimagplaneinput.size(
_Y);
276 matrix<Complex<Real>> complexplaneinput(nr, nc);
277 for (
int i = 0; i < nr; i++)
279 for (
int j = 0; j < nc; j++)
281 complexplaneinput(i, j)[
_RE] = realimagplaneinput(i, j);
282 complexplaneinput(i, j)[
_IM] = realimagplaneinput(i + nr, j);
287 matrix<Complex<Real>> temp(nr, nc);
288 for (
int k = 0;
k < nz;
k++)
291 for (
int i = 0; i < nr; i++)
293 for (
int j = 0; j < nc; j++)
295 realimagplaneoutput(i, j +
k*nc) = temp(i, j)[
_RE];
296 realimagplaneoutput(i + nr, j +
k*nc) = temp(i, j)[
_IM];
303 void ophSigCH::convert3Dto2D(matrix<Complex<Real>>* complex3Dinput,
int nz, matrix<Complex<Real>>& complex2Doutput)
305 int nr = complex3Dinput[0].size(
_X);
306 int nc = complex3Dinput[0].size(
_Y);
308 for (
int i = 0; i < nr; i++)
310 for (
int j = 0; j < nc; j++)
312 for (
int k = 0;
k < nz;
k++)
314 complex2Doutput(i, j +
k*nc) = complex3Dinput[
k](i, j);
320 void ophSigCH::convert2Dto3D(matrix<Complex<Real>>& complex2Dinput,
int nz, matrix<Complex<Real>>* complex3Doutput)
322 int nr = complex2Dinput.size(
_X);
323 int nc = complex2Dinput.size(
_Y)/nz;
325 for (
int k = 0;
k < nz;
k++)
327 for (
int i = 0; i < nr; i++)
329 for (
int j = 0; j < nc; j++)
331 complex3Doutput[
k](i, j) = complex2Dinput(i, j +
k*nc);
337 void ophSigCH::twist(matrix<Real>& realimagplaneinput, matrix<Real>& realimagvolumeoutput)
340 int nrv = realimagvolumeoutput.size(
_X);
341 int ncv = realimagvolumeoutput.size(
_Y);
342 int nrp = realimagplaneinput.size(
_X);
343 int ncp = realimagplaneinput.size(
_Y);
347 double stopCriterion = 1;
352 bool enforceMonotone = 1;
353 bool compute_mse = 0;
371 double rho0 = (1. - lam1 / lamN) / (1. + lam1 / lamN);
373 alpha = 2. / (1 + sqrt(1 - pow(rho0, 2)));
376 beta = alpha*2. / (lam1 + lamN);
382 double criterion = 0.0;
388 matrix<Real> resid(nrp, ncp);
390 for (
int i = 0; i < nrp; i++)
392 for (
int j = 0; j < ncp; j++)
394 resid(i, j) = realimagplaneinput(i, j) - resid(i, j);
405 LOG(
"initial objective = %10.6e\n", prev_f);
412 matrix<Real> xm1, xm2;
413 xm1.resize(nrv, ncv);
414 xm2.resize(nrv, ncv);
415 xm1 = realimagvolumeoutput;
416 xm2 = realimagvolumeoutput;
419 matrix<Real> grad, temp_volume;
420 grad.resize(nrv, ncv);
421 temp_volume.resize(nrv, ncv);
427 for (
int i = 0; i < nrv; i++)
429 for (
int j = 0; j < ncv; j++)
431 temp_volume(i, j) = xm1(i, j) + grad(i, j) / max_svd;
434 tvdenoise(temp_volume, 2.0 / (tau / max_svd), tv_iters, realimagvolumeoutput);
436 if ((IST_iters >= 2) | (TwIST_iters != 0))
440 for (
int i = 0; i < nrv; i++)
442 for (
int j = 0; j < ncv; j++)
444 if (realimagvolumeoutput(i, j) == 0)
454 for (
int i = 0; i < nrv; i++)
456 for (
int j = 0; j < ncv; j++)
458 xm2(i, j) = (alpha - beta)*xm1(i, j) + (1.0 - alpha)*xm2(i, j) + beta*realimagvolumeoutput(i, j);
464 for (
int i = 0; i < nrp; i++)
466 for (
int j = 0; j < ncp; j++)
468 resid(i, j) = realimagplaneinput(i, j) - resid(i, j);
472 if ((f > prev_f) & (enforceMonotone))
480 realimagvolumeoutput = xm2;
481 if (TwIST_iters % 10000 == 0)
483 max_svd = 0.9*max_svd;
491 for (
int i = 0; i < nrp; i++)
493 for (
int j = 0; j < ncp; j++)
495 resid(i, j) = realimagplaneinput(i, j) - resid(i, j);
501 max_svd = 2 * max_svd;
504 LOG(
"Incrementing S = %2.2e\n", max_svd);
518 xm1 = realimagvolumeoutput;
520 criterion = (
abs(f - prev_f)) / prev_f;
522 cont_outer = ((iter <= maxiter) & (criterion > tolA));
533 LOG(
"Iteration=%4d, objective=%9.5e, criterion=%7.3e\n", iter, f, criterion / tolA);
539 double sum_abs_x=0.0;
540 for (
int i = 0; i < nrv; i++)
542 for (
int j = 0; j < ncv; j++)
544 sum_abs_x +=
abs(realimagvolumeoutput(i, j));
547 LOG(
"\n Finishied the main algorithm!\n");
549 LOG(
"||x||_1 = %10.3e\n", sum_abs_x);
550 LOG(
"Objective function = %10.3e\n", f);
557 for (
int i = 0; i < input.size(
_X); i++)
559 for (
int j = 0; j < input.size(
_Y); j++)
561 output += input(i, j)*input(i, j);
569 LOG(
"CH Reading....%s...\n", fname);
577 LOG(
"file's extension is not 'xml'\n");
583 LOG(
"Failed to load file \"%s\"\n", fname);
605 for (
int i = 0; i <
Nz; i++)
607 Z.at(i) = zmin + i*dz;
615 string realname = real;
616 string imagname = imag;
617 int checktype =
static_cast<int>(realname.rfind(
"."));
618 matrix<Real> realMat[3], imagMat[3];
620 std::string realtype = realname.substr(checktype + 1, realname.size());
621 std::string imgtype = imagname.substr(checktype + 1, realname.size());
624 if (realtype != imgtype) {
625 LOG(
"failed : The data type between real and imaginary is different!\n");
628 if (realtype ==
"bmp")
630 FILE* freal =
nullptr;
631 FILE* fimag =
nullptr;
634 freal = fopen(realname.c_str(),
"rb");
635 fimag = fopen(imagname.c_str(),
"rb");
636 if (freal ==
nullptr)
638 LOG(
"real bmp file open fail!\n");
641 if (fimag ==
nullptr)
643 LOG(
"imaginary bmp file open fail!\n");
654 LOG(
"bmp header is empty!\n");
659 LOG(
"check your parameter file!\n");
664 LOG(
"image size is different!\n");
699 nRead = fread(realdata,
sizeof(
uchar), size, freal);
700 nRead = fread(imagdata,
sizeof(
uchar), size, fimag);
707 for (
int j = 0; j < static_cast<int>(
hInfo.
width); j++)
716 LOG(
"file load complete!\n");
721 else if (realtype ==
"bin")
726 ifstream freal(realname, ifstream::binary);
727 ifstream fimag(imagname, ifstream::binary);
731 double *realdata =
new double[total];
732 double *imagdata =
new double[total];
734 freal.read(reinterpret_cast<char*>(realdata),
sizeof(
double) * total);
735 fimag.read(reinterpret_cast<char*>(imagdata),
sizeof(
double) * total);
741 realMat[0](row, col) = realdata[
_cfgSig.
rows*col + row];
742 imagMat[0](row, col) = imagdata[
_cfgSig.
rows*col + row];
751 else if (bitpixel == 24)
768 string RGB_name[] = {
"_B",
"_G",
"_R" };
769 double *realdata =
new double[total];
770 double *imagdata =
new double[total];
771 char* context =
nullptr;
773 for (
int z = 0; z < (bitpixel / 8); z++)
775 ifstream freal(strtok((
char*)realname.c_str(),
".") + RGB_name[z] +
"bin", ifstream::binary);
776 ifstream fimag(strtok((
char*)imagname.c_str(),
".") + RGB_name[z] +
"bin", ifstream::binary);
778 freal.read(reinterpret_cast<char*>(realdata),
sizeof(
double) * total);
779 fimag.read(reinterpret_cast<char*>(imagdata),
sizeof(
double) * total);
785 realMat[z](row, col) = realdata[
_cfgSig.
rows*col + row];
786 imagMat[z](row, col) = imagdata[
_cfgSig.
rows*col + row];
805 for (
int z = 0; z < (bitpixel) / 8; z++)
811 ComplexH[z](i, j)[
_RE] = realMat[z](i, j)/255.0*2.0-1.0;
812 ComplexH[z](i, j)[
_IM] = imagMat[z](i, j)/255.0*2.0-1.0;
817 LOG(
"data nomalization complete\n");
835 int iStart = nr / 2 - 1;
836 int jStart = nc / 2 - 1;
837 for (
int i = 0; i < nr; i++)
839 for (
int j = 0; j < nc; j++)
841 src2(i+iStart, j+jStart)[
_RE] = complexH(i, j)[
_RE];
842 src2(i+iStart, j+jStart)[
_IM] = complexH(i, j)[
_IM];
847 double dfr = 1.0 / (((double)nr2)*dr);
848 double dfc = 1.0 / (((double)nc2)*dc);
850 matrix<Complex<Real>> propKernel(nr2, nc2);
852 for (
int i = 0; i < nr2; i++)
854 for (
int j = 0; j < nc2; j++)
856 fz = sqrt(pow(1.0 /
_cfgSig.
wavelength[0], 2) - pow((i - nr2 / 2.0 + 1.0)*dfr, 2) - pow((j - nc2 / 2.0 + 1.0)*dfc, 2));
862 matrix<Complex<Real>> src2temp(nr2, nc2);
863 matrix<Complex<Real>> FFZP(nr2, nc2);
864 matrix<Complex<Real>> FFZP2(nr2, nc2);
868 FFZP2.mulElem(propKernel);
874 matrix<Complex<Real>> dst(nr, nc);
875 for (
int i = 0; i < nr; i++)
877 for (
int j = 0; j < nc; j++)
879 dst(i,j)[
_RE] = src2(i + iStart , j + jStart)[
_RE];
880 dst(i,j)[
_IM] = src2(i + iStart , j + jStart)[
_IM];
oph::matrix< Complex< Real > > OphComplexField
void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
virtual bool saveAsImg(const char *fname, uint8_t bitsperpixel, uchar *src, int width, int height)
Function for creating image files.
void plane2volume(matrix< Real > &realimagplaneinput, vector< Real > z, matrix< Real > &realimagplaneoutput)
void ri2c(matrix< Real > &realimaginput, matrix< Complex< Real >> &complexoutput)
void c2ri(matrix< Complex< Real >> &complexinput, matrix< Real > &realimagoutput)
bool runCH(int complexHidx)
bool checkExtension(const char *fname, const char *ext)
Functions for extension checking.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
ophSigCH(void)
Constructor.
void fft2(matrix< Complex< T >> &src, matrix< Complex< T >> &dst, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Function for Fast Fourier transform 2D.
bool saveNumRec(const char *fname)
matrix< Complex< Real > > propagationHoloAS(matrix< Complex< Real >> complexH, float depth)
void convert2Dto3D(matrix< Complex< Real >> &complex2Dinput, int nz, matrix< Complex< Real >> *complex3Doutput)
double matrixEleSquareSum(matrix< Real > &input)
OphComplexField * ComplexH
void twist(matrix< Real > &realimagplaneinput, matrix< Real > &realimagvolumeoutput)
bool readConfig(const char *fname)
void fftShift(matrix< Complex< Real >> &src, matrix< Complex< Real >> &dst)
Shift zero-frequency component to center of spectrum.
void volume2plane(matrix< Real > &realimagvolumeinput, vector< Real > z, matrix< Real > &realimagplaneoutput)
XMLError LoadFile(const char *filename)
bool setCHparam(vector< Real > &z, int maxIter, double tau, double tolA, int tvIter)
void tvdenoise(matrix< Real > &input, double lam, int iters, matrix< Real > &output)
bool loadCHtemp(const char *real, const char *imag, uint8_t bitpixel)
matrix< Real > NumRecRealImag
const XMLElement * FirstChildElement(const char *name=0) const
void convert3Dto2D(matrix< Complex< Real >> *complex3Dinput, int nz, matrix< Complex< Real >> &complex2Doutput)
double tvnorm(matrix< Real > &input)