HOG功能用于在OpenCV中使用GPU进行对象检测

时间:2015-06-29 09:18:29

标签: c++ opencv computer-vision gpu pattern-recognition

在我的项目中,我正在计算同一图像中不同级别的GPU上的HOG功能。我的目标是检测以下物体 1.卡车
汽车
3.人 大多数重要问题是在多类物体检测器的情况下选择窗口大小。 This帖子提供了一个很好的基础,但在多类功能的情况下,它不能为选择窗口大小提供答案。
为了解决这个问题,我计算了不同级别/分辨率的每个正像的HOG特征,保持窗口大小(48 * 96)相同,但每个图像的文件大约600 MB,这太大了。 在多类对象检测的情况下,请让我知道如何选择窗口大小,块大小和单元格大小。这是我用来计算HOG功能的代码。

void App::run()
{
    unsigned int count = 1;
    FileStorage fs;
    running = true;

    //int width;
    //int height;

    Size win_size(args.win_width, args.win_width * 2); 
    Size win_stride(args.win_stride_width, args.win_stride_height);

    cv::gpu::HOGDescriptor gpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9,
                                   cv::gpu::HOGDescriptor::DEFAULT_WIN_SIGMA, 0.2, gamma_corr,
                                   cv::gpu::HOGDescriptor::DEFAULT_NLEVELS);

    VideoCapture vc("/home/ubuntu/Desktop/getdescriptor/images/image%d.jpg");
    Mat frame;
    Mat Left;
    Mat img_aux, img, img_to_show, img_new;
    cv::Mat temp;
    gpu::GpuMat gpu_img, descriptors, new_img;

    char cbuff[20];



    while (running)
    {

        vc.read(frame);


        if (!frame.empty())
        {
            workBegin();

            width  = frame.rows;
            height = frame.cols;

            sprintf (cbuff, "%04d", count);

            // Change format of the image
            if (make_gray) cvtColor(frame, img_aux, CV_BGR2GRAY);
            else if (use_gpu) cvtColor(frame, img_aux, CV_BGR2BGRA);
            else Left.copyTo(img_aux);

            // Resize image
            if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));
            else img = img_aux;
            img_to_show = img;

            gpu_hog.nlevels = nlevels;

            hogWorkBegin();
            if (use_gpu)
            {
                gpu_img.upload(img);
                new_img.upload(img_new);

                fs.open(cbuff, FileStorage::WRITE);


                for(int levels = 0; levels < nlevels; levels++)
                {
                gpu_hog.getDescriptors(gpu_img, win_stride, descriptors, cv::gpu::HOGDescriptor::DESCR_FORMAT_ROW_BY_ROW);
                descriptors.download(temp);

                //printf("size %d %d\n", temp.rows, temp.cols);

                fs <<"level" << levels;                
                fs << "features" << temp;

                cout<<"("<<width<<","<<height<<")"<<endl;

                width =  round(width/scale);
                height = round(height/scale);

                if( width < win_size.width || height < win_size.height )
                break;

                cout<<"Levels "<<levels<<endl;

                resize(img,img_new,Size(width,height));
                scale *= scale;
                }

                cout<<count<< " Image feature calculated !"<<endl;
                count++;
                //width = 640; height = 480;
                scale = 1.05;

            }

            hogWorkEnd();
            fs.release();
          }
           else  running = false;
       }
} 

1 个答案:

答案 0 :(得分:2)

应选择窗口大小,s.t。您要检测的对象适合窗口。如果您想为不同类型设置不同的窗口大小,这可能会变得棘手。

通常你做的是以下

  1. 获取每种类型对象的训练数据,并使用在对象的已知位置提取的特征训练[对象类型数]许多模型。
  2. 然后,您拍摄每个测试图像并使用滑动窗口方法来提取每个位置的要素。然后将这些特征与每个模型进行比较。如果其中一个模型导致分数高于某个阈值,则表示您已找到此对象。如果多个模型得分高于阈值,则只需将得分最高。
  3. 如果您想使用不同大小的检测窗口,您将获得不同大小的特征向量(根据HoG特征的性质)。棘手的是,在测试阶段,您必须使用与您使用的对象类型一样多的滑动窗口。这肯定有效,但你必须多次处理每个测试图像,导致更长的处理时间)

    回答你的尺码问题:我没有给你的价值,它总是取决于你的图像。如上所述使用图像金字塔是处理不同缩放对象的好方法。

    • 窗口大小:整个对象应该适合;必须可以按块大小整除
    • 块大小必须可以按单元格大小整除

    可以找到用于可视化HoG功能的示例代码here。这也有助于理解特征向量的外观。

    编辑:发现困难的方法,只允许 <div id="accountname1" style="position:relative;float:right"> <div id="divLogin" style="position:relative; margin-right:1px; margin-top:1%; float:right"> <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false"> <anonymoustemplate> <img src="/images/btn_login.png" alt="Login" /> <a href="~/Account/Login.aspx" ID="A3" runat="server" title="Sign in with your account details">Login</a> | <a id="A4" href="~/Account/Register.aspx" runat="server" title="Sign up for your 30 days free trial">Free Trial</a> </anonymoustemplate> <loggedintemplate> Welcome <span class="bold"><asp:Label id="UserLogonID" runat="server"></asp:Label></span> <br /> [<asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Account/login.aspx" />] <br /> </loggedintemplate> </asp:LoginView> <p style="text-align:right"> <asp:Label ID="lblFreeTrialDays" runat="server" Text=""></asp:Label> </p> </div> <div id="mobile-btn"> <img src="/images/menu_btn.png" alt="Menu Button" /> </div> </div> </div> 的单元格大小。请参阅documentation