17

I use the HOGDescriptor of the OpenCV C++ Lib to compute the feature vectors of an images. I would like to visualize the features in the source image. Can anyone help me?

ChHaupt
  • 363
  • 3
  • 6
  • 17
  • For anyone who wants to work on this, here is a link to the [HOG details](http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients) which seems to include all the definitions you would need to figure out some visualization. – KobeJohn Jun 02 '12 at 13:20
  • Excellent HOG description, but it dosnt help me to visualize the features in C++ with OpenCV. – ChHaupt Jun 02 '12 at 13:26
  • Are you serious? Without those definitions, there is no context for the values in the descriptors. Are you saying that you already understand everything about HOG, but you just don't know how to make a graph / GUI in C++? – KobeJohn Jun 02 '12 at 13:36

4 Answers4

28

I had exactly the same problem today. Computing a HOGDescriptor vector for a 64x128 image using OpenCV's HOGDescriptor::compute() function is easy, but there is no built-in functionality to visualize it.

Finally I managed to understand how the gradient orientation magnitudes are stored in the 3870 long HOG descriptor vector.

You can find my C++ code for visualizing the HOGDescriptor here:

http://www.juergenbrauer.org/old_wiki/doku.php?id=public:hog_descriptor_computation_and_visualization

Hope it helps!

Jürgen

Jürgen Brauer
  • 423
  • 3
  • 6
  • I noticed your code swapped indexing sequence "Y axis to X axis" with "X axis to Y axis." "for (int blockx=0; blockx – user1098761 May 18 '16 at 22:26
  • Computing the gradients per cell and visualizing the gradients per cell are independent - as far as I can see. One can first run over all rows, then run over all columns (outer loop: for y ...) or the other way round (outer loop: for x ...). It only makes a difference in what I draw first, but the result should be the same. Further, the order of the gradient information in the HOG descriptor vector is fixed when OpenCV's functionality is used to compute the HOG descriptor vector. – Jürgen Brauer May 23 '16 at 07:06
  • 2
    Thanks, but the link is dead. – nurettin May 07 '17 at 18:24
13

HOGgles¹ is a method developed for HOG visualization, published on ICCV 2013. Here is an example:

What does HOG sees?

This visualization tool may be more useful than plotting the gradient vectors of HOG because one can see better why HOG failed for a given sample.

More information can be found here: http://web.mit.edu/vondrick/ihog/


¹C. Vondrick, A. Khosla, T. Malisiewicz, A. Torralba. "HOGgles: Visualizing Object Detection Features" International Conference on Computer Vision (ICCV), Sydney, Australia, December 2013.

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
2

This opencv group discussion leads to a library written at Brown University.

In HOGpicture.m you should be able to get an idea how to visualize the descriptors. Here is the relevant (matlab) code. Is it enough for you to make something for yourself?

(below code is released under an MIT license)

function im = HOGpicture(w, bs)

% HOGpicture(w, bs)
% Make picture of positive HOG weights.

% construct a "glyph" for each orientation
bim1 = zeros(bs, bs);
bim1(:,round(bs/2):round(bs/2)+1) = 1;
bim = zeros([size(bim1) 9]);
bim(:,:,1) = bim1;
for i = 2:9,
  bim(:,:,i) = imrotate(bim1, -(i-1)*20, 'crop');
end

% make pictures of positive weights bs adding up weighted glyphs
s = size(w);    
w(w < 0) = 0;    
im = zeros(bs*s(1), bs*s(2));
for i = 1:s(1),
  iis = (i-1)*bs+1:i*bs;
  for j = 1:s(2),
    jjs = (j-1)*bs+1:j*bs;          
    for k = 1:9,
      im(iis,jjs) = im(iis,jjs) + bim(:,:,k) * w(i,j,k);
    end
  end
end
Stefan
  • 5,203
  • 8
  • 27
  • 51
KobeJohn
  • 7,390
  • 6
  • 41
  • 62
  • Thx. It's hard for me to read Matlap Syntax. I can partially follow the computation, but dont see how it helps me to visualize features. Is there any description to the code? – ChHaupt Jun 02 '12 at 13:46
  • I haven't used Matlab for many many years, so it's not immediately obvious to me either. If I get some time, I'll add my own notes to the code that I can understand. Assuming you have a clear description of what to put in each pixel of an image, will you be able to handle it from there? Or are you asking for both the HOG visualization technique and how to display the image with a GUI? Please clarify your question. – KobeJohn Jun 02 '12 at 13:58
  • I asking for the HOG visualization technique. – ChHaupt Jun 02 '12 at 14:03
  • Can you tell me what 1 feature of the feature vector represented? For example. I have an imgage (64x128) with 8x8 px per Cell and a block (105 blocks per image) of 2x2 cells with 9 bins per HOG (Dalal-triggs). This results in 3780 (= 105x2x2x9) features. How i visualize the features? – ChHaupt Jun 03 '12 at 12:20
  • Or how can i represent 1 of the 3780 features in the image source? – ChHaupt Jun 03 '12 at 12:27
2

I reimplement HOGImage for any blockSize and cellSize, which is based on Jürgen Brauer's. See https://github.com/zhouzq-thu/HOGImage.

  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Anton Menshov Jul 04 '19 at 01:25