Code covered by the BSD License

4.83333

4.8 | 6 ratings Rate this file 271 Downloads (last 30 days) File Size: 2.8 KB File ID: #33863

by Leo

21 Nov 2011 (Updated 15 Aug 2012)

mex function for calculating histograms of (oriented) gradients (Dalal & Triggs CVPR 2005).

File Information
Description

A mex function for calculating histograms of (oriented) gradients as described in the paper "Histograms of Oriented Gradients for Human Detection"[1]:

http://lear.inrialpes.fr/pubs/2005/DT05/

Function can be called with either one or two arguments :

hogs = HoG(Image,Params);
or
hogs = HoG(Image);

Params should be a size 5 vector with:

Params(0) = number of orientation bins.
Params(1) = cell size.
Params(2) = block size.
Params(3) = 1 for oriented gradients and 0 otherwise.
Params(4) = value for clipping of the L2-norm.

See [1] for more details on these values.

If the function is called with only one parameter then the default values are used:

Params = [9 8 2 0 0.2];

Function can be called for both a RGB and grayscale image.

The function only supports data of type double, image data should first be cast into double i.e. HoG(double(Image)).

Finally [1] mentions the possibility of downweighting "pixels near the edges
of the block by applying a Gaussian spatial window..." and that this leads to and increase in performance of 1% at 10^-4 FPPW. This downweighting scheme is not used by this function.

The HoG function code is part of the MASH public descriptors ("heuristics"):

http://mash-project.eu/

MATLAB release MATLAB 7.9 (R2009b)
18 Aug 2013

Hi, Leo, thank you for your nice work. I have a question about the output. Suppose that I have 12*12 image and I use 9 bin 6 cell and 2*2 block, the output will be 36-D features. Could you tell me what is the order of the output?

14 Aug 2013

Does this function work for 3d images? like a 3d brain image?

19 Mar 2013

Leo, thanks lot!

18 Mar 2013

Hi Victor,

It is a bit difficult to comment on the VL code as it isnt mine, but I think the difference comes from how the normalization of the cells on the boundary is treated.

The cells on the left and bottom boundary only belong to two blocks (or in some cases one). For example there is no block starting at a cell on the left boundary and extending to the left as that would take it outside the image. Thus a 256x256 image only has 63*63 blocks of size 2x2.

If I understand the VL code correctly ,hog.c line 927 and onward, it assumes that every cell belongs to 4 blocks. For the case of boudary cells, where this assumption does not hold, certain block normalization schemes are simply repeated to make up the four blocks, see lines 932-935. So if I am correct, for boundary cells, there should be repetitions in the final descriptor of the VL code. Though obviously you shouldnt cite me on this :)

Hope this helps.

Leo

17 Mar 2013

Hi Leo,

i'm trying to use your code. For an image of size 256x256, with default parameters i'm obtaining a vector of size - 142884 (63*63*9*4). However, using the vl_hog function (from VLFeat.org) with the same parameters im getting a vector of size-147456 (64*64*9*4). Could you clarify this? Furthermore, could you tell me for example how to fold the output vector back (as 3D) in case of signed case. Thanks,
Best regards,
V.

06 Mar 2013

how to write it in matlab
plz help to run the code in matlab

03 Mar 2013

Hi Leo..do u have MATLAB code for HOG descriptor??

29 Jan 2013

Hi Anas,

I am afraid tracking isnt my field soI dont really know. It would depend a lot on the resolution of the camera andhow many fps you want to process. In general HoGs are computationally expensive so I wouldn't be suprised if simpler descriptors are used in practice.

Leo

28 Jan 2013

hello Leo how are you ??
i have a question about your work, could i use it with video in real time ?? i want to detect the human with security camera for example so could i do that ??

04 Dec 2012

Hi Leo,
That was really helpful thank you very much

04 Dec 2012

Hi Hend,

Sorry for the late reply. There is overlapping between the blocks. Each block is shifted 1 cell to the right from the previous one.

Leo

29 Nov 2012

Hi Leo,
thank you for your fast reply. I have one more question as I read your code I noticed that there is no overlapping between the blocks is that right?

26 Nov 2012

Hi Hend,

For the normalization scheme the L2 norm is used. If you want to change normalization schemes you could change lines 147 and 168 :

block_norm+=h[y+i][x+j][k]*h[y+i][x+j][k];

By 1 for oriented gradients and 0 otherwise I mean 1 for signed gradients and 0 for unsigned gradients.

