草庐IT

c++ - 仅 block 的 HoG 特征

coder 2024-02-06 原文

我正在尝试仅为 block 计算 HOG 特征。我探索了 opencv/module/gpu/src/ 下列出的 hog.cpp。下面是我更改为仅计算 block 特征的代码。

void cv::gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format)
{
    CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);

    computeBlockHistograms(img);
    // give block back

/*
    const size_t block_hist_size = getBlockHistogramSize();
    Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
    Size wins_per_img   = numPartsWithin(img.size(), win_size, win_stride);

    descriptors.create(wins_per_img.area(), static_cast<int>(blocks_per_win.area() * block_hist_size), CV_32F); */

    switch (descr_format)
    {
    case DESCR_FORMAT_ROW_BY_ROW:
        hog::extract_descrs_by_rows(win_size.height, win_size.width, block_stride.height, block_stride.width,
                                    win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
        break;
    case DESCR_FORMAT_COL_BY_COL:
        hog::extract_descrs_by_cols(win_size.height, win_size.width, block_stride.height, block_stride.width,
                                    win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
        break;
    default:
        CV_Error(CV_StsBadArg, "Unknown descriptor format");
    }
}

这也是 computeBlockHistograms 代码。

void cv::gpu::HOGDescriptor::computeBlockHistograms(const GpuMat& img)
{
    computeGradient(img, grad, qangle);

    size_t block_hist_size = getBlockHistogramSize();
    Size blocks_per_img = numPartsWithin(img.size(), block_size, block_stride);

    //   block_hists.create(1, block_hist_size * blocks_per_img.area(), CV_32F);
    block_hists = getBuffer(1, static_cast<int>(block_hist_size * blocks_per_img.area()), CV_32F, block_hists_buf);

    hog::compute_hists(nbins, block_stride.width, block_stride.height, img.rows, img.cols,
                        grad, qangle, (float)getWinSigma(), block_hists.ptr<float>());

    hog::normalize_hists(nbins, block_stride.width, block_stride.height, img.rows, img.cols,
                         block_hists.ptr<float>(), (float)threshold_L2hys);
}

编辑:我还包括 hog.cpp

中的 getDescriptor 函数
void cv::gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format)
{
    CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);

    computeBlockHistograms(img);

    const size_t block_hist_size = getBlockHistogramSize();
    Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
    Size wins_per_img   = numPartsWithin(img.size(), win_size, win_stride);

    descriptors.create(wins_per_img.area(), static_cast<int>(blocks_per_win.area() * block_hist_size), CV_32F);

    switch (descr_format)
    {
    case DESCR_FORMAT_ROW_BY_ROW:
        hog::extract_descrs_by_rows(win_size.height, win_size.width, block_stride.height, block_stride.width,
                                    win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
        break;
    case DESCR_FORMAT_COL_BY_COL:
        hog::extract_descrs_by_cols(win_size.height, win_size.width, block_stride.height, block_stride.width,
                                    win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
        break;
    default:
        CV_Error(CV_StsBadArg, "Unknown descriptor format");
    }
}

谁能帮我获得仅 block 的 HOG 功能。 已编辑:我只对计算不同窗口大小的 HOG 特征感兴趣,同时保持单元格和 block 的特征相同。

最佳答案

我修改了以下函数以仅计算 block 的 HOG 描述符。

void cv::gpu::HOGDescriptor::getDescriptorsBlock(const GpuMat& img, Size win_stride, GpuMat& descriptors, FileStorage fs3, string fileName, double scale, int width, int height, size_t lev)
{
    CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);

    size_t block_hist_size = getBlockHistogramSize();
    computeBlockHistograms(img);
    Size blocks_per_img = numPartsWithin(img.size(), block_size, block_stride);

    // Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
    // Size wins_per_img   = numPartsWithin(img.size(), win_size, win_stride);

    // copy block_hists from GPU to CPU/

    float dest_ptr[block_hist_size * blocks_per_img.area()];

    cudaMemcpy( &dest_ptr[0], block_hists.ptr<float>(), block_hist_size *blocks_per_img.area()*sizeof(CV_32F),        cudaMemcpyDeviceToHost); 

    std::cout<<"( "<<width<< " ," << height<< ")"<< std::endl;
    std::cout <<lev<< std::endl;

    // write to yml file

    int level = lev;

    fs3<<"Scale"<<scale;
    fs3 <<"Level"<<level;
    fs3<<"Width"<<width<<"Height"<<height;
    fs3 << "features" << "[";
    for (unsigned int i = 0; i < (block_hist_size * blocks_per_img.area()) ; i++ )
    {
     fs3  << dest_ptr[i];
    }
    fs3 << "]";
}

