GainMatrix.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #include "GainMatrix.h"
  2. #include "String.Format.tlh"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. CGainMatrix* CGainMatrix::m_instance = NULL;
  9. #define FULL_IMG_WIDTH 3072L
  10. #define FULL_IMG_HEIGHT 2560L
  11. const int nGAINMIN = 65535;
  12. //extern unsigned w_gOffset; // = 50;
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CGainMatrix::CGainMatrix(int width, int height,int woffset, int hoffset,int refrencenum,unsigned short maxpv)
  17. :m_nHeight(height), m_nWidth(width), m_nHOffset(hoffset), m_nWOffset(woffset),m_nRefrenceNum(refrencenum),m_nMaxPV(maxpv)
  18. {
  19. int i = 0;
  20. m_wGainBuffer = new unsigned short*[m_nRefrenceNum];
  21. memset(m_wGainBuffer, 0, m_nRefrenceNum * sizeof(unsigned short));
  22. for(i = 0; i< m_nRefrenceNum;i++)
  23. {
  24. m_wGainBuffer[i]=NULL;
  25. }
  26. m_nAverage = new int[m_nRefrenceNum];
  27. for(i = 0; i< m_nRefrenceNum;i++)
  28. {
  29. m_nAverage[i] = 0;
  30. }
  31. m_nRefrenceCount = 0;
  32. w_gOffset = 50;
  33. m_nAverageCts = 0;
  34. m_nCurRefIndex = -1;
  35. }
  36. CGainMatrix::~CGainMatrix()
  37. {
  38. if(m_wGainBuffer != NULL)
  39. {
  40. for(int i = 0; i< m_nRefrenceNum;i++)
  41. {
  42. if(m_wGainBuffer[i] != NULL)
  43. {
  44. delete[] m_wGainBuffer[i];
  45. }
  46. }
  47. delete[] m_wGainBuffer;
  48. }
  49. m_wGainBuffer = NULL;
  50. //if(m_wTempGainBuffer != NULL)
  51. //{
  52. // for(int i = 0; i< m_nRefrenceNum;i++)
  53. // {
  54. // if(m_wTempGainBuffer[i] != NULL)
  55. // {
  56. // delete[] m_wTempGainBuffer[i];
  57. // }
  58. // }
  59. // delete[] m_wTempGainBuffer;
  60. //}
  61. //m_wTempGainBuffer = NULL;
  62. if(m_nAverage)
  63. {
  64. delete[] m_nAverage;
  65. m_nAverage = NULL;
  66. }
  67. }
  68. CGainMatrix* CGainMatrix::Instance()
  69. {
  70. if(m_instance == NULL) {
  71. m_instance = new CGainMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 0,0,1,16383);
  72. }
  73. return m_instance;
  74. }
  75. void CGainMatrix::ApplyGainMap(unsigned short *wImage)
  76. {
  77. // 改成3幅图: 分别是500,1000,1500;
  78. Dunsigned short temp;
  79. int i,j,k,offset;
  80. for( i = 0;i <m_nRefrenceNum;i++)
  81. {
  82. if(m_wGainBuffer[i] == NULL)
  83. {
  84. return;
  85. }
  86. }
  87. for(offset = 0; offset < m_nWidth*m_nHOffset; offset++) {
  88. wImage[offset] = m_nMaxPV;
  89. }
  90. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i++) {
  91. offset = i*m_nWidth;
  92. for(j = 0; j<m_nWOffset; j++)
  93. {
  94. wImage[offset] = m_nMaxPV;
  95. offset++;
  96. }
  97. for(;j<m_nWidth-m_nWOffset; j++)
  98. {
  99. if(m_nRefrenceNum == 1)
  100. {
  101. if(*(m_wGainBuffer[0]+i*m_nWidth+j)==0)
  102. {
  103. offset++;
  104. continue;
  105. }
  106. temp = *(wImage+i*m_nWidth+j)*m_nAverage[0]/(*(m_wGainBuffer[0]+i*m_nWidth+j))+w_gOffset;
  107. }
  108. else
  109. {
  110. if(*(wImage+i*m_nWidth+j)<*(m_wGainBuffer[0]+i*m_nWidth+j))
  111. {
  112. if(*(m_wGainBuffer[0]+i*m_nWidth+j)==0)
  113. {
  114. offset++;
  115. continue;
  116. }
  117. temp = *(wImage+i*m_nWidth+j)*m_nAverage[0]/(*(m_wGainBuffer[0]+i*m_nWidth+j))+w_gOffset;
  118. }
  119. else
  120. {
  121. for(k=0;k<m_nRefrenceNum-1;k++)
  122. {
  123. if(*(wImage+i*m_nWidth+j)>=*(m_wGainBuffer[k]+i*m_nWidth+j)&&*(wImage+i*m_nWidth+j)<*(m_wGainBuffer[k+1]+i*m_nWidth+j))
  124. break;
  125. }
  126. if (k==m_nRefrenceNum-1) k = max(0,(k-1));
  127. if((k+1) <=(m_nRefrenceNum-1))
  128. {
  129. if((*(m_wGainBuffer[k+1]+i*m_nWidth+j)-*(m_wGainBuffer[k]+i*m_nWidth+j))==0)
  130. {
  131. offset++;
  132. continue;
  133. }
  134. }
  135. temp = m_nAverage[k]+(*(wImage+i*m_nWidth+j)-*(m_wGainBuffer[k]+i*m_nWidth+j))*(m_nAverage[k+1]-m_nAverage[k])/(*(m_wGainBuffer[k+1]+i*m_nWidth+j)-*(m_wGainBuffer[k]+i*m_nWidth+j))+w_gOffset;
  136. }
  137. }
  138. if (temp>m_nMaxPV)
  139. *(wImage+i*m_nWidth+j) = m_nMaxPV;
  140. else
  141. *(wImage+i*m_nWidth+j) = unsigned short(temp);
  142. offset++;
  143. }
  144. for(;j<m_nWidth; j++)
  145. {
  146. wImage[offset] = m_nMaxPV;
  147. offset++;
  148. }
  149. }
  150. for(offset = i*m_nWidth; offset < m_nWidth*m_nHeight; offset++) {
  151. wImage[offset] = m_nMaxPV;
  152. }
  153. }
  154. bool CGainMatrix::LoadGainMap(void)
  155. {
  156. if(m_nRefrenceCount >= m_nRefrenceNum)
  157. {
  158. return false;
  159. }
  160. if(m_wGainBuffer[m_nRefrenceCount] != NULL)
  161. {
  162. delete [] m_wGainBuffer[m_nRefrenceCount];
  163. }
  164. m_wGainBuffer[m_nRefrenceCount] = new unsigned short [m_nHeight*m_nWidth];
  165. if(m_wGainBuffer[m_nRefrenceCount] == NULL)
  166. {
  167. return false;
  168. }
  169. m_nRefrenceCount++;
  170. return true;
  171. }
  172. bool CGainMatrix::LoadGainMap(const char *filename)
  173. {
  174. if(m_nRefrenceCount >= m_nRefrenceNum)
  175. {
  176. return false;
  177. }
  178. if(m_wGainBuffer[m_nRefrenceCount] != NULL) {
  179. delete [] m_wGainBuffer[m_nRefrenceCount];
  180. }
  181. m_wGainBuffer[m_nRefrenceCount] = new unsigned short [m_nHeight*m_nWidth];
  182. if(m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
  183. //
  184. // Check if the file exist or not
  185. //
  186. FILE * fp = fopen (filename, "rb");
  187. if (! fp)
  188. {
  189. return false;
  190. }
  191. if(fread( m_wGainBuffer[m_nRefrenceCount], 1,(UINT)(m_nHeight*m_nWidth*sizeof(unsigned short)),fp) != (UINT)(m_nHeight*m_nWidth*sizeof(unsigned short)))
  192. {
  193. fclose(fp);
  194. return false;
  195. }
  196. fclose(fp);
  197. //calculate avg pv
  198. ULONGLONG dwSum = 0;
  199. Dunsigned short dwIndex = 0;
  200. int i, j;
  201. Dunsigned short dwCts = 0;
  202. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8) {
  203. dwIndex = i*m_nWidth;
  204. for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8) {
  205. dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
  206. dwCts++;
  207. }
  208. }
  209. m_nAverage[m_nRefrenceCount] = dwSum/dwCts;
  210. char szOut[128];
  211. sprintf(szOut, "LoadGainMap m_nAverage[%i]=%i\n", m_nRefrenceCount, m_nAverage[m_nRefrenceCount]);
  212. OutputDebugStringA(szOut);
  213. m_nRefrenceCount++;
  214. if(m_nRefrenceCount == m_nRefrenceNum)
  215. {
  216. PlaceRefrenceInOrder();
  217. }
  218. /*CString strLog;
  219. strLog.Format("The average pixel value of current node is %d", m_nAverage[m_nRefrenceCount]);
  220. m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);*/
  221. return true;
  222. }
  223. bool CGainMatrix::LoadGainMap(unsigned short* wImage)
  224. {
  225. if(m_nRefrenceCount >= m_nRefrenceNum)
  226. {
  227. return false;
  228. }
  229. if(wImage == NULL)
  230. {
  231. return false;
  232. }
  233. if(m_wGainBuffer[m_nRefrenceCount] == NULL) {
  234. m_wGainBuffer[m_nRefrenceCount] = new unsigned short [m_nHeight*m_nWidth];
  235. }
  236. if(m_wGainBuffer[m_nRefrenceCount] == NULL) return false;
  237. //
  238. // Check if the file exist or not
  239. //
  240. memcpy(m_wGainBuffer[m_nRefrenceCount],wImage,(UINT)(m_nHeight*m_nWidth*sizeof(unsigned short)));
  241. //calculate avg pv
  242. //Dunsigned short dwSum = 0;//delelte by song 2014-9-27 如果像素较多且值很大会越界
  243. LONGLONG dwSum = 0;//add by song 2014-9-27 64位类型,防止越界
  244. Dunsigned short dwIndex = 0;
  245. int i, j;
  246. Dunsigned short dwCts = 0;
  247. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8) {
  248. dwIndex = i*m_nWidth;
  249. for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8) {
  250. dwSum += *(m_wGainBuffer[m_nRefrenceCount] + dwIndex + j);
  251. dwCts++;
  252. }
  253. }
  254. m_nAverage[m_nRefrenceCount] = dwSum/dwCts;
  255. m_nRefrenceCount++;
  256. if(m_nRefrenceCount == m_nRefrenceNum)
  257. {
  258. PlaceRefrenceInOrder();
  259. }
  260. return true;
  261. }
  262. void CGainMatrix::PlaceRefrenceInOrder()
  263. {
  264. //code delete 20101214
  265. //unsigned short nMin = 16383;
  266. //code begin 20101214
  267. unsigned short nMin = nGAINMIN;
  268. //code end 20101214
  269. unsigned short nMin_Index = 0;
  270. unsigned short nTemp = 0;
  271. unsigned short* nTempPtr = NULL;
  272. int i,j;
  273. for( i = 0;i <m_nRefrenceNum;i++)
  274. {
  275. if(m_wGainBuffer[i] == NULL)
  276. {
  277. return;
  278. }
  279. }
  280. for(i = 0;i<m_nRefrenceNum;i++)
  281. {
  282. //code delete 20101214
  283. //nMin = 16383;
  284. //code begin 20101214
  285. nMin = nGAINMIN;
  286. //code end 20101214
  287. nMin_Index = 0;
  288. for(j = i; j< m_nRefrenceNum; j++)
  289. {
  290. if(m_nAverage[j] <= nMin)
  291. {
  292. nMin = m_nAverage[j];
  293. nMin_Index = j;
  294. }
  295. }
  296. nTemp = m_nAverage[i];
  297. m_nAverage[i] = m_nAverage[nMin_Index];
  298. m_nAverage[nMin_Index] = nTemp;
  299. nTempPtr = m_wGainBuffer[i];
  300. m_wGainBuffer[i] = m_wGainBuffer[nMin_Index];
  301. m_wGainBuffer[nMin_Index] = nTempPtr;
  302. }
  303. }
  304. bool CGainMatrix::AverageGainMap(unsigned short* wImage, int nRefIndex)
  305. {
  306. int dwCts = m_nWidth*m_nHeight;
  307. if(NULL == m_wGainBuffer[nRefIndex])
  308. {
  309. return false;
  310. }
  311. if (m_nCurRefIndex != nRefIndex)
  312. {
  313. m_nCurRefIndex = nRefIndex;
  314. m_nAverageCts = 0;
  315. memcpy(m_wGainBuffer[nRefIndex], wImage, dwCts*sizeof(unsigned short));
  316. m_nAverageCts ++;
  317. }
  318. else
  319. {
  320. for(int i = 0; i<dwCts; i++)
  321. {
  322. m_wGainBuffer[nRefIndex][i] = unsigned short(float(m_wGainBuffer[nRefIndex][i])*m_nAverageCts/(m_nAverageCts+1) + float(wImage[i])/(m_nAverageCts+1));
  323. }
  324. m_nAverageCts++;
  325. }
  326. return true;
  327. }
  328. bool CGainMatrix::AverageGainMap(unsigned short* wImage, int refenceindex, bool bStart)
  329. {
  330. int dwCts = m_nWidth*m_nHeight;
  331. if(m_wGainBuffer[refenceindex] == NULL)
  332. return false;
  333. if(bStart)
  334. {
  335. m_nAverageCts = 0;
  336. memcpy(m_wGainBuffer[refenceindex], wImage, dwCts*sizeof(unsigned short));
  337. m_nAverageCts ++;
  338. return true;
  339. }
  340. else
  341. {
  342. //average offset
  343. for(int i = 0; i < dwCts; i++)
  344. {
  345. m_wGainBuffer[refenceindex][i] = unsigned short(float(m_wGainBuffer[refenceindex][i])*m_nAverageCts/(m_nAverageCts+1) + float(wImage[i])/(m_nAverageCts+1));
  346. }
  347. m_nAverageCts++;
  348. return true;
  349. }
  350. /*if (refenceindex >= m_nRefrenceNum || refenceindex < 0)
  351. {
  352. m_pLogFile->WriteLog("The reference index is out of range", LOG_INFORMATION, LOG_DEBUG, true);
  353. return false;
  354. }
  355. int dwCts = m_nWidth*m_nHeight;
  356. if(m_wTempGainBuffer[refenceindex] == NULL)
  357. return false;
  358. memset(m_wTempGainBuffer[refenceindex], 0, m_nHeight * m_nWidth * sizeof(unsigned short));
  359. if(bStart)
  360. {
  361. m_nAverageCts = 0;
  362. memcpy(m_wTempGainBuffer[refenceindex], wImage, dwCts*sizeof(unsigned short));
  363. m_nAverageCts ++;
  364. return true;
  365. }
  366. else
  367. {
  368. //average offset
  369. for(int i = 0; i < dwCts; i++)
  370. {
  371. m_wTempGainBuffer[refenceindex][i] = unsigned short(float((m_wTempGainBuffer[refenceindex][i])*m_nAverageCts/(m_nAverageCts+1)) + float(wImage[i])/(m_nAverageCts+1));
  372. }
  373. m_nAverageCts++;
  374. //do it at here //test
  375. //calculate avg pv
  376. Dunsigned short dwSum = 0;
  377. Dunsigned short dwIndex = 0;
  378. int i, j;
  379. Dunsigned short dwCts = 0;
  380. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
  381. {
  382. dwIndex = i*m_nWidth;
  383. for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
  384. {
  385. dwSum += *(m_wTempGainBuffer[refenceindex] + dwIndex + j);
  386. dwCts++;
  387. }
  388. }
  389. int nAverage = dwSum/dwCts;
  390. CString strLog;
  391. strLog.Format("The average pixel value of current node is %d", nAverage);
  392. m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
  393. //test end
  394. return true;
  395. }*/
  396. return true;
  397. }
  398. //replace the old gain map, do it after calibration
  399. /*bool CGainMatrix::ApplyGainCalibration()
  400. {
  401. //replace the old gain map
  402. for (int refenceindex = 0; refenceindex < m_nRefrenceNum; refenceindex++)
  403. {
  404. if(m_wTempGainBuffer[refenceindex] == NULL || m_wGainBuffer[refenceindex] == NULL)
  405. {
  406. AfxMessageBox("No gain buffer exists");
  407. return false;
  408. }
  409. memcpy(m_wGainBuffer[refenceindex], m_wTempGainBuffer[refenceindex], m_nHeight*m_nWidth*sizeof(unsigned short));
  410. delete [] m_wTempGainBuffer[refenceindex];
  411. //do it at here
  412. //calculate avg pv
  413. Dunsigned short dwSum = 0;
  414. Dunsigned short dwIndex = 0;
  415. int i, j;
  416. Dunsigned short dwCts = 0;
  417. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
  418. {
  419. dwIndex = i*m_nWidth;
  420. for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
  421. {
  422. dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
  423. dwCts++;
  424. }
  425. }
  426. m_nAverage[refenceindex] = dwSum/dwCts;
  427. CString strLog;
  428. strLog.Format("The average pixel value of current node is %d", m_nAverage[refenceindex]);
  429. m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
  430. }
  431. return true;
  432. }*/
  433. bool CGainMatrix::StoreGainMap(const char* filename, int refenceindex)
  434. {
  435. if (refenceindex>m_nRefrenceNum) return false;
  436. if(m_wGainBuffer[refenceindex] == NULL) {
  437. //AfxMessageBox("No gain buffer exists");
  438. return false;
  439. }
  440. //do it at here
  441. //calculate avg pv
  442. ULONGLONG dwSum = 0;
  443. Dunsigned short dwIndex = 0;
  444. int i, j;
  445. Dunsigned short dwCts = 0;
  446. for(i = m_nHOffset; i < m_nHeight-m_nHOffset; i += 8)
  447. {
  448. dwIndex = i*m_nWidth;
  449. for(j = m_nWOffset; j < m_nWidth-m_nWOffset; j+= 8)
  450. {
  451. dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
  452. dwCts++;
  453. }
  454. }
  455. m_nAverage[refenceindex] = dwSum/dwCts;
  456. //CString strLog;
  457. //strLog.Format("The average pixel value of current node 3008 x 3072 is %d", m_nAverage[refenceindex]);
  458. //m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);
  459. {
  460. ULONGLONG dwSum = 0;
  461. Dunsigned short dwIndex = 0;
  462. int i, j;
  463. Dunsigned short dwCts = 0;
  464. for(i = 0; i < m_nHeight; i += 8)
  465. {
  466. dwIndex = i*m_nWidth;
  467. for(j = 0; j < m_nWidth; j+= 8)
  468. {
  469. dwSum += *(m_wGainBuffer[refenceindex] + dwIndex + j);
  470. dwCts++;
  471. }
  472. }
  473. int nAverage = dwSum/dwCts;
  474. /*CString strLog;
  475. strLog.Format("The average pixel value of current node 3072 x 3072is %d", nAverage);
  476. m_pLogFile->WriteLog(strLog, LOG_INFORMATION, LOG_DEBUG, true);*/
  477. }
  478. try {
  479. FILE *fp;
  480. fp = fopen(filename, "wb");
  481. if (fwrite(m_wGainBuffer[refenceindex],1,m_nHeight*m_nWidth*sizeof(unsigned short),fp)!=m_nHeight*m_nWidth*sizeof(unsigned short))
  482. {
  483. ASSERT("123456");
  484. }
  485. fclose(fp);
  486. //CFile file( filename, CFile::modeWrite|CFile::modeCreate);
  487. //file.Write(m_wGainBuffer[refenceindex], (UINT)(m_nHeight*m_nWidth*sizeof(unsigned short)));
  488. //file.Close();
  489. //return true;
  490. }
  491. catch (.../*CFileException &e*/)
  492. {
  493. #ifdef _DEBUG
  494. // afxDump << "File could not be opened " << e->m_cause << "\n";
  495. #endif
  496. // (void)e;
  497. return false;
  498. }
  499. return true;
  500. }