#include "surf.h" // filter parameters static const int FKERNELSIZE = 7; //7// for gaussian blur static const float FSTDDEV = 1.5; static const int FBLOCKSIZE = 51; //47// for adaptive threshold static const int FC = -5; //-5 /* ideataba2109a android 4.1.1 kernel 3.1.10 bld num: a2109a_a411_03_13_121126_il hw info: 0.hy16 */ // A33_U70_U708HC_1511235_20160820 // https://cloud.mail.ru/public/e4a01c0091ae/Allwinner%20A33 surfDetector::surfDetector() : basicVideoAnalyzer(), BestOf2NearestMatcher(), SurfFeaturesFinder() { m_dArea = 20.0; m_bPIP = false; type = VA_SURF_DETECT; m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC); SimpleBlobDetector::Params PP; memset(&PP, 0, sizeof(SimpleBlobDetector::Params)); PP.filterByColor = true; PP.blobColor = 255; PP.thresholdStep = 47; PP.minThreshold = 20; PP.maxThreshold = 200; // Filter by Area. PP.filterByArea = true; PP.minArea = 100.f; PP.maxArea = 160000.f; PP.minDistBetweenBlobs = 20.f; m_pBlobDetector = new SimpleBlobDetector(PP); //if (m_pSurfDetector == NULL) m_pSurfDetector = new DenseFeatureDetector(1.f, 1, 0.1f, 24, 0, false, false);//new SurfFeatureDetector(MAX_SURF_COUNT); //if (m_pSurfExtractor == NULL) //m_pSurfExtractor = new SurfDescriptorExtractor(); //m_pSurfExtractor->extended = true; //m_pSurfExtractor->hessianThreshold = 400; //m_pSurfExtractor->upright = true; int tCount = gpu::getCudaEnabledDeviceCount(); if (tCount){ gpu::setDevice(0); }; }; surfDetector::surfDetector(double Area) : basicVideoAnalyzer(), BestOf2NearestMatcher() { //m_iWidth = Width; //m_iHeight = Height; m_dArea = Area; m_bPIP = false; type = VA_SURF_DETECT; m_oFilter = new filterImage(FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC); //m_pDetectionCriteria = new TermCriteria( TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03 ); //m_pSurfDetector = NULL; //m_pSurfExtractor = NULL; //m_pFlannMatcher = NULL; SimpleBlobDetector::Params PP; memset(&PP, 0, sizeof(SimpleBlobDetector::Params)); PP.filterByColor = true; PP.filterByArea = true; PP.thresholdStep = 47; PP.minThreshold = 128; PP.maxThreshold = 200; PP.minDistBetweenBlobs = 24.f; PP.minArea = 256.f; PP.maxArea = 160000.f; PP.blobColor = 255; //if (m_pBlobDetector == NULL) m_pBlobDetector = new SimpleBlobDetector(PP); //if (m_pSurfDetector == NULL) m_pSurfDetector = new DenseFeatureDetector();//new SurfFeatureDetector(MAX_SURF_COUNT); //if (m_pSurfExtractor == NULL) //m_pSurfExtractor = new SurfDescriptorExtractor(); //if (m_pFlannMatcher == NULL) m_pFlannMatcher = new FlannBasedMatcher(); //if (m_pFlannMatcher == NULL) m_pFlannMatcher = new detail::BestOf2NearestMatcher(); }; surfDetector::~surfDetector(){ //delete(m_pDetectionCriteria); delete(m_oFilter); }; /* unsigned int root(unsigned int x){ unsigned int a,b; b = x; a = x = 0x3f; x = b/x; a = x = (x+a)>>1; x = b/x; a = x = (x+a)>>1; x = b/x; x = (x+a)>>1; return(x); }; */ bool surfDetector::m_fImageAnalyzer(Mat &Picture){ if( Picture.empty() ) return false; m_oFilter->filter(Picture); if( m_oMat.empty() ){ if (m_pBlobDetector != NULL ){//&& m_pSurfExtractor!= NULL){ m_oFilter->thresholdFrame().copyTo(m_oMat); m_pBlobDetector->detect( m_oFilter->thresholdFrame(), m_aOldKeyPoints); //m_pSurfExtractor->compute(m_oFilter->filterFrame(), m_aOldKeyPoints, m_oNewImageFutures.descriptors); }; return false; };// else m_oFilter->thresholdFrame().copyTo(m_oMat); m_aOldKeyPoints.clear(); m_aOldKeyPoints = m_aNewKeyPoints; m_pBlobDetector->detect( m_oFilter->thresholdFrame(), m_aNewKeyPoints); //m_pSurfExtractor->compute(m_oFilter->filterFrame(), m_aNewKeyPoints, m_oNewImageFutures.descriptors); if (!m_oNewImageFutures.descriptors.empty() && !m_oOldImageFutures.descriptors.empty()){ match(m_oNewImageFutures, m_oOldImageFutures, m_aMatches); } else { if (m_oOldImageFutures.descriptors.empty()) m_oNewImageFutures.descriptors.copyTo(m_oOldImageFutures.descriptors); return false; }; if ( m_aMatches.num_inliers ){// ! .matches.empty()){ float max_dist; float min_dist; max_dist = 0; min_dist = 100000; //-- Quick calculation of max and min distances between keypoints for (int i = 0; i < m_oNewImageFutures.descriptors.rows; i++){ float dist = m_aMatches.matches[i].distance; if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; }; m_aGoodMatches.matches.clear(); for (int i = 0; i < m_oNewImageFutures.descriptors.rows; i++){ float dist = m_aMatches.matches[i].distance; if (dist < max(2 * min_dist, 0.02f) ){ // 3xmin m_aGoodMatches.matches.push_back(m_aMatches.matches[i]); }; }; m_oNewImageFutures.descriptors.copyTo(m_oOldImageFutures.descriptors); /* //-- Localize the object std::vector mPoint2fNew; std::vector mPoint2fOld; for (int i = 0; i < m_aGoodMatches.size(); i++){ //-- Get the keypoints from the good matches mPoint2fNew.push_back(m_aNewKeyPoints[m_aGoodMatches[i].queryIdx].pt); mPoint2fOld.push_back(m_aOldKeyPoints[m_aGoodMatches[i].trainIdx].pt); } Mat H = findHomography(mPoint2fNew, mPoint2fOld, CV_RANSAC); //-- Get the corners from the image_1 ( the object to be "detected" ) std::vector obj_corners(4); obj_corners[0] = cvPoint(0, 0); obj_corners[1] = cvPoint(m_oFilter->gaussianFrame().cols, 0); obj_corners[2] = cvPoint(m_oFilter->gaussianFrame().cols, m_oFilter->gaussianFrame().rows); obj_corners[3] = cvPoint(0, m_oFilter->gaussianFrame().rows); std::vector scene_corners(4); perspectiveTransform(obj_corners, scene_corners, H); */ return true; }; return false; }; void surfDetector::markup(Mat &Picture){ m_mBufferProtectorMutex.lock(); //if (!m_oMat.empty()) { // cvtColor(m_oMat, Picture, CV_GRAY2BGR); //}; for (int i = 0; i < m_aGoodMatches.matches.size(); i++){ int a, b; a = m_aGoodMatches.matches[i].trainIdx; b = m_aGoodMatches.matches[i].queryIdx; if (a < m_aOldKeyPoints.size() && b < m_aNewKeyPoints.size()){ //circle(Picture, m_aOldKeyPoints[a].pt, 5, Scalar(0, 255, 0), 1, 8); //circle(Picture, m_aNewKeyPoints[b].pt, 5, Scalar(255, 0, 255), 1, 8); line(Picture, m_aOldKeyPoints[a].pt, m_aNewKeyPoints[b].pt, Scalar(255, 255, 128), 2); }; }; for (int i = 0; ifilterFrame().empty()){ resize(m_oFilter->filterFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA); subView = Picture(Rect((Picture.cols - 510), 50 + 120, 160, 120)); cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR); convertedSrc.copyTo(subView); }; if (!m_oFilter->gaussianFrame().empty()){ resize(m_oFilter->gaussianFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA); subView = Picture(Rect((Picture.cols - 340), 50 + 120, 160, 120)); cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR); convertedSrc.copyTo(subView); }; if (!m_oFilter->thresholdFrame().empty()){ resize(m_oFilter->thresholdFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA); subView = Picture(Rect((Picture.cols - 170), 50 + 120, 160, 120)); cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR); convertedSrc.copyTo(subView); }; }; m_mBufferProtectorMutex.unlock(); };