HOG可视化 opencv
原文: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;
- // HOGDescriptor visual_imagealizer
- // adapted for arbitrary size of feature sets and training images
- Mat get_hogdescriptor_visual_image(Mat& origImg,
- vector<float>& descriptorValues,
- Size winSize,
- Size cellSize,
- int scaleFactor,
- double viz_factor)
- {
- Mat visual_image;
- resize(origImg, visual_image, Size(origImg.cols*scaleFactor, origImg.rows*scaleFactor));
- int gradientBinSize = 9;
- // dividing 180° into 9 bins, how large (in rad) is one bin?
- float radRangeForOneBin = 3.14/(float)gradientBinSize;
- // prepare data structure: 9 orientation / gradient strenghts for each cell
- int cells_in_x_dir = winSize.width / cellSize.width;
- int cells_in_y_dir = winSize.height / cellSize.height;
- int totalnrofcells = cells_in_x_dir * cells_in_y_dir;
- float*** gradientStrengths = new float**[cells_in_y_dir];
- int** cellUpdateCounter = new int*[cells_in_y_dir];
- for (int y=0; y<cells_in_y_dir; y++)
- {
- gradientStrengths[y] = new float*[cells_in_x_dir];
- cellUpdateCounter[y] = new int[cells_in_x_dir];
- for (int x=0; x<cells_in_x_dir; x++)
- {
- gradientStrengths[y][x] = new float[gradientBinSize];
- cellUpdateCounter[y][x] = 0;
- for (int bin=0; bin<gradientBinSize; bin++)
- gradientStrengths[y][x][bin] = 0.0;
- }
- }
- // nr of blocks = nr of cells - 1
- // since there is a new block on each cell (overlapping blocks!) but the last one
- int blocks_in_x_dir = cells_in_x_dir - 1;
- int blocks_in_y_dir = cells_in_y_dir - 1;
- // compute gradient strengths per cell
- int descriptorDataIdx = 0;
- int cellx = 0;
- int celly = 0;
- for (int blockx=0; blockx<blocks_in_x_dir; blockx++)
- {
- for (int blocky=0; blocky<blocks_in_y_dir; blocky++)
- {
- // 4 cells per block ...
- for (int cellNr=0; cellNr<4; cellNr++)
- {
- // compute corresponding cell nr
- int cellx = blockx;
- int celly = blocky;
- if (cellNr==1) celly++;
- if (cellNr==2) cellx++;
- if (cellNr==3)
- {
- cellx++;
- celly++;
- }
- for (int bin=0; bin<gradientBinSize; bin++)
- {
- float gradientStrength = descriptorValues[ descriptorDataIdx ];
- descriptorDataIdx++;
- gradientStrengths[celly][cellx][bin] += gradientStrength;
- } // for (all bins)
- // note: overlapping blocks lead to multiple updates of this sum!
- // we therefore keep track how often a cell was updated,
- // to compute average gradient strengths
- cellUpdateCounter[celly][cellx]++;
- } // for (all cells)
- } // for (all block x pos)
- } // for (all block y pos)
- // compute average gradient strengths
- for (int celly=0; celly<cells_in_y_dir; celly++)
- {
- for (int cellx=0; cellx<cells_in_x_dir; cellx++)
- {
- float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];
- // compute average gradient strenghts for each gradient bin direction
- for (int bin=0; bin<gradientBinSize; bin++)
- {
- gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;
- }
- }
- }
- cout << "descriptorDataIdx = " << descriptorDataIdx << endl;
- // draw cells
- for (int celly=0; celly<cells_in_y_dir; celly++)
- {
- for (int cellx=0; cellx<cells_in_x_dir; cellx++)
- {
- int drawX = cellx * cellSize.width;
- int drawY = celly * cellSize.height;
- int mx = drawX + cellSize.width/2;
- int my = drawY + cellSize.height/2;
- rectangle(visual_image,
- Point(drawX*scaleFactor,drawY*scaleFactor),
- Point((drawX+cellSize.width)*scaleFactor,
- (drawY+cellSize.height)*scaleFactor),
- CV_RGB(100,100,100),
- 1);
- // draw in each cell all 9 gradient strengths
- for (int bin=0; bin<gradientBinSize; bin++)
- {
- float currentGradStrength = gradientStrengths[celly][cellx][bin];
- // no line to draw?
- if (currentGradStrength==0)
- continue;
- float currRad = bin * radRangeForOneBin + radRangeForOneBin/2;
- float dirVecX = cos( currRad );
- float dirVecY = sin( currRad );
- float maxVecLen = cellSize.width/2;
- float scale = viz_factor; // just a visual_imagealization scale,
- // to see the lines better
- // compute line coordinates
- float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;
- float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;
- float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;
- float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;
- // draw gradient visual_imagealization
- line(visual_image,
- Point(x1*scaleFactor,y1*scaleFactor),
- Point(x2*scaleFactor,y2*scaleFactor),
- CV_RGB(0,0,255),
- 1);
- } // for (all bins)
- } // for (cellx)
- } // for (celly)
- // don't forget to free memory allocated by helper data structures!
- for (int y=0; y<cells_in_y_dir; y++)
- {
- for (int x=0; x<cells_in_x_dir; x++)
- {
- delete[] gradientStrengths[y][x];
- }
- delete[] gradientStrengths[y];
- delete[] cellUpdateCounter[y];
- }
- delete[] gradientStrengths;
- delete[] cellUpdateCounter;
- return visual_image;
- }
- int main()
- {
- HOGDescriptor hog;
- hog.winSize=Size(80,128);
- vector<float> des;
- Mat src = imread("d:/1.jpg");
- Mat dst ;
- resize(src,dst,Size(80,128));
- imshow("src",src);
- hog.compute(dst,des);
- Mat d = get_hogdescriptor_visual_image(dst,des,hog.winSize,hog.cellSize,3,2.0);
- imshow("dst",d);
- waitKey();
- return 0;
- }
HOG可视化 opencv相关推荐
- 微笑识别(HOG+SVM+opencv+python)
如愿 一.流程 二.代码 三.随笔 四.参考资料 一.流程 这是得到模型的大致流程,思路还是蛮清晰的,一步一步做就行了 下面是使用训练出的模型来检测时的流程,思路也不难,慢慢做就行 dlib库及其训练 ...
- opencv之使用open3d可视化opencv加载的点云数据
代码展示 import cv2 as cv import open3d as o3dmodelname = "parasaurolophus_6700"detector = cv. ...
- HOG:从理论到OpenCV实践
HOG:从理论到OpenCV实践 时间 2014-03-11 22:55:57 CSDN博客 原文 http://blog.csdn.net/zhazhiqiang/article/details/ ...
- 详细的基于opencv svm hog的描述讲解
(转自http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 1.HOG特征描述子的定义: locally normalised histogram ...
- opencv Hog Demo
需注意此代码要在Release x64之下运行 #include <iostream> #include <stdexcept> #include <opencv2/o ...
- 【OpenCV流程+代码分析】Opencv HOG行人检测 源码分析
[原文:http://blog.csdn.net/ttransposition/article/details/11874285] OpenCV demo演示见本人的另一篇灌水博客 http://bl ...
- 深入浅出理解HOG特征---梯度方向直方图
梯度方向直方图 原文路径:https://www.learnopencv.com/histogram-of-oriented-gradients/ 最近在搞车牌识别的时候,训练样本去识别车牌的时候用到 ...
- Python+OpenCV实用案例应用教程:建立自定义物体检测器
本章将深入探讨物体检测的概念,这是计算机视觉中最常见的挑 战之一.既然在这本教程中已经讲了很多内容了,读到这里,你也许会 想,什么时候才能把计算机视觉应用实践中呢.你是否想过建立一个 系统来检测车辆和 ...
- 目标检测(三)传统目标检测与识别的特征提取——基于HOG特征的目标检测原理
目录 简介 提取HOG特征的步骤 1.预处理获取要计算其特征的输入图像 2.计算图像的梯度 3.计算8×8细胞梯度直方图 4.直方图归一化 5.计算HOG特征向量 Opencv利用HOG特征实现行人检 ...
最新文章
- 现阶段的微信小程序能实现直播功能么?
- C# 语句中的各种单例模式代码
- linux 内存 实例,内存管理与使用实例
- 关于Uncaught SyntaxError: Unexpected identifier
- 普通调幅(AM)与抑制载波双边带调幅(DSB)matlab编程实现
- Git使用命令行回退版本git reset --hard
- 判断存储过程是否存在某个参数名
- OpenCV cvLine
- php代码最佳实践,分享几个 PHP 编码的最佳实践
- python目录名称无效怎么处理_Python目录和文件处理总结详解
- 计算机和绘画的论文,浅析毕沙罗的绘画风格
- 二十个让你泪流满面的瞬间
- Python自动化运维开发----基础(十二)函数
- 无盘服务器秒卡 锐起0359,锐起无盘系统问题汇集
- python十折交叉验证
- 用DirectX12绘制一个几何体的程序详述
- LINQ分页和排序,skip和Take 用法
- jenkins添加从节点
- 电容降压工作原理简介
- 使用Beautiful Soup和lxml轻松搞掂网页数据爬取
热门文章
- linux通过tftp下载的文件大小为0,linux 通过 tftp下载文件
- openstack neutron-fwaas 中的几个概念
- 四位先行进位电路逻辑表达式_如何用基本的逻辑门设计32bit的超前进位加法器?...
- linux手动安装unzip_centos下离线安装zip和unzip
- oracle 数据不可恢复,Oracle数据恢复:错误叠加导致灾难不可恢复解决办法
- 用 IDEA 看源码的正确姿势!你掌握了吗?
- 腾讯员工吐槽:团队来了个阿里高p,瞬间会议变多,群多了
- 每日一皮:你偷偷藏私房钱时被老婆发现的样子...
- 数学之美:“植物身上的黄金分割”
- 你如果只是一直囤干货,那永远不可能进步