HOG OpenCV 代码片段
直接上代码:
#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 代码片段的更多相关文章
- opencv代码片段合集
个人笔记 长期更新 #### 创建一个图片 import cv2 # Not actually necessary if you just want to create an image. impor ...
- CMake相关代码片段
目录 用于执行CMake的.bat脚本 CMakeLists.txt和.cmake中的代码片段 判断平台:32位还是64位? 判断Visual Studio版本 判断操作系统 判断是Debug还是Re ...
- sublimetext3中保存代码片段
在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...
- Code Snippets 代码片段
Code Snippets 代码片段 1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...
- 10个 jQuery 代码片段,可以帮你快速开发。
转载自:http://mp.weixin.qq.com/s/mMstI10vqwu8PvUwlLborw 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而 ...
- 代码片段添加智能提示,打造一款人见人爱的ORM框架
SqlSugar ORM优点: 1.高性能,达到原生最高水准,比SqlHelper性能要高,比Dapper快30% 比EF快50% 2.支持多种数据库 ,sql版本更新最快,其它会定期更新,可以在多种 ...
- js/jquery/html前端开发常用到代码片段
1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...
- Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!
使用Code Snippet简化Coding 在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符 ...
- Visual Studio 的代码片段工具
当安装完Visual Studio之后,会有附带一些原生的代码片段文件(*.snippet),对于vs2013参考目录如下: X:\Program Files (x86)\Microsoft Visu ...
随机推荐
- Spring基础复习
Spring IOC 使用注解实现Bean管理 注解类型: @Component:spring定义的通用注解,可用于注解任何bean @Repository, @Service,@Controller ...
- [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 ...
- Mysql单表查询(胖胖老师)
数据准备drop table if exists class;create table class( class_no int(2) unsigned zerofill primary key ...
- mysql \N 的疑惑
\N 在mysql查询为NULL 好像是可以代替成一个字符串,并与后面的单词隔绝,我的理解为下图这样 还是不懂为啥会这样.
- [JSOI2007]字符加密
题目描述 喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法. 例如‘JSOI07’,可以读作 ...
- 【BZOJ1016】【JSOI2008】最小生成树计数
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
- [USACO12OPEN]书架Bookshelf
Description 当农夫约翰闲的没事干的时候,他喜欢坐下来看书.多年过去,他已经收集了 N 本书 (1 <= N <= 100,000), 他想造一个新的书架来装所有书. 每本书 i ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- bzoj3622已经没有什么好害怕的了 dp+组合+容斥(?)
3622: 已经没有什么好害怕的了 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1033 Solved: 480[Submit][Status][ ...
- Thread类中的静态方法
1.currentThread() currentThread()方法返回的是对当前正在执行线程对象的引用. package thread; /** * 线程类的构造方法.静态块是被main线程调用的 ...