Detect upper body portion using OpenCV

一世执手 提交于 2019-11-30 18:57:08

问题


I am working on an iOS project that is integrated with OpenCV.
My desired output is something like this:

How can I detect Upper Body Portion (i.e: below neck to legs)?
I have done so far to achieve body detection is something like this..
If anyone has made this before.
Please help me..

-(void)processImage:(Mat&)image
{
    std::vector<cv::Rect> bodies;
    Mat grayscaleFrame;

    cvtColor(image, grayscaleFrame, CV_BGR2GRAY);
    equalizeHist(grayscaleFrame, grayscaleFrame);

    upperBodyCascade.detectMultiScale(grayscaleFrame, image, bodies, HaarOptions,cv::Size(30,30));
    for (size_t i = 0; i < bodies.size(); i++)
    {
        rectangle(image, bodies[i], Scalar(255, 0, 255));
    }

}

回答1:


You can use an Haar Cascade Classifier loading the haarcascade_upperbody.xml

You can find an example here. You just need to change the loaded classifier.

The code as below:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String upper_body_cascade_name = "path\\to\\haarcascade_upperbody.xml";
CascadeClassifier upper_body_cascade;
string window_name = "Capture - Upper Body detection";
RNG rng(12345);

/** @function main */
int main(int argc, const char** argv)
{
    VideoCapture capture(0);
    Mat frame;

    //-- 1. Load the cascades
    if (!upper_body_cascade.load(upper_body_cascade_name)){ printf("--(!)Error loading\n"); return -1; };

    //-- 2. Read the video stream
    if (capture.isOpened())
    {
        while (true)
        {
            capture >> frame;

            //-- 3. Apply the classifier to the frame
            if (!frame.empty())
            {
                detectAndDisplay(frame);
            }
            else
            {
                printf(" --(!) No captured frame -- Break!"); break;
            }

            int c = waitKey(10);
            if ((char)c == 'c') { break; }
        }
    }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> bodies;
    Mat frame_gray;

    cvtColor(frame, frame_gray, CV_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    upper_body_cascade.detectMultiScale(frame_gray, bodies, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));

    for (size_t i = 0; i < bodies.size(); i++)
    {
        rectangle(frame, bodies[i], Scalar(255, 0, 255));
    }
    //-- Show what you got
    imshow(window_name, frame);
}


来源:https://stackoverflow.com/questions/31834415/detect-upper-body-portion-using-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!