PageRenderTime 593ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/oculawince/DLL/DLL.cpp

https://github.com/makerere-compute/ocula
C++ | 196 lines | 140 code | 28 blank | 28 comment | 6 complexity | 455d50fa254824b285f0f668c072bd88 MD5 | raw file
  1. /**
  2. @author mistaguy
  3. MALERIA OPENCV MODULE
  4. sorce file for ocula wm
  5. **/
  6. #include "stdafx.h"
  7. #include "DLL.h"
  8. //Main function, defines the entry point for the program.
  9. BOOL APIENTRY DllMain( HANDLE hModule,
  10. DWORD ul_reason_for_call,
  11. LPVOID lpReserved )
  12. {
  13. switch (ul_reason_for_call)
  14. {
  15. case DLL_PROCESS_ATTACH:
  16. case DLL_THREAD_ATTACH:
  17. case DLL_THREAD_DETACH:
  18. case DLL_PROCESS_DETACH:
  19. break;
  20. }
  21. return TRUE;
  22. }
  23. //find matches
  24. CvSeq *findmatches(CvSeq imageDescriptors[],float mu[],float inverse_sigma[],int threshold)
  25. {
  26. /**
  27. For each of the keypoint descriptors, calculate whether they match the
  28. training examples given a mean and inverse variance of the training
  29. descriptors.
  30. **/
  31. // initialise linked list for storing indices of matching descriptors
  32. CvSeqWriter writer;
  33. CvMemStorage* storage = cvCreateMemStorage(0);
  34. cvStartWriteSeq(CV_32S, sizeof(CvSeq), sizeof(0), storage, &writer );
  35. int desc_num=imageDescriptors->total;
  36. for (int k =0;k<desc_num;k++){
  37. const float* des = (const float*)cvGetSeqElem(imageDescriptors, k);
  38. // squared Mahalanobis distance, assuming diagonal covariance
  39. float dist = 0;
  40. for(int i=0;i<64;i++)
  41. {
  42. dist += ((pow((des[i]-mu[i]),2))*inverse_sigma[i]);
  43. }
  44. if (dist<threshold){
  45. CV_WRITE_SEQ_ELEM(k, writer);
  46. }
  47. }
  48. CvSeq* matches = cvEndWriteSeq( &writer );
  49. //release memory
  50. cvReleaseMemStorage(&storage);
  51. return matches;
  52. }
  53. //Grab Current Mahalnobis distance
  54. DLL_API int getMatchThreshold()
  55. {
  56. return MATCH_THRESHOLD;
  57. }
  58. //Get Current Matched Surf features
  59. DLL_API int getMatches()
  60. {
  61. return MATCHES;
  62. }
  63. //Grab Current Features Detected on the image
  64. DLL_API int getFeatures()
  65. {
  66. return FEATURES;
  67. }
  68. //set Mahalnobis distance
  69. DLL_API void setMatchThreshold(int t)
  70. {
  71. MATCH_THRESHOLD=t;
  72. }
  73. //function to detect maleria parasite
  74. DLL_API short detectParasite(const char * in_file)
  75. {
  76. short msgint=0;
  77. try
  78. {
  79. //create memory for use
  80. CvMemStorage* storage = cvCreateMemStorage(0);
  81. //colors to be used
  82. static CvScalar colors[] =
  83. {
  84. {{0,0,255}},
  85. {{0,128,255}},
  86. {{0,255,255}},
  87. {{0,255,0}},
  88. {{255,128,0}},
  89. {{255,255,0}},
  90. {{255,0,0}},
  91. {{255,0,255}},
  92. {{255,255,255}}
  93. };
  94. IplImage* object = cvLoadImage( in_file);
  95. CvSeq *objectKeypoints = 0, *objectDescriptors = 0;
  96. CvSeq *imageKeypoints = 0, *imageDescriptors = 0;
  97. //extract keypoints and descriptors from this frame
  98. CvSURFParams params = cvSURFParams(SURF_HESSIAN_THRESHOLD, SURF_EXTENDED);
  99. params.nOctaves=SURF_NOCTAVES;
  100. params.nOctaveLayers=SURF_NOCTAVELAYERS;
  101. IplImage* imcolour;
  102. static CvScalar colour_red = {{255,0,0}};
  103. //magic numbers from surftraining.py
  104. float mu[] = {5.819627e-04, -1.362842e-03, 2.391441e-03, 2.645388e-03,
  105. 1.736164e-02, -1.230714e-02, 2.497016e-02, 1.932175e-02,
  106. 1.357752e-02, -5.328481e-03, 1.746906e-02, 1.109308e-02,
  107. 4.588704e-04, 1.048637e-04, 9.707647e-04, 8.380755e-04,
  108. 1.770340e-03, -4.429551e-03, 1.501682e-02, 1.513996e-02,
  109. -4.374340e-04, -1.287441e-01, 2.782514e-01, 1.948743e-01,
  110. 2.738553e-01, -1.216200e-01, 2.884202e-01, 1.637557e-01,
  111. 3.944878e-03, 1.265369e-03, 6.518393e-03, 5.406772e-03,
  112. 3.621008e-03, 3.041889e-03, 1.612071e-02, 1.579410e-02,
  113. -1.054817e-02, 1.283805e-01, 2.812133e-01, 2.028567e-01,
  114. 2.837351e-01, 1.170555e-01, 2.924680e-01, 1.694813e-01,
  115. 3.927908e-03, -9.551742e-04, 6.768408e-03, 6.973732e-03,
  116. 6.549640e-04, 2.033027e-03, 2.435575e-03, 2.853759e-03,
  117. 1.458403e-02, 1.152258e-02, 2.540248e-02, 2.062079e-02,
  118. 1.357735e-02, 1.539471e-03, 1.763335e-02, 1.248309e-02,
  119. 7.774467e-04, -6.445289e-04, 1.358011e-03, 1.293342e-03};
  120. float inverse_sigma[] = {6.801185e+04, 6.588168e+04, 9.711188e+04, 8.665568e+04,
  121. 1.158875e+03, 2.371370e+03, 1.567239e+03, 3.626427e+03,
  122. 2.683483e+03, 5.881650e+03, 3.250738e+03, 8.754201e+03,
  123. 5.403449e+05, 8.249664e+05, 6.542625e+05, 8.664666e+05,
  124. 2.302975e+03, 2.368799e+03, 3.773819e+03, 3.477313e+03,
  125. 1.244203e+01, 6.123593e+01, 8.211791e+01, 1.743040e+02,
  126. 7.084477e+01, 8.174691e+01, 1.041477e+02, 1.523873e+02,
  127. 1.958434e+04, 1.445799e+04, 2.428065e+04, 1.636511e+04,
  128. 1.742460e+03, 1.978514e+03, 2.543752e+03, 2.813858e+03,
  129. 1.284125e+01, 5.745215e+01, 8.913852e+01, 1.617787e+02,
  130. 1.000989e+02, 7.500543e+01, 1.201700e+02, 1.758948e+02,
  131. 1.844502e+04, 6.310191e+03, 2.515136e+04, 7.853815e+03,
  132. 7.065411e+04, 8.797910e+04, 9.633089e+04, 1.050358e+05,
  133. 1.138238e+03, 2.344646e+03, 1.575057e+03, 3.568681e+03,
  134. 2.623678e+03, 4.360050e+03, 3.405330e+03, 7.129815e+03,
  135. 1.734295e+05, 1.515146e+05, 1.806751e+05, 1.696722e+05};
  136. IplImage* img = cvCreateImage(cvGetSize(object), IPL_DEPTH_8U, 1);
  137. cvCvtColor(object, img, CV_BGR2GRAY);
  138. IplImage* bigimage=cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 1);
  139. cvResize(img,bigimage,0);
  140. cvExtractSURF( bigimage, 0, &objectKeypoints, &objectDescriptors, storage, params );
  141. FEATURES=objectKeypoints->total;
  142. if (sizeof(objectKeypoints)>0){
  143. CvSeq *matches = findmatches(objectDescriptors, mu, inverse_sigma, MATCH_THRESHOLD);
  144. MATCHES= matches->total;
  145. for( int i = 0; i <MATCHES; i++ )
  146. {
  147. int *k = (int*)cvGetSeqElem(matches, i);
  148. CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem(objectKeypoints, *k);
  149. CvPoint center;
  150. int radius;
  151. center.x = cvRound(r->pt.x);
  152. center.y = cvRound(r->pt.y);
  153. radius = cvRound(15);
  154. cvCircle(bigimage, center, radius,colors[6], 2, 8, 0 );
  155. }
  156. }//end if
  157. msgint=1;
  158. ////release image resources
  159. cvSaveImage("\\Program Files\\ocula\\parasite.jpg",bigimage);
  160. cvReleaseImage(&img);
  161. // cvReleaseImage(&object);
  162. //cvReleaseImage(&imcolour);
  163. cvReleaseMemStorage(&storage);
  164. // cvReleaseImage(&bigimage);
  165. }
  166. catch(Exception_info_ptr p){}
  167. return msgint;
  168. }