Hope this helps.

Leo

26 Nov 2012

Regarding the parameters, how to set:
2. normalization scheme, I guess only L2-norm is used?

and what do you mean by 1 for oriented gradients and 0 otherwise?

18 Sep 2012

Hi, I have one question.
I want to use HOG with SVM. Your code returned different size's of vector's for different images. But, to train the features with SVM, the vector's must be in the same size.
How can I solve this problem?
Sorry for my bad english, I'm Brazilian.

10 Sep 2012

Thank you for sharing.
I got this error message:
atan2: DOMAIN error
because of the division by zero, could you please fix it. the histogram is computed and it doesn't contain any strange NAN's etc but the message is annoying.

04 Sep 2012

Dear Leo,

Thank you for submitting your implementation. Could you please note the meaning of the algorithm's output in general? What does is produce and in what structure?

13 Jul 2012

I tried to compile it and these are the errors I got. Could you please help me out?

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\yvals.h(576) : error C2371: 'char16_t' : redefinition; different basic types
c:\program files\matlab\r2010a\extern\include\matrix.h(330) : see declaration of 'char16_t'
HoG.cpp(10) : warning C4305: 'initializing' : truncation from 'double' to 'const float'
HoG.cpp(21) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
HoG.cpp(22) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
HoG.cpp(36) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(37) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(39) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(41) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(42) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(44) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(46) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(47) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(52) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(53) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(54) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(58) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(59) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(60) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(63) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(64) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(65) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(70) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(71) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(72) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(76) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(77) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(78) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(81) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(82) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(83) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(113) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(114) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(116) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(140) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(141) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(144) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(145) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(146) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(147) : warning C4244: '+=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(153) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(154) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(155) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(165) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(166) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(167) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(168) : warning C4244: '+=' : conversion from 'double' to 'float', possible loss of data
HoG.cpp(174) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(175) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(176) : warning C4018: '<' : signed/unsigned mismatch
HoG.cpp(199) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
HoG.cpp(200) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
HoG.cpp(224) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
HoG.cpp(225) : warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data

C:\PROGRA~1\MATLAB\R2010A\BIN\MEX.PL: Error: Compile of 'HoG.cpp' failed.

??? Error using ==> mex at 222
Unable to complete successfully.

08 Jun 2012

in mac ox it crashes if you not cast the image as double. i am not sure if this solves the crashing problems.

21 Feb 2012

Sorry double posted, this is my first please excuse. Anyway 5 stars for this, code is good and also because HoG itself is amazing. No trouble compiling on Win 7 x64 with VS C++ 2010 Compiler

21 Feb 2012
04 Feb 2012

Hi Vidit,

I am not very well acquainted with lcc but if I am not mistaken it does not support C++. You need to use a C++ compiler to compile the code.

Leo

04 Feb 2012

Hi Leo,

I am running your code on Matlab 7.8. When compiling the file, I get this error:
lcc preprocessor error: HoG.cpp:4 Could not find include file <vector>
Error HoG.cpp: 5 syntax error; found `namespace' expecting `;'
Error HoG.cpp: 5 syntax error; found `std' expecting `;'
Error HoG.cpp: 30 undeclared identifier `vector'
Error HoG.cpp: 30 illegal expression
Error HoG.cpp: 30 illegal expression
Error HoG.cpp: 30 illegal expression
Error HoG.cpp: 30 illegal expression
Error HoG.cpp: 30 found `int' expected a function
Error HoG.cpp: 30 illegal expression
Error HoG.cpp: 30 operands of > have illegal types `int' and `void'
Error HoG.cpp: 31 illegal expression
Error HoG.cpp: 31 illegal expression
Error HoG.cpp: 31 illegal expression
Error HoG.cpp: 31 illegal expression
Error HoG.cpp: 31 found `int' expected a function
Error HoG.cpp: 31 illegal expression
Error HoG.cpp: 31 operands of > have illegal types `int' and `void'
Error HoG.cpp: 35 syntax error; found `unsigned' expecting `;'
Error HoG.cpp: 35 syntax error; found `unsigned' expecting `;'
Error HoG.cpp: 35 too many errors

C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Compile of 'HoG.cpp' failed.

Vidit

03 Feb 2012

Lino Hi,

I have tried to contact you via email but you haven't replied. I cannot reproduce this error so it is difficult to say what is going on. If anyone has the same problem, please contact me.

