本文整理汇总了C++中cv::CascadeClassifier::detectMultiScale方法的典型用法代码示例。如果您正苦于以下问题:C++ CascadeClassifier::detectMultiScale方法的具体用法?C++ CascadeClassifier::detectMultiScale怎么用?C++ CascadeClassifier::detectMultiScale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv::CascadeClassifier
的用法示例。
在下文中一共展示了CascadeClassifier::detectMultiScale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: detect
void FaceDetector::detect( cv::Mat & frame )
{
using namespace cv;
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade_.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( auto face : faces )
{
Point center( face.x + face.width*0.5, face.y + face.height*0.5 );
ellipse( frame, center, Size( face.width*0.5, face.height*0.5), 0, 0, 360, Scalar( 0, 0, 255 ), 4, 8, 0 );
Mat faceROI = frame_gray( face );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade_.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t j = 0; j < eyes.size(); j++ )
{
Point center( face.x + eyes[j].x + eyes[j].width*0.5, face.y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
}
示例2: detect
bool detect(workshop_examples::DetectFaces::Request &req,
workshop_examples::DetectFaces::Response &res
)
{
ROS_INFO_STREAM("Request: detect_all = " << (int)req.detect_all);
if( !image_received )
{
return false;
}
cv::Mat frame_gray;
cv::cvtColor( frame_rgb, frame_gray, CV_BGR2GRAY );
cv::equalizeHist( frame_gray, frame_gray );
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale( frame_gray, faces, 1.5, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
// Fill in service response
res.detections.type = workshop_msgs::Detections::FACE;
for( int i = 0; i < (int)faces.size(); i++ )
{
sensor_msgs::RegionOfInterest det;
det.x_offset = faces[i].x;
det.y_offset = faces[i].y;
det.width = faces[i].width;
det.height = faces[i].height;
res.detections.rects.push_back(det);
}
ROS_INFO_STREAM("...sending back response.");
return true;
}
示例3: detectLargestObject
bool AnchorPointSelector::detectLargestObject(cv::CascadeClassifier cascade, cv::Mat image, cv::Rect &largestObject, double scaleFactor, int minNeighbors, int flags, cv::Size minSize) {
std::vector<cv::Rect> results;
largestObject.x = 0;
largestObject.y = 0;
largestObject.width = 0;
largestObject.height = 0;
cascade.detectMultiScale(image, results, scaleFactor, minNeighbors, flags, minSize);
// Save the largest object
if (results.size() > 0) {
for(int i=0; i<results.size(); i++) {
cv::Rect temp = results[0];
if((temp.width * temp.height) > (largestObject.width*largestObject.height)) {
largestObject = temp;
}
}
return true;
}
return false;
}
示例4: detect
/*
Detects faces on an image and displays it to the screen.
*/
void detect( const cv::Mat frame, cv::CascadeClassifier face_cascade ){
std::vector< cv::Rect > faces;
cv::Mat frame_gray;
// Create gray scale duplicate image
cv::cvtColor( frame, frame_gray, CV_BGR2GRAY );
// Run detection
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size( 24, 24 ) );
// Iterate through all detected faces
for( int i = 0; i < faces.size(); i++ ){
// Draw rectangle over each face
cv::rectangle(
frame,
cv::Point( faces[i].x, faces[i].y ),
cv::Point( faces[i].x + faces[i].width,
faces[i].y + faces[i].height ),
cv::Scalar( 0, 0, 255 ), // red
+1,
4
);
}
cv::imwrite( "output.pgm", frame ); // Save image
}
示例5: grayCameraImage
void TellThatToMyCamera_v1_0App::updateExpressions (Surface cameraImage){
cv::Mat grayCameraImage( toOcv( cameraImage, CV_8UC1 ) ); // create a grayscale copy of the input image
cv::equalizeHist( grayCameraImage, grayCameraImage ); // equalize the histogram for (just a little) more accuracy
mExpressions.clear(); // clear out the previously deteced expressions
mPredictions.clear();
vector<cv::Rect> expressions;
// Next is to detect the faces and iterate them, appending them to mExpressions
mExpressionsCascade.detectMultiScale(grayCameraImage, expressions);
// At this point the position of the faces has been calculated!
// Now it's time to get the faces, make a prediction and save it for the video.
cv::Mat graySq(100,100,CV_8UC1); // gray square for assigning the proper size of the resized detected faces
for(vector<cv::Rect>::const_iterator expressionIter = expressions.begin(); expressionIter != expressions.end(); ++expressionIter){
// Get the process face by face (in case there's more than one face in the video frame image)
Rectf expressionRect(fromOcv(*expressionIter));
mExpressions.push_back(expressionRect);
cv::Rect face_i (*expressionIter); // Rect with data (size and position) of the detected face
cv::Mat face = grayCameraImage(face_i); // Image containing the detected face
cv::Mat face_resized; // Image for the resized version of the detected face
cv::resize(face, face_resized, graySq.size(), 1, 1, cv::INTER_CUBIC); // resizes the image
// cv::resize(face, face_resized, graySq.size(), 0, 0, cv::INTER_LINEAR);
// Now, perform the EXPRESSION PREDICTION!!!
int predicted = mFisherFaceRec->predict(face_resized);
mPredictions.push_back(predicted); // put the corresponding label to the corresponding face
}
}
示例6: faceDetect
void faceDetect(){
//Detect faces
cv::cvtColor( frame, frame_gray, CV_BGR2GRAY );
cv::equalizeHist( frame_gray, frame_gray );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
//for each face draws an ellipse arround and look for the red color at a distance from
for( i = 0; i < faces.size(); i++ )
{
cv::Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
cv::ellipse( frame, center, cv::Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, cv::Scalar( 0, 255, 0 ), 2, 8, 0 );
faceX = (float) faces[i].x;
faceY = (float) faces[i].y;
if( ((faceX + faceColorThresh) > (posX ) | (faceX - faceColorThresh) < (posX )) ) {
face = true;
//publishing camera image
out_msg.image = frame; //frame
out_msg.encoding = sensor_msgs::image_encodings::BGR8;
msg = out_msg.toImageMsg();
pub.publish(msg);
ROS_FATAL("PERSON DETECTED");
break;
}
}
}
示例7: fd_timeslice
void fd_timeslice( cv::Mat& frame_gray, cv::Mat& original, bool mFindEyes )
{
frame_gray.copyTo(debugImage);
//equalizeHist( frame_gray, frame_gray );
//cv::pow(frame_gray, CV_64F, frame_gray);
uint64_t start = GetTimeStamp2();
face_cascade.detectMultiScale( frame_gray, faces, 1.015, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150) );
// findSkin(debugImage);
uint64_t end = GetTimeStamp2();
num_faces_present = faces.size();
// extract Face :
if (num_faces_present)
{
printf("FaceDetect() Duration = %6.2f; faces_present=%d\n", (float)(end-start)/1000000., num_faces_present );
//cv::flip(frame_gray, frame_gray, 1);
start = GetTimeStamp2();
printf("FaceRect: <%6.2f, %d>\n", faces[0].height, faces[0].width );
faceROIc = original(faces[0]);
if (mFindEyes)
findEyes( frame_gray, faces[0] );
end = GetTimeStamp2();
printf("FindEyes() Duration = %6.2f\n", (float)(end-start)/1000000. );
imshow(face_window_name, faceROIc);
}
annotate_faces( original );
}
示例8: detectAndDisplay
/**
* @function detectAndDisplay
*/
void detectAndDisplay( cv::Mat frame ) {
clock_t start,end;
double time_taken;
start = clock();
std::vector<cv::Rect> faces;
//cv::Mat frame_gray;
std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels);
cv::Mat frame_gray = rgbChannels[2];
//cvtColor( frame, frame_gray, CV_BGR2GRAY );
//equalizeHist( frame_gray, frame_gray );
//cv::pow(frame_gray, CV_64F, frame_gray);
//-- Detect faces
//Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(80, 80) );
// findSkin(debugImage);
for( int i = 0; i < faces.size(); i++ )
{
rectangle(debugImage, faces[i], 1234);
}
//-- Show what you got
if (faces.size() > 0) {
findEyes(frame_gray, faces[0]);
}
end = clock();
time_taken = ((double) (end - start)) / CLOCKS_PER_SEC;
//std::cout<<"Time taken by the detect and display function : "<<time_taken<<std::endl;
}
示例9: detectAndDisplay
void MainWindow::detectAndDisplay(const QImage &image){
#if 0
std::vector<Rect> faces;
Mat face_gray;
cv::Mat face = QImage2cvMat(image);
cvtColor( face, face_gray, CV_BGR2GRAY ); //rgb类型转换为灰度类型
equalizeHist( face_gray, face_gray ); //直方图均衡化
face_cascade.detectMultiScale( face_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(1, 1) );
for( int i = 0; i < faces.size(); i++ ){
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( face, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 0), 2,7, 0 );
}
QImage image1 = cvMat2QImage(face);
QPixmap pix = QPixmap::fromImage(image1.scaled(image1.width(),image1.height()));
ui->Lab_VideoOut->setPixmap(pix);
#else
QPixmap pix = QPixmap::fromImage(image.scaled(image.width(),image.height()));
ui->Lab_VideoOut->setPixmap(pix);
m_timerPlay->start();
#endif
}
示例10: detectEye
/**
* Function to detect human face and the eyes from an image.
*
* @param im The source image
* @param tpl Will be filled with the eye template, if detection success.
* @param rect Will be filled with the bounding box of the eye
* @return zero=failed, nonzero=success
*/
auto detectEye(cv::Mat const& im, cv::Mat & tpl, cv::Rect & rect)
{
std::vector<cv::Rect> faces, eyes;
face_cascade.detectMultiScale(im, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
for (int i = 0; i < faces.size(); i++)
{
cv::Mat face = im(faces[i]);
eye_cascade.detectMultiScale(face, eyes, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(20,20));
if (eyes.size())
{
rect = eyes[0] + cv::Point(faces[i].x, faces[i].y);
tpl = im(rect);
}
}
return eyes.size();
}
示例11: detect_hand
// Analiza una imagen tratando de identificar un vehículo.
bool detect_hand(cv::Mat& frame){
// Variables necesarias.
float puntos_extraidos[2*stasm_NLANDMARKS];
std::vector<cv::Rect> detecciones;
cv::Mat gray_frame;
int salida = false;
// Preparo la imagen.
cv::cvtColor(frame, gray_frame, cv::COLOR_BGR2GRAY);
// Uso el detector sobre la imagen. Me aseguro de detectar rectángulos mayores de la mitad de la
// imagen.
hand_detector.detectMultiScale(
gray_frame,
detecciones,
1.5,
3,
0|cv::CASCADE_SCALE_IMAGE,
frame.size()/2,
frame.size());
// Si se han detectado objetos.
if(detecciones.size() > 0){
int mano_encontrada;
std::cout << "Mano encontrada en "<< detecciones[0] << std::endl;
cv::rectangle(frame, detecciones[0], cv::Scalar(0,0,255), 4);
if(!stasm_search_single(
&mano_encontrada,
puntos_extraidos,
(char*)gray_frame.data,
gray_frame.size().width,
gray_frame.size().height,
"path_prueba",
"../data")){
std::cout << "Puntos no encontrados" << std::endl;
}
else{
salida = true;
cv::Point2f p1, p2;
p1 = cv::Point2f(puntos_extraidos[0],puntos_extraidos[1]);
cv::circle(frame, p1, 1, cv::Scalar(0,255,0), 3);
for(int i=2; i<stasm_NLANDMARKS*2; i+=2){
p2 = cv::Point2f(puntos_extraidos[i],puntos_extraidos[i+1]);
cv::circle(frame, p2, 1, cv::Scalar(0,255,0), 3);
cv::line(frame, p1, p2, cv::Scalar(0,255,0));
p1 = p2;
}// Fin for.
}// Fin else.
}// Fin if.
return salida;
}//Fin 'detect_hand'.
示例12: calculateReferenceMaxDistanceAndPosition
double MainWindow::calculateReferenceMaxDistanceAndPosition(cv::VideoCapture cap,cv::CascadeClassifier frontal_face_cascade, cv::Point2d &referencePosition)
{
// find a real face (consistent detections)
std::vector<double> faceDistancesBuffer;
std::vector<cv::Point2d> facePositionsBuffer;
cv::Point2d previousPosition(0,0);
double sumDistances = 0;
int facesFound = 0;
while (facesFound < 10)
{
cv::Mat frame;
cap >> frame;
cv::Mat frame_gray;
cv::cvtColor( frame, frame_gray, cv::COLOR_BGR2GRAY );
cv::equalizeHist( frame_gray, frame_gray );
std::vector<cv::Rect> frontal_faces;
frontal_face_cascade.detectMultiScale(frame_gray, frontal_faces);
if (!frontal_faces.empty())
{
facesFound++;
cv::Point2d currentPosition(frontal_faces[0].x,frontal_faces[0].y);
facePositionsBuffer.push_back(currentPosition);
double distance = cv::norm(cv::Mat(previousPosition),cv::Mat(currentPosition));
sumDistances += distance;
faceDistancesBuffer.push_back(distance);
previousPosition = currentPosition;
rectangle(frame,frontal_faces[0],cv::Scalar(0,255,0));
}
QImage image = QImage((const uchar *)frame.data, frame.cols, frame.rows, QImage::Format_RGB888).rgbSwapped();
ui->imageDisplay->setPixmap(QPixmap::fromImage(image));
qApp->processEvents();
}
double averageDistance = sumDistances/faceDistancesBuffer.size();
int countDistancesNoOutliers = 0;
double sumDistancesNoOutliers = 0;
for (unsigned int i = 0; i < faceDistancesBuffer.size(); i++)
{
if (faceDistancesBuffer[i] < 2*averageDistance)
{
countDistancesNoOutliers++;
sumDistancesNoOutliers += faceDistancesBuffer[i];
if (i<facePositionsBuffer.size())
{
referencePosition = facePositionsBuffer[i];
}
}
}
double goundTruthMaxDistance = 4*(sumDistancesNoOutliers/countDistancesNoOutliers);
return goundTruthMaxDistance;
}
示例13: detectAndDisplay
/**
* @function detectAndDisplay
*/
int detectAndDisplay( cv::Mat frame ) {
std::vector<cv::Rect> faces;
//cv::Mat frame_gray;
std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels);
cv::Mat frame_gray = rgbChannels[2];
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150) );
// findSkin(debugImage);
for( int i = 0; i < faces.size(); i++ )
{
rectangle(debugImage, faces[i], 1234);
}
//-- Show what you got
if (faces.size() > 0) {
findEyes(frame_gray, faces[0]);
if (frame_gray.data) {
frame_gray.convertTo(frame_gray, CV_32FC1);
cv::Mat target = CropFace(frame_gray,leftPupil, rightPupil, offset_pct, dest_sz);
if (errorflag) {
return -1;
}
if (target.data) {
target.convertTo(target, CV_8UC1);
equalizeHist(target,target);
target.convertTo(target, CV_32FC1);
int index = query(target,ef);
index+= startnum;
char temp[3];
sprintf(temp, "%d", index);
std::string s(temp);
std::cout << "face recognized, index : " << index << std::endl;
// imshow("target", ef.norm_0_255(target).reshape(1, dest_sz.x));
imshow("result"+s, ef.getdb()[index-startnum]);
waitKey(0);
destroyWindow("result"+s);
return index;
}
}
}
return -1;
}
示例14: updateFaces
void ocvFaceDetectApp::updateFaces( Surface cameraImage )
{
const int calcScale = 2; // calculate the image at half scale
// create a grayscale copy of the input image
cv::Mat grayCameraImage( toOcv( cameraImage, CV_8UC1 ) );
// scale it to half size, as dictated by the calcScale constant
int scaledWidth = cameraImage.getWidth() / calcScale;
int scaledHeight = cameraImage.getHeight() / calcScale;
cv::Mat smallImg( scaledHeight, scaledWidth, CV_8UC1 );
cv::resize( grayCameraImage, smallImg, smallImg.size(), 0, 0, cv::INTER_LINEAR );
// equalize the histogram
cv::equalizeHist( smallImg, smallImg );
// clear out the previously deteced faces & eyes
mFaces.clear();
mEyes.clear();
// detect the faces and iterate them, appending them to mFaces
vector<cv::Rect> faces;
mFaceCascade.detectMultiScale( smallImg, faces );
for( vector<cv::Rect>::const_iterator faceIter = faces.begin(); faceIter != faces.end(); ++faceIter ) {
Rectf faceRect( fromOcv( *faceIter ) );
faceRect *= calcScale;
mFaces.push_back( faceRect );
// detect eyes within this face and iterate them, appending them to mEyes
vector<cv::Rect> eyes;
mEyeCascade.detectMultiScale( smallImg( *faceIter ), eyes );
for( vector<cv::Rect>::const_iterator eyeIter = eyes.begin(); eyeIter != eyes.end(); ++eyeIter ) {
Rectf eyeRect( fromOcv( *eyeIter ) );
eyeRect = eyeRect * calcScale + faceRect.getUpperLeft();
mEyes.push_back( eyeRect );
}
}
}
示例15: detectObjects
void detectObjects(const cv::Mat &img, cv::CascadeClassifier &cascade, std::vector<cv::Rect> &objects, int scaledWidth, int flags, Size minFeatureSize, float searchScaleFactor, int minNeighbors)
{
cv::Mat gray;
if (img.channels() == 3) {
cvtColor(img, gray, CV_BGR2GRAY);
}
else if (img.channels() == 4) {
cvtColor(img, gray, CV_BGRA2GRAY);
}
else {
gray = img;
}
cv::Mat inputImg;
float scale = img.cols / (float)scaledWidth;
if (img.cols > scaledWidth) {
int scaledHeight = cvRound(img.rows / scale);
cv::resize(gray, inputImg, cv::Size(scaledWidth, scaledHeight));
inputImg = gray;
}
else {
inputImg = gray;
}
cv::Mat equalizedImg;
cv::equalizeHist(inputImg, equalizedImg);
cascade.detectMultiScale(equalizedImg, objects, searchScaleFactor, minNeighbors, flags, minFeatureSize);
if (img.cols > scaledWidth) {
for (int i = 0; i < (int)objects.size(); i++ ) {
objects[i].x = cvRound(objects[i].x * scale);
objects[i].y = cvRound(objects[i].y * scale);
objects[i].width = cvRound(objects[i].width * scale);
objects[i].height = cvRound(objects[i].height * scale);
}
}
for (int i = 0; i < (int)objects.size(); i++ ) {
if (objects[i].x < 0)
objects[i].x = 0;
if (objects[i].y < 0)
objects[i].y = 0;
if (objects[i].x + objects[i].width > img.cols)
objects[i].x = img.cols - objects[i].width;
if (objects[i].y + objects[i].height > img.rows)
objects[i].y = img.rows - objects[i].height;
}
}