直接上代码:

#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. SQL基础----DCL

    在之前的文章已经讲到SQL基础DDL(数据库定义语句 http://www.cnblogs.com/cxq0017/p/6433938.html)和 DML(数据库操作语句 http://www.cn ...

  2. 学习React系列(五)——使性能最优

    提高性能可分为两方面: 一.配置层面 二.代码层面 本文只从代码层面考虑: 一.避免重复渲染 这里要说一句: 当shouldComponentUpdate返回false的时候不触发render函数也就 ...

  3. C# 删除文件夹

    三种方法 1.这种方法简单,能删除文件夹内的所有文件(文件及子目录) DirectoryInfo di = new DirectoryInfo(string Path);         di.Del ...

  4. JQ五星好评效果

    $(".list-txt ul").find("li").click(function(){    if($(this).index()==0){       ...

  5. jade 详解

    简介 jade 是HTMl模板引擎,用javascript编写,可以在Node.js中使用.本文主要介绍原生node操作jade文件的方法.   安装 npm install jade 方法(API) ...

  6. [BZOJ 5093]图的价值

    Description 题库链接 一个带标号的图的价值定义为每个点度数的 \(k\) 次方的和.给定 \(n\) 和 \(k\) ,请计算所有 \(n\) 个点的带标号的简单无向图的价值之和.对 \( ...

  7. 数轴line

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq8AAAGaCAYAAAAhPqoeAAAgAElEQVR4nOzdCbh2U/k/8C0NpkgRzZ

  8. 洛谷P3233 [HNOI2014]世界树

    虚树= = #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring&g ...

  9. 【BZOJ1951】【SDOI2010】古代猪文

    Background "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很 ...

  10. bzoj 2560: 串珠子

    Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不 ...