void detectAndDrawObjects( Mat& image, LatentSvmDetector& detector, const vector<Scalar>& colors, float overlapThreshold, int numThreads ) { vector<LatentSvmDetector::ObjectDetection> detections; TickMeter tm; tm.start(); detector.detect( image, detections, overlapThreshold, numThreads); tm.stop(); cout << "Detection time = " << tm.getTimeSec() << " sec" << endl; const vector<string> classNames = detector.getClassNames(); CV_Assert( colors.size() == classNames.size() ); for( size_t i = 0; i < detections.size(); i++ ) { const LatentSvmDetector::ObjectDetection& od = detections[i]; rectangle( image, od.rect, colors[od.classID], 3 ); } // put text over the all rectangles for( size_t i = 0; i < detections.size(); i++ ) { const LatentSvmDetector::ObjectDetection& od = detections[i]; putText( image, classNames[od.classID], Point(od.rect.x+4,od.rect.y+13), FONT_HERSHEY_SIMPLEX, 0.55, colors[od.classID], 2 ); } }
static void detectarobjetos(Mat& image, LatentSvmDetector& detector, float overlapThreshold, int numThreads, Mat& imageMapaDisp) { int cont = 1; Mat mapaDisparidad = imread("mapaDisparidad.png", CV_8UC1); vector<LatentSvmDetector::ObjectDetection> detections; detector.detect(image, detections, overlapThreshold, numThreads); FileStorage distancias("distancias.yml", FileStorage::WRITE); for( size_t i = 0; i < detections.size(); i++ ) { const LatentSvmDetector::ObjectDetection& od = detections[i]; rectangle(image, od.rect, CV_RGB(255, 0, 0), 2); rectangle(imageMapaDisp, od.rect, CV_RGB(255, 0, 0), 2); int height = od.rect.height; int width = od.rect.width; int x = od.rect.y; int y = od.rect.x; int dispMax = -1; int disp; for(int h = x; h<=height+x; h++) { for(int w = y; w<=width+y; w++) { disp = mapaDisparidad.at<uchar>(h,w); if(disp>dispMax) dispMax = disp; } } int Distancia = ((1100*85)/dispMax)/10; string DistanciaObj = static_cast<ostringstream*>(&(ostringstream() << Distancia << " cm."))->str(); putText(image, DistanciaObj, Point(od.rect.x+4, od.rect.y+13), FONT_HERSHEY_SIMPLEX, 0.55, CV_RGB(255, 255, 255), 2); putText(imageMapaDisp, DistanciaObj, Point(od.rect.x+4, od.rect.y+13), FONT_HERSHEY_SIMPLEX, 0.55, CV_RGB(255, 255, 255), 2); //Guardar distancias en archivo Point coordenada; coordenada.x = x; coordenada.y = y; Size dimension; dimension.height = height; dimension.width = width; distancias << "Objeto" << cont; distancias << "Distancia" << Distancia; distancias << "Coordenada" << coordenada; distancias << "Dimension" << dimension; cont++; } distancias.release(); }