直接上代码:

#include <opencv2/opencv.hpp>
using namespace cv;

#include <cmath>
using namespace std;

template<typename T>
int _get_hog(vector<float> &hist, const Mat &img, const int nbins = 8, const bool need_normalize=false){

  int rows_minus_1 = img.rows - 1;
  int cols_minus_1 = img.cols - 1;

  float dx, dy;
  float angle;

  const float angle_base = atan2f(0, -1);
  const float angle_piece = 2.f*angle_base / (float)nbins;

  int bin_id;

  for (int y = 0; y < rows_minus_1; y++){
    for (int x = 0; x < cols_minus_1; x++){
      dx = (float)(img.at<T>(y, x) - img.at<T>(y, x + 1));
      dy = (float)(img.at<T>(y, x) - img.at<T>(y + 1, x));

      angle = atan2f(dy, dx) + angle_base;

      bin_id = (int)floorf(angle / angle_piece);

      hist[bin_id] += 1.f;
    }
  }

  hist[nbins - 1] += hist[nbins];
  hist.resize(nbins);

  if (!need_normalize){
    return 0;
  }

  float num_pixels = (float)(rows_minus_1*cols_minus_1);

  for (int i = 0; i < nbins; i++){
    hist[i] = hist[i] / num_pixels;
  }

  return 0;
}

// support
// CV_8UC1
// CV_32FC1
// CV_64FC1
int get_hog(vector<float> &hist, const Mat &img, const int nbins = 8, const bool need_normalize = false){
  if (img.type() != CV_8UC1 && img.type() != CV_32FC1 && img.type() != CV_64FC1){
    cerr << __FUNCDNAME__ << " invalid image type!" << endl;
    return 1;
  }

  hist.resize(nbins+1);
  hist.assign(nbins+1, 0);

  if (img.type()      == CV_8UC1){
    return _get_hog<uchar> (hist, img, nbins, need_normalize);
  }
  else if (img.type() == CV_32FC1) {
    return _get_hog<float> (hist, img, nbins, need_normalize);
  }
  else if (img.type() == CV_64FC1) {
    return _get_hog<double>(hist, img, nbins, need_normalize);
  }

  return 1;
}

HOG OpenCV 代码片段的更多相关文章

  1. opencv代码片段合集

    个人笔记 长期更新 #### 创建一个图片 import cv2 # Not actually necessary if you just want to create an image. impor ...

  2. CMake相关代码片段

    目录 用于执行CMake的.bat脚本 CMakeLists.txt和.cmake中的代码片段 判断平台:32位还是64位? 判断Visual Studio版本 判断操作系统 判断是Debug还是Re ...

  3. sublimetext3中保存代码片段

    在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...

  4. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

  5. 10个 jQuery 代码片段,可以帮你快速开发。

    转载自:http://mp.weixin.qq.com/s/mMstI10vqwu8PvUwlLborw 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而 ...

  6. 代码片段添加智能提示,打造一款人见人爱的ORM框架

    SqlSugar ORM优点: 1.高性能,达到原生最高水准,比SqlHelper性能要高,比Dapper快30% 比EF快50% 2.支持多种数据库 ,sql版本更新最快,其它会定期更新,可以在多种 ...

  7. js/jquery/html前端开发常用到代码片段

    1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...

  8. Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!

      使用Code Snippet简化Coding 在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符 ...

  9. Visual Studio 的代码片段工具

    当安装完Visual Studio之后,会有附带一些原生的代码片段文件(*.snippet),对于vs2013参考目录如下: X:\Program Files (x86)\Microsoft Visu ...

随机推荐

  1. SSH端口转发(本地转发、远程转发、动态转发)

    SSH端口转发   一:什么是端口转发?     SSH 会自动加密和解密所有SSH 客户端与服务端之间的网络数据.但是,SSH 还能够将其他TCP 端口的网络数据通过SSH 链接来转发,并且自动提供 ...

  2. [CVPR2018] Context-aware Deep Feature Compression for High-speed Visual Tracking

    基于内容感知深度特征压缩的高速视觉跟踪 论文下载:http://cn.arxiv.org/abs/1803.10537对于视频这种高维度数据,作者训练了多个自编码器AE来进行数据压缩,至于怎么选择具体 ...

  3. c#代码输入图片

    Image bgimage = Image.FromFile(flieUrl + bgImg); if (bgimage != null) { Bitmap bmp1 = new Bitmap(bgi ...

  4. drupal 8 之 calendar模块

    一.安装模块 calendar.view.date三个模块同时安装 二.创建一个事件内容类型 主要的是有一个时间字段 然后添加一个事件内容进行测试 [保存并发布] 三.创建日历视图 点击[结构]> ...

  5. [LeetCode] Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  6. xcode8的那些坑儿

    前几天手又贱,更新了xcode8....被几个坑玩坏了.最起码,字体改了,我现在还没有适应.下面列举了这两天遇到的问题 1.关于相册,照相,通讯录,麦克风的权限问题 xcode8打完包安装后,你会发现 ...

  7. 半径无关快速高斯模糊实现(附完整C代码)

    之前,俺也发过不少快速高斯模糊算法. 俺一般认为,只要处理一千六百万像素彩色图片,在2.2GHz的CPU上单核单线程超过1秒的算法,都是不快的. 之前发的几个算法,在俺2.2GHz的CPU上耗时都会超 ...

  8. ●BZOJ 2839 集合计数

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2839 题解: 容斥原理 真的是神题!!! 定义 f[k] 表示交集大小至少为 k时的方案数怎 ...

  9. Django中ORM操作

    ORM操作: class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.Ch ...

  10. Linux查看CPU、内存、进程使用情况(转)

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...