原文:http://blog.csdn.net/yeyang911/article/details/25645177

#include <opencv2/opencv.hpp>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <vector>
using namespace cv;
using namespace std;

  1. // HOGDescriptor visual_imagealizer
  2. // adapted for arbitrary size of feature sets and training images
  3. Mat get_hogdescriptor_visual_image(Mat& origImg,
  4. vector<float>& descriptorValues,
  5. Size winSize,
  6. Size cellSize,
  7. int scaleFactor,
  8. double viz_factor)
  9. {
  10. Mat visual_image;
  11. resize(origImg, visual_image, Size(origImg.cols*scaleFactor, origImg.rows*scaleFactor));
  12. int gradientBinSize = 9;
  13. // dividing 180° into 9 bins, how large (in rad) is one bin?
  14. float radRangeForOneBin = 3.14/(float)gradientBinSize;
  15. // prepare data structure: 9 orientation / gradient strenghts for each cell
  16. int cells_in_x_dir = winSize.width / cellSize.width;
  17. int cells_in_y_dir = winSize.height / cellSize.height;
  18. int totalnrofcells = cells_in_x_dir * cells_in_y_dir;
  19. float*** gradientStrengths = new float**[cells_in_y_dir];
  20. int** cellUpdateCounter   = new int*[cells_in_y_dir];
  21. for (int y=0; y<cells_in_y_dir; y++)
  22. {
  23. gradientStrengths[y] = new float*[cells_in_x_dir];
  24. cellUpdateCounter[y] = new int[cells_in_x_dir];
  25. for (int x=0; x<cells_in_x_dir; x++)
  26. {
  27. gradientStrengths[y][x] = new float[gradientBinSize];
  28. cellUpdateCounter[y][x] = 0;
  29. for (int bin=0; bin<gradientBinSize; bin++)
  30. gradientStrengths[y][x][bin] = 0.0;
  31. }
  32. }
  33. // nr of blocks = nr of cells - 1
  34. // since there is a new block on each cell (overlapping blocks!) but the last one
  35. int blocks_in_x_dir = cells_in_x_dir - 1;
  36. int blocks_in_y_dir = cells_in_y_dir - 1;
  37. // compute gradient strengths per cell
  38. int descriptorDataIdx = 0;
  39. int cellx = 0;
  40. int celly = 0;
  41. for (int blockx=0; blockx<blocks_in_x_dir; blockx++)
  42. {
  43. for (int blocky=0; blocky<blocks_in_y_dir; blocky++)
  44. {
  45. // 4 cells per block ...
  46. for (int cellNr=0; cellNr<4; cellNr++)
  47. {
  48. // compute corresponding cell nr
  49. int cellx = blockx;
  50. int celly = blocky;
  51. if (cellNr==1) celly++;
  52. if (cellNr==2) cellx++;
  53. if (cellNr==3)
  54. {
  55. cellx++;
  56. celly++;
  57. }
  58. for (int bin=0; bin<gradientBinSize; bin++)
  59. {
  60. float gradientStrength = descriptorValues[ descriptorDataIdx ];
  61. descriptorDataIdx++;
  62. gradientStrengths[celly][cellx][bin] += gradientStrength;
  63. } // for (all bins)
  64. // note: overlapping blocks lead to multiple updates of this sum!
  65. // we therefore keep track how often a cell was updated,
  66. // to compute average gradient strengths
  67. cellUpdateCounter[celly][cellx]++;
  68. } // for (all cells)
  69. } // for (all block x pos)
  70. } // for (all block y pos)
  71. // compute average gradient strengths
  72. for (int celly=0; celly<cells_in_y_dir; celly++)
  73. {
  74. for (int cellx=0; cellx<cells_in_x_dir; cellx++)
  75. {
  76. float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];
  77. // compute average gradient strenghts for each gradient bin direction
  78. for (int bin=0; bin<gradientBinSize; bin++)
  79. {
  80. gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;
  81. }
  82. }
  83. }
  84. cout << "descriptorDataIdx = " << descriptorDataIdx << endl;
  85. // draw cells
  86. for (int celly=0; celly<cells_in_y_dir; celly++)
  87. {
  88. for (int cellx=0; cellx<cells_in_x_dir; cellx++)
  89. {
  90. int drawX = cellx * cellSize.width;
  91. int drawY = celly * cellSize.height;
  92. int mx = drawX + cellSize.width/2;
  93. int my = drawY + cellSize.height/2;
  94. rectangle(visual_image,
  95. Point(drawX*scaleFactor,drawY*scaleFactor),
  96. Point((drawX+cellSize.width)*scaleFactor,
  97. (drawY+cellSize.height)*scaleFactor),
  98. CV_RGB(100,100,100),
  99. 1);
  100. // draw in each cell all 9 gradient strengths
  101. for (int bin=0; bin<gradientBinSize; bin++)
  102. {
  103. float currentGradStrength = gradientStrengths[celly][cellx][bin];
  104. // no line to draw?
  105. if (currentGradStrength==0)
  106. continue;
  107. float currRad = bin * radRangeForOneBin + radRangeForOneBin/2;
  108. float dirVecX = cos( currRad );
  109. float dirVecY = sin( currRad );
  110. float maxVecLen = cellSize.width/2;
  111. float scale = viz_factor; // just a visual_imagealization scale,
  112. // to see the lines better
  113. // compute line coordinates
  114. float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;
  115. float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;
  116. float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;
  117. float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;
  118. // draw gradient visual_imagealization
  119. line(visual_image,
  120. Point(x1*scaleFactor,y1*scaleFactor),
  121. Point(x2*scaleFactor,y2*scaleFactor),
  122. CV_RGB(0,0,255),
  123. 1);
  124. } // for (all bins)
  125. } // for (cellx)
  126. } // for (celly)
  127. // don't forget to free memory allocated by helper data structures!
  128. for (int y=0; y<cells_in_y_dir; y++)
  129. {
  130. for (int x=0; x<cells_in_x_dir; x++)
  131. {
  132. delete[] gradientStrengths[y][x];
  133. }
  134. delete[] gradientStrengths[y];
  135. delete[] cellUpdateCounter[y];
  136. }
  137. delete[] gradientStrengths;
  138. delete[] cellUpdateCounter;
  139. return visual_image;
  140. }
  141. int main()
  142. {
  143. HOGDescriptor hog;
  144. hog.winSize=Size(80,128);
  145. vector<float> des;
  146. Mat src = imread("d:/1.jpg");
  147. Mat dst ;
  148. resize(src,dst,Size(80,128));
  149. imshow("src",src);
  150. hog.compute(dst,des);
  151. Mat d = get_hogdescriptor_visual_image(dst,des,hog.winSize,hog.cellSize,3,2.0);
  152. imshow("dst",d);
  153. waitKey();
  154. return 0;
  155. }

