GainMatrix.cpp 13 KB

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