Openholo  beta 2.0
Open Source Digital Holographic Library
Phase Shifting

Functions

bool ophSig::getComplexHFromPSDH (const char *fname0, const char *fname90, const char *fname180, const char *fname270)
 Extraction of complex field from 4 phase shifted interference patterns . More...
 

Detailed Description

Extract complex field from 4 interference patterns with 90 degree phase shifts of the reference wave Store the result complex field to the member variable ComplexH

Function Documentation

◆ getComplexHFromPSDH()

bool ophSig::getComplexHFromPSDH ( const char *  fname0,
const char *  fname90,
const char *  fname180,
const char *  fname270 
)

Extraction of complex field from 4 phase shifted interference patterns .

Parameters
fname0,fname90,fname180,fname270Input image files for 4 interference patterns
Returns
if works well return 0 or error occurs return -1

Definition at line 1030 of file ophSig.cpp.

1031 {
1032  string fname0str = fname0;
1033  string fname90str = fname90;
1034  string fname180str = fname180;
1035  string fname270str = fname270;
1036  int checktype = static_cast<int>(fname0str.rfind("."));
1037  OphRealField f0Mat[3], f90Mat[3], f180Mat[3], f270Mat[3];
1038 
1039  std::string f0type = fname0str.substr(checktype + 1, fname0str.size());
1040 
1041  uint8_t bitsperpixel;
1042 
1043  if (f0type == "bmp")
1044  {
1045  FILE *f0, *f90, *f180, *f270;
1046  fileheader hf;
1047  bitmapinfoheader hInfo;
1048  fopen_s(&f0, fname0str.c_str(), "rb"); fopen_s(&f90, fname90str.c_str(), "rb");
1049  fopen_s(&f180, fname180str.c_str(), "rb"); fopen_s(&f270, fname270str.c_str(), "rb");
1050  if (!f0)
1051  {
1052  LOG("bmp file open fail! (phase shift = 0)\n");
1053  return false;
1054  }
1055  if (!f90)
1056  {
1057  LOG("bmp file open fail! (phase shift = 90)\n");
1058  return false;
1059  }
1060  if (!f180)
1061  {
1062  LOG("bmp file open fail! (phase shift = 180)\n");
1063  return false;
1064  }
1065  if (!f270)
1066  {
1067  LOG("bmp file open fail! (phase shift = 270)\n");
1068  return false;
1069  }
1070  fread(&hf, sizeof(fileheader), 1, f0);
1071  fread(&hInfo, sizeof(bitmapinfoheader), 1, f0);
1072 
1073  if (hf.signature[0] != 'B' || hf.signature[1] != 'M') { LOG("Not BMP File!\n"); }
1074  if ((hInfo.height == 0) || (hInfo.width == 0))
1075  {
1076  LOG("bmp header is empty!\n");
1077  hInfo.height = context_.pixel_number[_X];
1078  hInfo.width = context_.pixel_number[_Y];
1079  if (hInfo.height == 0 || hInfo.width == 0)
1080  {
1081  LOG("check your parameter file!\n");
1082  return false;
1083  }
1084  }
1085  if ((context_.pixel_number[_Y] != hInfo.height) || (context_.pixel_number[_X] != hInfo.width)) {
1086  LOG("image size is different!\n");
1087  context_.pixel_number[_Y] = hInfo.height;
1088  context_.pixel_number[_X] = hInfo.width;
1089  LOG("changed parameter of size %d x %d\n", context_.pixel_number[_X], context_.pixel_number[_Y]);
1090  }
1091  bitsperpixel = hInfo.bitsperpixel;
1092  if (hInfo.bitsperpixel == 8)
1093  {
1094  rgbquad palette[256];
1095  fread(palette, sizeof(rgbquad), 256, f0);
1096  fread(palette, sizeof(rgbquad), 256, f90);
1097  fread(palette, sizeof(rgbquad), 256, f180);
1098  fread(palette, sizeof(rgbquad), 256, f270);
1099 
1100  f0Mat[0].resize(hInfo.height, hInfo.width);
1101  f90Mat[0].resize(hInfo.height, hInfo.width);
1102  f180Mat[0].resize(hInfo.height, hInfo.width);
1103  f270Mat[0].resize(hInfo.height, hInfo.width);
1104  ComplexH[0].resize(hInfo.height, hInfo.width);
1105  }
1106  else
1107  {
1108  f0Mat[0].resize(hInfo.height, hInfo.width);
1109  f90Mat[0].resize(hInfo.height, hInfo.width);
1110  f180Mat[0].resize(hInfo.height, hInfo.width);
1111  f270Mat[0].resize(hInfo.height, hInfo.width);
1112  ComplexH[0].resize(hInfo.height, hInfo.width);
1113 
1114  f0Mat[1].resize(hInfo.height, hInfo.width);
1115  f90Mat[1].resize(hInfo.height, hInfo.width);
1116  f180Mat[1].resize(hInfo.height, hInfo.width);
1117  f270Mat[1].resize(hInfo.height, hInfo.width);
1118  ComplexH[1].resize(hInfo.height, hInfo.width);
1119 
1120  f0Mat[2].resize(hInfo.height, hInfo.width);
1121  f90Mat[2].resize(hInfo.height, hInfo.width);
1122  f180Mat[2].resize(hInfo.height, hInfo.width);
1123  f270Mat[2].resize(hInfo.height, hInfo.width);
1124  ComplexH[2].resize(hInfo.height, hInfo.width);
1125  }
1126 
1127  uchar* f0data = (uchar*)malloc(sizeof(uchar)*hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8));
1128  uchar* f90data = (uchar*)malloc(sizeof(uchar)*hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8));
1129  uchar* f180data = (uchar*)malloc(sizeof(uchar)*hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8));
1130  uchar* f270data = (uchar*)malloc(sizeof(uchar)*hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8));
1131 
1132  fread(f0data, sizeof(uchar), hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8), f0);
1133  fread(f90data, sizeof(uchar), hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8), f90);
1134  fread(f180data, sizeof(uchar), hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8), f180);
1135  fread(f270data, sizeof(uchar), hInfo.width*hInfo.height*(hInfo.bitsperpixel / 8), f270);
1136 
1137  fclose(f0);
1138  fclose(f90);
1139  fclose(f180);
1140  fclose(f270);
1141 
1142  for (int i = hInfo.height - 1; i >= 0; i--)
1143  {
1144  for (int j = 0; j < static_cast<int>(hInfo.width); j++)
1145  {
1146  for (int z = 0; z < (hInfo.bitsperpixel / 8); z++)
1147  {
1148  f0Mat[z](hInfo.height - i - 1, j) = (double)f0data[i*hInfo.width*(hInfo.bitsperpixel / 8) + (hInfo.bitsperpixel / 8)*j + z];
1149  f90Mat[z](hInfo.height - i - 1, j) = (double)f90data[i*hInfo.width*(hInfo.bitsperpixel / 8) + (hInfo.bitsperpixel / 8)*j + z];
1150  f180Mat[z](hInfo.height - i - 1, j) = (double)f180data[i*hInfo.width*(hInfo.bitsperpixel / 8) + (hInfo.bitsperpixel / 8)*j + z];
1151  f270Mat[z](hInfo.height - i - 1, j) = (double)f270data[i*hInfo.width*(hInfo.bitsperpixel / 8) + (hInfo.bitsperpixel / 8)*j + z];
1152  }
1153  }
1154  }
1155  LOG("PSDH file load complete!\n");
1156 
1157  free(f0data);
1158  free(f90data);
1159  free(f180data);
1160  free(f270data);
1161 
1162  }
1163  else
1164  {
1165  LOG("wrong type (only BMP supported)\n");
1166  }
1167 
1168  // calculation complexH from 4 psdh and then normalize
1169  double normalizefactor = 1. / 256.;
1170  for (int z = 0; z < (bitsperpixel / 8); z++)
1171  {
1172  for (int i = 0; i < context_.pixel_number[_X]; i++)
1173  {
1174  for (int j = 0; j < context_.pixel_number[_Y]; j++)
1175  {
1176  ComplexH[z](i, j)._Val[_RE] = (f0Mat[z](i, j) - f180Mat[z](i, j))*normalizefactor;
1177  ComplexH[z](i, j)._Val[_IM] = (f90Mat[z](i, j) - f270Mat[z](i, j))*normalizefactor;
1178 
1179  }
1180  }
1181  }
1182  LOG("complex field obtained from 4 psdh\n");
1183  return true;
1184 }