HOG可视化 opencv相关推荐

  1. 微笑识别(HOG+SVM+opencv+python)

    如愿 一.流程 二.代码 三.随笔 四.参考资料 一.流程 这是得到模型的大致流程,思路还是蛮清晰的,一步一步做就行了 下面是使用训练出的模型来检测时的流程,思路也不难,慢慢做就行 dlib库及其训练 ...

  2. opencv之使用open3d可视化opencv加载的点云数据

    代码展示 import cv2 as cv import open3d as o3dmodelname = "parasaurolophus_6700"detector = cv. ...

  3. HOG:从理论到OpenCV实践

    HOG:从理论到OpenCV实践 时间 2014-03-11 22:55:57 CSDN博客 原文  http://blog.csdn.net/zhazhiqiang/article/details/ ...

  4. 详细的基于opencv svm hog的描述讲解

    (转自http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 1.HOG特征描述子的定义: locally normalised histogram ...

  5. opencv Hog Demo

    需注意此代码要在Release x64之下运行  #include <iostream> #include <stdexcept> #include <opencv2/o ...

  6. 【OpenCV流程+代码分析】Opencv HOG行人检测 源码分析

    [原文:http://blog.csdn.net/ttransposition/article/details/11874285] OpenCV demo演示见本人的另一篇灌水博客 http://bl ...

  7. 深入浅出理解HOG特征---梯度方向直方图

    梯度方向直方图 原文路径:https://www.learnopencv.com/histogram-of-oriented-gradients/ 最近在搞车牌识别的时候,训练样本去识别车牌的时候用到 ...

  8. Python+OpenCV实用案例应用教程:建立自定义物体检测器

    本章将深入探讨物体检测的概念,这是计算机视觉中最常见的挑 战之一.既然在这本教程中已经讲了很多内容了,读到这里,你也许会 想,什么时候才能把计算机视觉应用实践中呢.你是否想过建立一个 系统来检测车辆和 ...

  9. 目标检测(三)传统目标检测与识别的特征提取——基于HOG特征的目标检测原理

    目录 简介 提取HOG特征的步骤 1.预处理获取要计算其特征的输入图像 2.计算图像的梯度 3.计算8×8细胞梯度直方图 4.直方图归一化 5.计算HOG特征向量 Opencv利用HOG特征实现行人检 ...

最新文章

  1. 现阶段的微信小程序能实现直播功能么?
  2. C# 语句中的各种单例模式代码
  3. linux 内存 实例,内存管理与使用实例
  4. 关于Uncaught SyntaxError: Unexpected identifier
  5. 普通调幅(AM)与抑制载波双边带调幅(DSB)matlab编程实现
  6. Git使用命令行回退版本git reset --hard
  7. 判断存储过程是否存在某个参数名
  8. OpenCV cvLine
  9. php代码最佳实践,分享几个 PHP 编码的最佳实践
  10. python目录名称无效怎么处理_Python目录和文件处理总结详解
  11. 计算机和绘画的论文,浅析毕沙罗的绘画风格
  12. 二十个让你泪流满面的瞬间
  13. Python自动化运维开发----基础(十二)函数
  14. 无盘服务器秒卡 锐起0359,锐起无盘系统问题汇集
  15. python十折交叉验证
  16. 用DirectX12绘制一个几何体的程序详述
  17. LINQ分页和排序,skip和Take 用法
  18. jenkins添加从节点
  19. 电容降压工作原理简介
  20. 使用Beautiful Soup和lxml轻松搞掂网页数据爬取

热门文章

  1. linux通过tftp下载的文件大小为0,linux 通过 tftp下载文件
  2. openstack neutron-fwaas 中的几个概念
  3. 四位先行进位电路逻辑表达式_如何用基本的逻辑门设计32bit的超前进位加法器?...
  4. linux手动安装unzip_centos下离线安装zip和unzip
  5. oracle 数据不可恢复,Oracle数据恢复:错误叠加导致灾难不可恢复解决办法
  6. 用 IDEA 看源码的正确姿势!你掌握了吗?
  7. 腾讯员工吐槽:团队来了个阿里高p,瞬间会议变多,群多了
  8. 每日一皮:你偷偷藏私房钱时被老婆发现的样子...
  9. 数学之美:“植物身上的黄金分割”
  10. 你如果只是一直囤干货,那永远不可能进步