You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "surf.h"
  2. // filter parameters
  3. static const int FKERNELSIZE = 7; //7// for gaussian blur
  4. static const float FSTDDEV = 1.5;
  5. static const int FBLOCKSIZE = 51; //47// for adaptive threshold
  6. static const int FC = -5; //-5
  7. /*
  8. ideataba2109a
  9. android 4.1.1
  10. kernel 3.1.10
  11. bld num: a2109a_a411_03_13_121126_il
  12. hw info: 0.hy16
  13. */
  14. // A33_U70_U708HC_1511235_20160820
  15. // https://cloud.mail.ru/public/e4a01c0091ae/Allwinner%20A33
  16. surfDetector::surfDetector() : basicVideoAnalyzer(), BestOf2NearestMatcher(), SurfFeaturesFinder() {
  17. m_dArea = 20.0;
  18. m_bPIP = false;
  19. type = VA_SURF_DETECT;
  20. m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC);
  21. SimpleBlobDetector::Params PP;
  22. memset(&PP, 0, sizeof(SimpleBlobDetector::Params));
  23. PP.filterByColor = true;
  24. PP.blobColor = 255;
  25. PP.thresholdStep = 47;
  26. PP.minThreshold = 20;
  27. PP.maxThreshold = 200;
  28. // Filter by Area.
  29. PP.filterByArea = true;
  30. PP.minArea = 100.f;
  31. PP.maxArea = 160000.f;
  32. PP.minDistBetweenBlobs = 20.f;
  33. m_pBlobDetector = new SimpleBlobDetector(PP);
  34. //if (m_pSurfDetector == NULL) m_pSurfDetector = new DenseFeatureDetector(1.f, 1, 0.1f, 24, 0, false, false);//new SurfFeatureDetector(MAX_SURF_COUNT);
  35. //if (m_pSurfExtractor == NULL)
  36. //m_pSurfExtractor = new SurfDescriptorExtractor();
  37. //m_pSurfExtractor->extended = true;
  38. //m_pSurfExtractor->hessianThreshold = 400;
  39. //m_pSurfExtractor->upright = true;
  40. int tCount = gpu::getCudaEnabledDeviceCount();
  41. if (tCount){
  42. gpu::setDevice(0);
  43. };
  44. };
  45. surfDetector::surfDetector(double Area) : basicVideoAnalyzer(), BestOf2NearestMatcher() {
  46. //m_iWidth = Width;
  47. //m_iHeight = Height;
  48. m_dArea = Area;
  49. m_bPIP = false;
  50. type = VA_SURF_DETECT;
  51. m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC);
  52. //m_pDetectionCriteria = new TermCriteria( TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03 );
  53. //m_pSurfDetector = NULL;
  54. //m_pSurfExtractor = NULL;
  55. //m_pFlannMatcher = NULL;
  56. SimpleBlobDetector::Params PP;
  57. memset(&PP, 0, sizeof(SimpleBlobDetector::Params));
  58. PP.filterByColor = true;
  59. PP.filterByArea = true;
  60. PP.thresholdStep = 47;
  61. PP.minThreshold = 128;
  62. PP.maxThreshold = 200;
  63. PP.minDistBetweenBlobs = 24.f;
  64. PP.minArea = 256.f;
  65. PP.maxArea = 160000.f;
  66. PP.blobColor = 255;
  67. //if (m_pBlobDetector == NULL)
  68. m_pBlobDetector = new SimpleBlobDetector(PP);
  69. //if (m_pSurfDetector == NULL) m_pSurfDetector = new DenseFeatureDetector();//new SurfFeatureDetector(MAX_SURF_COUNT);
  70. //if (m_pSurfExtractor == NULL)
  71. //m_pSurfExtractor = new SurfDescriptorExtractor();
  72. //if (m_pFlannMatcher == NULL) m_pFlannMatcher = new FlannBasedMatcher();
  73. //if (m_pFlannMatcher == NULL) m_pFlannMatcher = new detail::BestOf2NearestMatcher();
  74. };
  75. surfDetector::~surfDetector(){
  76. //delete(m_pDetectionCriteria);
  77. delete(m_oFilter);
  78. };
  79. /*
  80. unsigned int root(unsigned int x){
  81. unsigned int a,b;
  82. b = x;
  83. a = x = 0x3f;
  84. x = b/x;
  85. a = x = (x+a)>>1;
  86. x = b/x;
  87. a = x = (x+a)>>1;
  88. x = b/x;
  89. x = (x+a)>>1;
  90. return(x);
  91. };
  92. */
  93. bool surfDetector::m_fImageAnalyzer(Mat &Picture){
  94. if( Picture.empty() ) return false;
  95. m_oFilter->filter(Picture);
  96. if( m_oMat.empty() ){
  97. if (m_pBlobDetector != NULL ){//&& m_pSurfExtractor!= NULL){
  98. m_oFilter->thresholdFrame().copyTo(m_oMat);
  99. m_pBlobDetector->detect( m_oFilter->thresholdFrame(), m_aOldKeyPoints);
  100. //m_pSurfExtractor->compute(m_oFilter->filterFrame(), m_aOldKeyPoints, m_oNewImageFutures.descriptors);
  101. };
  102. return false;
  103. };// else m_oFilter->thresholdFrame().copyTo(m_oMat);
  104. m_aOldKeyPoints.clear();
  105. m_aOldKeyPoints = m_aNewKeyPoints;
  106. m_pBlobDetector->detect( m_oFilter->thresholdFrame(), m_aNewKeyPoints);
  107. //m_pSurfExtractor->compute(m_oFilter->filterFrame(), m_aNewKeyPoints, m_oNewImageFutures.descriptors);
  108. if (!m_oNewImageFutures.descriptors.empty() && !m_oOldImageFutures.descriptors.empty()){
  109. match(m_oNewImageFutures, m_oOldImageFutures, m_aMatches);
  110. } else {
  111. if (m_oOldImageFutures.descriptors.empty()) m_oNewImageFutures.descriptors.copyTo(m_oOldImageFutures.descriptors);
  112. return false;
  113. };
  114. if ( m_aMatches.num_inliers ){// ! .matches.empty()){
  115. float max_dist;
  116. float min_dist;
  117. max_dist = 0;
  118. min_dist = 100000;
  119. //-- Quick calculation of max and min distances between keypoints
  120. for (int i = 0; i < m_oNewImageFutures.descriptors.rows; i++){
  121. float dist = m_aMatches.matches[i].distance;
  122. if (dist < min_dist) min_dist = dist;
  123. if (dist > max_dist) max_dist = dist;
  124. };
  125. m_aGoodMatches.matches.clear();
  126. for (int i = 0; i < m_oNewImageFutures.descriptors.rows; i++){
  127. float dist = m_aMatches.matches[i].distance;
  128. if (dist < max(2 * min_dist, 0.02f) ){ // 3xmin
  129. m_aGoodMatches.matches.push_back(m_aMatches.matches[i]);
  130. };
  131. };
  132. m_oNewImageFutures.descriptors.copyTo(m_oOldImageFutures.descriptors);
  133. /*
  134. //-- Localize the object
  135. std::vector<Point2f> mPoint2fNew;
  136. std::vector<Point2f> mPoint2fOld;
  137. for (int i = 0; i < m_aGoodMatches.size(); i++){
  138. //-- Get the keypoints from the good matches
  139. mPoint2fNew.push_back(m_aNewKeyPoints[m_aGoodMatches[i].queryIdx].pt);
  140. mPoint2fOld.push_back(m_aOldKeyPoints[m_aGoodMatches[i].trainIdx].pt);
  141. }
  142. Mat H = findHomography(mPoint2fNew, mPoint2fOld, CV_RANSAC);
  143. //-- Get the corners from the image_1 ( the object to be "detected" )
  144. std::vector<Point2f> obj_corners(4);
  145. obj_corners[0] = cvPoint(0, 0);
  146. obj_corners[1] = cvPoint(m_oFilter->gaussianFrame().cols, 0);
  147. obj_corners[2] = cvPoint(m_oFilter->gaussianFrame().cols, m_oFilter->gaussianFrame().rows);
  148. obj_corners[3] = cvPoint(0, m_oFilter->gaussianFrame().rows);
  149. std::vector<Point2f> scene_corners(4);
  150. perspectiveTransform(obj_corners, scene_corners, H);
  151. */
  152. return true;
  153. };
  154. return false;
  155. };
  156. void surfDetector::markup(Mat &Picture){
  157. m_mBufferProtectorMutex.lock();
  158. //if (!m_oMat.empty()) {
  159. // cvtColor(m_oMat, Picture, CV_GRAY2BGR);
  160. //};
  161. for (int i = 0; i < m_aGoodMatches.matches.size(); i++){
  162. int a, b;
  163. a = m_aGoodMatches.matches[i].trainIdx;
  164. b = m_aGoodMatches.matches[i].queryIdx;
  165. if (a < m_aOldKeyPoints.size() && b < m_aNewKeyPoints.size()){
  166. //circle(Picture, m_aOldKeyPoints[a].pt, 5, Scalar(0, 255, 0), 1, 8);
  167. //circle(Picture, m_aNewKeyPoints[b].pt, 5, Scalar(255, 0, 255), 1, 8);
  168. line(Picture, m_aOldKeyPoints[a].pt, m_aNewKeyPoints[b].pt, Scalar(255, 255, 128), 2);
  169. };
  170. };
  171. for (int i = 0; i<m_aOldKeyPoints.size(); i++)circle(Picture, m_aOldKeyPoints[i].pt, 5, Scalar(255, 0, 0), 1, 8);
  172. for (int i = 0; i<m_aNewKeyPoints.size(); i++)circle(Picture, m_aNewKeyPoints[i].pt, 5, Scalar(0, 0, 255), 1, 8);
  173. if (m_bPIP){
  174. Mat resizedSrc;
  175. Mat subView;
  176. Mat convertedSrc;
  177. if (!m_oFilter->filterFrame().empty()){
  178. resize(m_oFilter->filterFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  179. subView = Picture(Rect((Picture.cols - 510), 50 + 120, 160, 120));
  180. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  181. convertedSrc.copyTo(subView);
  182. };
  183. if (!m_oFilter->gaussianFrame().empty()){
  184. resize(m_oFilter->gaussianFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  185. subView = Picture(Rect((Picture.cols - 340), 50 + 120, 160, 120));
  186. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  187. convertedSrc.copyTo(subView);
  188. };
  189. if (!m_oFilter->thresholdFrame().empty()){
  190. resize(m_oFilter->thresholdFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  191. subView = Picture(Rect((Picture.cols - 170), 50 + 120, 160, 120));
  192. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  193. convertedSrc.copyTo(subView);
  194. };
  195. };
  196. m_mBufferProtectorMutex.unlock();
  197. };