2013年8月26日 星期一

【OpenCV】HOGDescriptor使用方法

#include "stdafx.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/ml/ml.hpp"
#include <opencv2/gpu/gpu.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main( )
{    
    
    CvCapture* capture = 0;
    IplImage* srcImg;

    capture = cvCaptureFromCAM(0);    
    cvNamedWindow( "result", 1 );
    
    if( capture )
    {
        for(;;)
        {                    
            srcImg = cvQueryFrame( capture );                
            cv::Mat frame = srcImg;
            cv::Mat src_gray;
            cv::cvtColor( frame, src_gray, CV_RGB2GRAY );        
            cv::Mat src_resize;
            cv::resize(src_gray, src_resize, cvSize(60,60));

            /***************HOG Descriptor*****************/
            CvSize winSize = cvSize(60,60); 
            CvSize blockSize = cvSize(20,20);
            CvSize strideSize = cvSize(10,10); 
            CvSize cellSize = cvSize(5,5);
            int bins = 9;
            //一個Cell有9個方向,共9維
            //一個Block有16個Cells((20/5)*(20/5)),共144維
            //一個Window有25個Blocks(((60-20)/10+1)*((60-20)/10+1)),共3600維
            HOGDescriptor *hog= new  HOGDescriptor(winSize, blockSize, strideSize, cellSize, bins);
            vector< float >descriptors;//HOG描述向量
            CvSize winShiftSize = cvSize(10,10);//搜尋框移動位置
            CvSize paddingSize = cvSize(0,0);//補滿邊界位置
            hog->compute(src_resize, descriptors, winShiftSize, paddingSize);//調用計算函數  
            printf( "%d\n" ,descriptors.size()); 
                    
            cv::imshow( "result", src_resize );
        
            if( waitKey( 10 ) >= 0 )
                goto _cleanup_;
        }
        waitKey(10);

    _cleanup_:
        cvReleaseCapture( &capture );
    }
    
    cvDestroyWindow("result");
    return 0;
}



相關文章:OpenCV學習日記

0 意見:

張貼留言