直接上代码:

#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. win10安装Ubuntu14.04双系统

    1 制作镜像 UltralISO刻录镜像到U盘,下载地址:http://pan.baidu.com/s/1o7JpthS 2压缩空间给Ubuntu安装 使用windows自带的压缩(磁盘管理) 3安装 ...

  2. mysql索引类型和索引方法

    索引类型 mysql索引类型normal,unique,full text的区别是什么? normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号 ...

  3. 从源码看JDK提供的线程池(ThreadPoolExecutor)

    一丶什么是线程池 (1)博主在听到线程池三个字的时候第一个想法就是数据库连接池,回忆一下,我们在学JavaWeb的时候怎么理解数据库连接池的,数据库创建连接和关闭连接是一个比较耗费资源的事情,对于那些 ...

  4. python字符串-内置方法用法分析

    1.字母大小写相关(中文无效) 1.1 S.upper() -> string 返回一个字母全部大写的副本

  5. 简明shell入门

  6. codevs 搜索题汇总(钻石+大师级)

    1043 方格取数 2000年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 设有N*N的方格图 ...

  7. codefroces 612E Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  8. [JSOI2015]非诚勿扰

    Description [故事背景] JYY赶上了互联网创业的大潮,为非常勿扰开发了最新的手机App实现单身 大龄青年之间的“速配”.然而随着用户数量的增长,JYY发现现有速配的算法似 乎很难满足大家 ...

  9. bzoj 3672: [Noi2014]购票

    Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的 ...

  10. 【Codeforces Round #430 (Div. 2) A C D三个题】

    ·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意:    给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...