下面是计算多尺度图像的 HOG 描述符。

void cv::gpu::HOGDescriptor::getDescriptorsMultiScale(const GpuMat& img,
                                              Size win_stride, double scale0, unsigned int count)
{

    CV_Assert(img.type() == CV_8UC1 || img.type() == CV_8UC4);

    vector<double> level_scale;
    double scale = 1.;
    int levels = 0;

    for (levels = 0; levels < nlevels; levels++)
    {
        level_scale.push_back(scale);
        if (cvRound(img.cols/scale) < win_size.width ||
            cvRound(img.rows/scale) < win_size.height || scale0 <= 1)
            break;
        scale *= scale0;
    }
    levels = std::max(levels, 1);
    level_scale.resize(levels);
    image_scales.resize(levels);

    // open yml file with image ID

    FileStorage fs3;
    char fileName[20];
    GpuMat descriptors;
    sprintf (fileName, "%04d", count);
    fs3.open(fileName, FileStorage::WRITE);

    for (size_t i = 0; i < level_scale.size(); i++)
    {
        scale = level_scale[i];
        Size sz(cvRound(img.cols / scale), cvRound(img.rows / scale));
        GpuMat smaller_img;

        if (sz == img.size())
            smaller_img = img;
        else
        {
            image_scales[i].create(sz, img.type());
            switch (img.type())
            {
                case CV_8UC1: hog::resize_8UC1(img, image_scales[i]); break;
                case CV_8UC4: hog::resize_8UC4(img, image_scales[i]); break;
            }
            smaller_img = image_scales[i];
        }
        std::cout<<"scale "<<level_scale[i]<<std::endl;

        // calculate descriptors for blocks 
        getDescriptorsBlock( smaller_img, win_stride, descriptors, fs3, fileName, scale, smaller_img.cols, smaller_img.rows, i);

        // detect(smaller_img, locations, hit_threshold, win_stride, padding);
    }
    // close yml file
    fs3.release();

} 

不要忘记在中添加这两个函数的定义 opencv/modules/gpu/include/opencv2/gpu/gpu.hpp

关于c++ - 仅 block 的 HoG 特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158790/

有关c++ - 仅 block 的 HoG 特征的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  3. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  4. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  5. ruby - 在匿名 block 中产生 - 2

    我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi

  6. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  7. ruby - Ruby 中的单 block AES 解密 - 2

    我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec

  8. ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出 - 2

    我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e

  9. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  10. ruby - 具有两个参数的 block - 2

    我从用户Hirolau那里找到了这段代码:defsum_to_n?(a,n)a.combination(2).find{|x,y|x+y==n}enda=[1,2,3,4,5]sum_to_n?(a,9)#=>[4,5]sum_to_n?(a,11)#=>nil我如何知道何时可以将两个参数发送到预定义方法(如find)?我不清楚,因为有时它不起作用。这是重新定义的东西吗? 最佳答案 如果您查看Enumerable#find的文档,您会发现它只接受一个block参数。您可以将它发送两次的原因是因为Ruby可以方便地让您根据它的“并行赋

随机推荐