Leo

23 Jan 2012

Hi Leo
I'm trying to use this code in a Linux enviroment and Matlab crashes when I use it. The error given is the next one:

MATLAB crash file:/home/lino/matlab_crash_dump.2121
------------------------------------------------------------------------
Segmentation violation detected at Mon Jan 23 11:04:45 2012
------------------------------------------------------------------------

Configuration:
MATLAB Version: 7.8.0.347 (R2009a)
Operating System: Linux 3.1.1-1-ARCH #1 SMP PREEMPT Fri Nov 11 22:28:29 CET 2011 x86_64
GNU C Library: 2.14.1 stable
Window System: The X.Org Foundation (11102000), display :0.0
Current Visual: 0x21 (class 4, depth 24)
Processor ID: x86 Family 6 Model 10 Stepping 7, GenuineIntel
Virtual Machine: Java 1.6.0_04-b12 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Default Encoding: ISO-8859-1

Fault Count: 1

Register State:
rax = 00000000000000bf rbx = 0000000080000000
rcx = 00000003fffffff8 rdx = 0000000000000000
rbp = 00007f770ad419e0 rsi = 0000000000000001
rdi = 00007f7ae94158e8 rsp = 00007f770ad41760
r8 = 000000003fe00000 r9 = 0000000000000000
r10 = 000000003fe00000 r11 = 0000000000000000
r12 = 0000000000000240 r13 = 0000000080000001
r14 = 0000000000000019 r15 = 0000000000000009
rip = 00007f76e404591c flg = 0000000000010a92

Stack Trace:
[7] libmwm_interpreter.so:Resolver::CallMFunction(int, int, _m_operand*, m_operand_storage*, int, _m_operand*, m_operand_storage*, int*)(0x7f76e91f1330, 0, 0x7f770ad43250, 0x2e8ba2e8ba2e8ba3) + 1637 bytes
[8] libmwm_interpreter.so:inResolveMFunctionCall(_m_function_desc*, int, int, _m_operand*, m_operand_storage*, int, _m_operand*, m_operand_storage*, int*, inMarshalType*, int, mpsTypeSequenceNlhs const*, mxArray_tag* (*)(int))(0x7f76e91f1330, 0, 0x7f770ad43250, 0x7f770ad433f8) + 501 bytes
[9] libmwm_interpreter.so:accelImpl::MFunctionCall(_accelOp**)(0x7f770ad431e4, 0x7f76e962c298, 0x1e8a33310, 0x7f76e9623b30) + 239 bytes
[13] libmwm_interpreter.so:inExecuteMFunctionOrScript(Mfh_mp*, bool)(0, 0x7f770ad43750, 0, 0) + 1104 bytes
[14] libmwm_interpreter.so:inRunMfile(int, mxArray_tag**, int, mxArray_tag**, Mfh_mp*, inWorkSpace_tag*)(0, 32, 0x7f76e95f0390, 0x7f770ad44830) + 1411 bytes
[18] libmwm_interpreter.so:inEvalStringWithIsVarFcn(_memory_context*, char const*, EvalType, int, mxArray_tag**, inDebugCheck, _pcodeheader*, int*, bool (*)(void*, char const*), void*, bool, bool)(0, 0, 0x7f7718dbefc0, 0) + 2610 bytes
[19] libmwm_interpreter.so:inEvalCmdWithLocalReturn(char const*, int*, bool, bool, bool (*)(void*, char const*))(0x7f770ad44630, 0x7f76e96cd6b0 "hogs_8=HoG(I);\n", 0x7f770ad447f0, 0) + 149 bytes
[21] libmwbridge.so:mnParser(0x01b324c0, 0x02d642f0, 0x197a3101, 0x0a53fba0) + 355 bytes
[22] libmwmcr.so:mcrInstance::mnParser_on_interpreter_thread()(0x00c00000, 0xfffffffffffffc00, 0xffffffffffffffff, 0x80101020000ffff) + 41 bytes
[26] libmwuix.so:uix_DispatchOrProcess(_XEvent*, _XtAppStruct*, int, bool)(0x01b37a80, 0x7f771a988b5c, 0x7f770ad44bf0, 0x1007f7700000001) + 361 bytes
[27] libmwuix.so:ws_ProcessPendingEventsHelper(int, int, bool)(9, 0x01b31060, 0x01b367a0, 0x01b36700) + 489 bytes
[30] libmwmcr.so:run_init_and_handle_events(void*)(0, 0x00801000, 0x7f770ad44e10, 0x00402670) + 60 bytes
[31] MATLAB:boost::function0<void>::operator()() const(0x7fff61464428, 0x200000000, 0, 0x01b321e0) + 412 bytes
[33] libmwmcr.so:runMcrMain(void*)(0, 0x7f770ad45700, 0, 0) + 28 bytes

