hog cython
pip安装cython之后,将下面代码写入hogtest2.pyx文件(我通过改文件后缀新建)
import numpy as np
from PIL import Image
cimport numpy as np
cimport cython
cdef extern from "math.h":
double sqrt(double i)
double fabs(double i)
double floor(double i)
cpdef hog(im, int sbin = 8):
cdef np.ndarray[np.double_t, ndim=3] data, feat
cdef np.ndarray[np.double_t, ndim=1] hist, norm
cdef int blocks0, blocks1
cdef int out0, out1, out2
cdef int visible0, visible1
cdef double dy, dx, v
cdef double dy2, dx2, v2
cdef double dy3, dx3, v3
cdef double best_dot, dot
cdef int best_o
cdef double xp, yp, vx0, vy0, vx1, vy1
cdef int ixp, iyp
cdef double n1, n2, n3, n4, t1, t2, t3, t4, h1, h2, h3, h4
cdef int p
cdef np.ndarray[np.double_t, ndim=1] uu
uu = np.array([ 1.0000, 0.9397, 0.7660, 0.500, 0.1736,
-0.1736, -0.5000, -0.7660, -0.9397])
cdef np.ndarray[np.double_t, ndim=1] vv
vv = np.array([0.0000, 0.3420, 0.6428, 0.8660, 0.9848,
0.9848, 0.8660, 0.6428, 0.3420])
cdef double eps = 0.0001 # to avoid division by 0
cdef unsigned int cc0, cc1, cc2
cdef int x, y, o, q
cdef int dstptr, srcptr
width, height = im.size
blocks0 = height / sbin
blocks1 = width / sbin
out0 = blocks0 - 2
out1 = blocks1 - 2
out2 = 9 + 4
visible0 = blocks0 * sbin
visible1 = blocks1 * sbin
data = np.asarray(im, dtype=np.double)
cc0 = <unsigned int>(0)
cc1 = <unsigned int>(1)
cc2 = <unsigned int>(2)
hist = np.zeros(shape=(blocks0 * blocks1 * 9), dtype=np.double)
norm = np.zeros(shape=(blocks0 * blocks1), dtype=np.double)
feat = np.zeros(shape=(out0, out1, out2), dtype=np.double)
for x from 1 <= x < visible1 - 1:
for y from 1 <= y < visible0 - 1:
dy = data[y + 1, x, cc0] - data[y - 1, x, cc0]
dx = data[y, x + 1, cc0] - data[y, x - 1, cc0]
v = dx * dx + dy * dy
dy2 = data[y + 1, x, cc1] - data[y - 1, x, cc1]
dx2 = data[y, x + 1, cc1] - data[y, x - 1, cc1]
v2 = dx2 * dx2 + dy2 * dy2
dy3 = data[y + 1, x, cc2] - data[y - 1, x, cc2]
dx3 = data[y, x + 1, cc2] - data[y, x - 1, cc2]
v3 = dx3 * dx3 + dy3 * dy3
if v2 > v: # pick channel with strongest gradient
v = v2
dx = dx2
dy = dy2
if v3 > v:
v = v3
dx = dx3
dy = dy3
# snap to one of 9 orientations
best_dot = 0.
best_o = 0
for o from 0 <= o < 9:
dot = fabs(uu[o] * dx + vv[o] * dy)
if dot > best_dot:
best_dot = dot
best_o = o
# add to 4 histograms around pixel using linear interpolation
xp = (<double>(x) + 0.5) / <double>(sbin) - 0.5
yp = (<double>(y) + 0.5) / <double>(sbin) - 0.5
ixp = <int>floor(xp)
iyp = <int>floor(yp)
vx0 = xp - ixp
vy0 = yp - iyp
vx1 = 1.0 - vx0
vy1 = 1.0 - vy0
v = sqrt(v)
if ixp >= 0 and iyp >= 0:
hist[ixp * blocks0 + iyp + best_o*blocks0*blocks1] += vx1 * vy1 * v
if ixp + 1 < blocks1 and iyp >= 0:
hist[(ixp + 1) * blocks0 + iyp + best_o*blocks0*blocks1] += vx0 * vy1 * v
if ixp >= 0 and iyp + 1 < blocks0:
hist[ixp * blocks0 + (iyp + 1) + best_o*blocks0*blocks1] += vx1 * vy0 * v
if ixp + 1 < blocks1 and iyp + 1 < blocks0:
hist[(ixp + 1) * blocks0 + (iyp + 1) + best_o * blocks0 * blocks1] += vx0 * vy0 * v
# compute energy in each block by summing over orientations
for o from 0 <= o < 9:
for q from 0 <= q < blocks0 * blocks1:
norm[q] += hist[o * blocks0 * blocks1 + q] * hist[o * blocks0 * blocks1 + q]
# compute normalized values
for x from 0 <= x < out1:
for y from 0 <= y < out0:
p = (x+1) * blocks0 + y + 1
n1 = 1.0 / sqrt(norm[p] + norm[p+1] + norm[p+blocks0] + norm[p+blocks0+1] + eps)
p = (x+1) * blocks0 + y
n2 = 1.0 / sqrt(norm[p] + norm[p+1] + norm[p+blocks0] + norm[p+blocks0+1] + eps)
p = x * blocks0 + y + 1
n3 = 1.0 / sqrt(norm[p] + norm[p+1] + norm[p+blocks0] + norm[p+blocks0+1] + eps)
p = x * blocks0 + y
n4 = 1.0 / sqrt(norm[p] + norm[p+1] + norm[p+blocks0] + norm[p+blocks0+1] + eps)
t1 = 0
t2 = 0
t3 = 0
t4 = 0
srcptr = (x+1) * blocks0 + y + 1
for o from 0 <= o < 9:
h1 = hist[srcptr] * n1
h2 = hist[srcptr] * n2
h3 = hist[srcptr] * n3
h4 = hist[srcptr] * n4
# for some reason, gcc will not automatically inline
# the min function here, so we just do it ourselves
# for impressive speedups
if h1 > 0.2:
h1 = 0.2
if h2 > 0.2:
h2 = 0.2
if h3 > 0.2:
h3 = 0.2
if h4 > 0.2:
h4 = 0.2
feat[y, x, o] = 0.5 * (h1 + h2 + h3 + h4)
t1 += h1
t2 += h2
t3 += h3
t4 += h4
srcptr += blocks0 * blocks1
feat[y, x, 9] = 0.2357 * t1
dstptr += out0 * out1
feat[y, x, 10] = 0.2357 * t2
dstptr += out0 * out1
feat[y, x, 11] = 0.2357 * t3
dstptr += out0 * out1
feat[y, x, 12] = 0.2357 * t4
return feat
cpdef hogpad(np.ndarray[np.double_t, ndim=3] hog):
cdef np.ndarray[np.double_t, ndim=3] out
cdef int i, j, k
cdef int w = hog.shape[0], h = hog.shape[1], z = hog.shape[2]
out = np.zeros((w + 2, h + 2, z))
for i in range(w):
for j in range(h):
for k in range(z):
out[i+1, j+1, k] = hog[i, j, k]
return out
cpdef rgbhist(im, int binsize = 8):
"""
Computes an RGB color histogram with a binsize.
"""
cdef int w = im.size[0], h = im.size[1]
cdef np.ndarray[np.uint8_t, ndim=3] data = np.asarray(im)
cdef np.ndarray[np.double_t, ndim=1] hist
hist = np.zeros(binsize * binsize * binsize)
for i from 0 <= i < w:
for j from 0 <= j < h:
bin = (<int>data[j,i,0]) / (256/binsize)
bin += (<int>data[j,i,1]) / (256/binsize) * binsize
bin += (<int>data[j,i,2]) / (256/binsize) * binsize * binsize
hist[bin] += 1
return hist
cpdef rgbmean(im):
"""
Computes mean and covariances of RGB colors.
"""
cdef int w = im.size[0], h = im.size[1]
cdef double r, g, b
cdef np.ndarray[np.uint8_t, ndim=3] data = np.asarray(im)
cdef np.ndarray[np.double_t, ndim=1] out = np.zeros(9)
for i in range(w):
for j in range(h):
r = data[j, i, 0] / 255.
g = data[j, i, 1] / 255.
b = data[j, i, 2] / 255.
out[0] += r
out[1] += g
out[2] += b
out[3] += r * r
out[4] += r * g
out[5] += r * b
out[6] += g * g
out[7] += g * b
out[8] += b * b
return out / (w * h)
然后,新建setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
setup(name='hog2',
ext_modules=cythonize("hogtest2.pyx"),
include_dirs=[numpy.get_include()]
)
cmd 运行 python setup.py build
继续运行python setup.py install
将运行后出现的build文件夹中的pyd后缀文件放到python的LIB\sit-packages里,或者放在待会要跑的py文件同目录下
我新建了一个test.py
from PIL import Image
import hogtest2
img = Image.open("cat.jpg")
hogtest2.hog(img)
a=hogtest2.rgbmean(img)
print(a)
是可用的
HOG代码来自这里(做了一丢丢修改)
hog cython的更多相关文章
- 特征检测之HOG
参考: http://blog.csdn.net/liulina603/article/details/8291093 http://blog.csdn.net/woxincd/article/det ...
- libsvm Minist Hog 手写体识别(源码文件)
以上是我上一篇文章中的代码实现,里面分别用了opencv中的SVM和LibSVM,opencv的SVM用起来更方便,但貌似内部其实也是基于Libsvm,同样的参数训练出来的结果是一致的,里面有Libs ...
- libsvm Minist Hog 手写体识别
统计手写数字集的HOG特征 转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 这篇文章是模式识别的小作业,利用sv ...
- 这是啥-Cython语言简单介绍
Cython是一种既可以编写c又可以编写python的编程语言,他的目标是成为一个python语言的超集,为python提供高层次的.面向对象的.函数化.动态编程功能.不同于纯粹的python,它提供 ...
- HoG
实现步骤 先计算每一个像素点位置上x和y方向上的梯度. 这样在每一个像素点位置上得到一个二维向量, 计算它的方向和模长 将图片分为一个个的cell, 如\(8\times 8\). 计算它的HOG: ...
- matlab实现hog特征
%%matlab实现hog特征 %修改自http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html %input: img %outp ...
- OpenCV人形检测Hog
#include "iostream" #include "queue" using namespace std; #include "opencv2 ...
- 【HOG】
http://blog.csdn.net/masibuaa/article/details/12917961 把这份资料大概看完了 大概了解Hog了
- 学习OpenCV——HOG+SVM
#include "cv.h" #include "highgui.h" #include "stdafx.h" #include < ...
随机推荐
- cocos2dx C++ imageView(图片/九宫格)相关属性大全
ImageView * imageView = ImageView::create("cocosui/ccicon.png");//新建图片 imageView->setSc ...
- 文档设计也需要坚持DRY原则--支付中心应用部署结构图完善
今天上午,我拿着支付中心的设计文档,给入职不久的同事讲解目前支付中心系统的应用部署情况.当时同事嗯嗯地点头反应. 下午呢,发现自己设计的有问题,赶紧给予完善. 代码重构方面讲究DRY编程原则.我们在设 ...
- PHP json_encode/json_decode与serialize/unserializ性能测
PHP里面,有时候出于实际需求考虑,需要将某些信息以数组的方式进行存储,甚至有时候介于数组.字符串两者之间,很难确定是数组还是字符串,如果最终还需要将这些信息存储到文件系统中,而且要保证正确无误的存储 ...
- CSU 1817 Bones’s Battery Submit(二分+Floyd)
Bones's Battery Submit [题目链接]Bones's Battery Submit [题目类型]二分+Floyd &题意: 有n个点,m条边,从点ui到点vi的费电量是di ...
- python3学习笔记之安装
一.Python安装 1.下载地址: https://www.python.org/downloads/release/python-365/ 2. Linux系统自带Python2.7,如需安装3 ...
- EL语言表达式 (一)【语法和特点】
一.基本语法规则: EL表达式语言以“${”开头,以"}"结尾的程序段,具体格式如下: ${expression} 其中expression:表示要指定输出的内容和字符串以及EL运 ...
- scrapy yield
生成器 一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环 ...
- 17. Letter Combinations of a Phone Number(bfs)
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 互联网级监控系统必备-时序数据库之Influxdb技术
时间序列数据库,简称时序数据库,Time Series Database,一个全新的领域,最大的特点就是每个条数据都带有Time列. 时序数据库到底能用到什么业务场景,答案是:监控系统. Baidu一 ...
- MySql 应用语句
[1]MySQL基础语句 -- 查询mysql版本号 SELECT VERSION(); -- 创建数据库 DROP DATABASE IF EXISTS study; -- 如果存在先删除 CREA ...