直接上代码:

  1. #include <opencv2/opencv.hpp>
  2. using namespace cv;
  3. #include <cmath>
  4. using namespace std;
  5. template<typename T>
  6. int _get_hog(vector<float> &hist, const Mat &img, const int nbins = 8, const bool need_normalize=false){
  7. int rows_minus_1 = img.rows - 1;
  8. int cols_minus_1 = img.cols - 1;
  9. float dx, dy;
  10. float angle;
  11. const float angle_base = atan2f(0, -1);
  12. const float angle_piece = 2.f*angle_base / (float)nbins;
  13. int bin_id;
  14. for (int y = 0; y < rows_minus_1; y++){
  15. for (int x = 0; x < cols_minus_1; x++){
  16. dx = (float)(img.at<T>(y, x) - img.at<T>(y, x + 1));
  17. dy = (float)(img.at<T>(y, x) - img.at<T>(y + 1, x));
  18. angle = atan2f(dy, dx) + angle_base;
  19. bin_id = (int)floorf(angle / angle_piece);
  20. hist[bin_id] += 1.f;
  21. }
  22. }
  23. hist[nbins - 1] += hist[nbins];
  24. hist.resize(nbins);
  25. if (!need_normalize){
  26. return 0;
  27. }
  28. float num_pixels = (float)(rows_minus_1*cols_minus_1);
  29. for (int i = 0; i < nbins; i++){
  30. hist[i] = hist[i] / num_pixels;
  31. }
  32. return 0;
  33. }
  34. // support
  35. // CV_8UC1
  36. // CV_32FC1
  37. // CV_64FC1
  38. int get_hog(vector<float> &hist, const Mat &img, const int nbins = 8, const bool need_normalize = false){
  39. if (img.type() != CV_8UC1 && img.type() != CV_32FC1 && img.type() != CV_64FC1){
  40. cerr << __FUNCDNAME__ << " invalid image type!" << endl;
  41. return 1;
  42. }
  43. hist.resize(nbins+1);
  44. hist.assign(nbins+1, 0);
  45. if (img.type() == CV_8UC1){
  46. return _get_hog<uchar> (hist, img, nbins, need_normalize);
  47. }
  48. else if (img.type() == CV_32FC1) {
  49. return _get_hog<float> (hist, img, nbins, need_normalize);
  50. }
  51. else if (img.type() == CV_64FC1) {
  52. return _get_hog<double>(hist, img, nbins, need_normalize);
  53. }
  54. return 1;
  55. }

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. Spring基础复习

    Spring IOC 使用注解实现Bean管理 注解类型: @Component:spring定义的通用注解,可用于注解任何bean @Repository, @Service,@Controller ...

  2. [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  3. Mysql单表查询(胖胖老师)

    数据准备drop table if exists class;create table class(    class_no int(2) unsigned zerofill primary key ...

  4. mysql \N 的疑惑

    \N 在mysql查询为NULL 好像是可以代替成一个字符串,并与后面的单词隔绝,我的理解为下图这样 还是不懂为啥会这样.

  5. [JSOI2007]字符加密

    题目描述 喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法. 例如‘JSOI07’,可以读作 ...

  6. 【BZOJ1016】【JSOI2008】最小生成树计数

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  7. [USACO12OPEN]书架Bookshelf

    Description 当农夫约翰闲的没事干的时候,他喜欢坐下来看书.多年过去,他已经收集了 N 本书 (1 <= N <= 100,000), 他想造一个新的书架来装所有书. 每本书 i ...

  8. [SPOJ962]Intergalactic Map 拆点+最大流

    Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...

  9. bzoj3622已经没有什么好害怕的了 dp+组合+容斥(?)

    3622: 已经没有什么好害怕的了 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1033  Solved: 480[Submit][Status][ ...

  10. Thread类中的静态方法

    1.currentThread() currentThread()方法返回的是对当前正在执行线程对象的引用. package thread; /** * 线程类的构造方法.静态块是被main线程调用的 ...