16 Jan 2012

Hi Nour,

Perhaps the function is not in Matlab's search path? Another possibility is that you forgot to compile it before running.

Otherwise can't think of what the problem might be.

15 Jan 2012

Hi Leo,
thanks for this submission,
but help needed, I can't run the mex file
I have this message error
>> hogs = HoG(Image);
??? Undefined function or method 'HoG' for input arguments of type 'uint8'.

Can you help me? thanks a lot

02 Jan 2012

Hi syki,

The code has been developed and tested on 64-bit Ubuntu with Matlab 7.9.0 (R2009b).

01 Jan 2012

hi leo
I use a HOG function in matlab? it is recognized by Matlab 7.9 ?

31 Dec 2011

Hi Leo, sorry bout the multiple ratings. The code now works perfectly. Thanks a lot.

31 Dec 2011
27 Dec 2011

Hi Ronald,

I reformatted the code to use vectors instead of static arrays. Should be standard C,C++ compliant.

20 Dec 2011

Hi Ronald, disregard the previous post, it is probably due to the fact that in standard C/C++ arrays h, block (lines 28 and 29) have to be dynamically allocated (or declared using vectors). I don't have time now to correct this, but if you can't do it , I'll try to do it by tomorrow.

20 Dec 2011

Hi Leo, thanks for the reply. I did the changes you mentioned in your post but the problem remain unsolved. I'm wondering could it be due to the platform I'm using? For I'm using Windows.

18 Dec 2011

Hi Ronald, based on those errors maybe the following will help :

add a const in front of each line from 8 to 21, so for example :

int nb_bins = (int) params[0];

should become

const int nb_bins = (int) params[0];

unsigned int i,j,k,cli,x,y;

at line 22.

Finally remove all declarations from the for loops. For example line 39 :

for(unsigned int y=0; y<img_height; y++) {

should become

for(y=0; y<img_height; y++) {

Not sure this helps, hope it does.

Leo

18 Dec 2011

Error HoG.cpp: 28 integer expression must be constant
Error HoG.cpp: 28 integer expression must be constant
Error HoG.cpp: 28 integer expression must be constant
Error HoG.cpp: 29 integer expression must be constant
Error HoG.cpp: 29 integer expression must be constant
Error HoG.cpp: 29 integer expression must be constant
Error HoG.cpp: 31 syntax error; found `int' expecting `;'
Error HoG.cpp: 31 syntax error; found `int' expecting `;'
Error HoG.cpp: 31 syntax error; found `int' expecting `)'
Error HoG.cpp: 31 skipping `int'
Error HoG.cpp: 31 undeclared identifier `x'
Warning HoG.cpp: 31 Statement has no effect
Warning HoG.cpp: 31 unreachable code
Error HoG.cpp: 31 syntax error; found `)' expecting `;'
Error HoG.cpp: 31 illegal statement termination
Error HoG.cpp: 31 skipping `)'
Error HoG.cpp: 32 syntax error; found `int' expecting `;'
Error HoG.cpp: 32 syntax error; found `int' expecting `;'
Error HoG.cpp: 32 syntax error; found `int' expecting `)'
Error HoG.cpp: 32 skipping `int'
Error HoG.cpp: 32 undeclared identifier `y'
Warning HoG.cpp: 32 Statement has no effect
Warning HoG.cpp: 32 unreachable code
Error HoG.cpp: 32 syntax error; found `)' expecting `;'
Error HoG.cpp: 32 too many errors

C:\PROGRA~1\MATLAB\R2011B\BIN\MEX.PL: Error: Compile of 'HoG.cpp' failed.

Error using mex (line 206)
Unable to complete successfully.

18 Dec 2011

Hi Ronald, can you provide me with the compilation errors you get?

18 Dec 2011

Hi Leo, help needed. I can't manage to get this code to work. I can't compile the HoG.cpp file provided. Thanks.

22 Nov 2011

typo in description

26 Dec 2011

code should now be standard C/C++ compliant

15 Aug 2012

Function returns an error if input is not of type double