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;



派生到我的代码片

  1. // HOGDescriptor visual_imagealizer  
  2. // adapted for arbitrary size of feature sets and training images  
  3. Mat get_hogdescriptor_visual_image(Mat& origImg,  
  4.     vector<float>& descriptorValues,  
  5.     Size winSize,  
  6.     Size cellSize,                                     
  7.     int scaleFactor,  
  8.     double viz_factor)  
  9.     {     
  10.     Mat visual_image;  
  11.     resize(origImg, visual_image, Size(origImg.cols*scaleFactor, origImg.rows*scaleFactor));  
  12.   
  13.     int gradientBinSize = 9;  
  14.     // dividing 180° into 9 bins, how large (in rad) is one bin?  
  15.     float radRangeForOneBin = 3.14/(float)gradientBinSize;   
  16.   
  17.     // prepare data structure: 9 orientation / gradient strenghts for each cell  
  18.     int cells_in_x_dir = winSize.width / cellSize.width;  
  19.     int cells_in_y_dir = winSize.height / cellSize.height;  
  20.     int totalnrofcells = cells_in_x_dir * cells_in_y_dir;  
  21.     float*** gradientStrengths = new float**[cells_in_y_dir];  
  22.     int** cellUpdateCounter   = new int*[cells_in_y_dir];  
  23.     for (int y=0; y<cells_in_y_dir; y++)  
  24.         {  
  25.         gradientStrengths[y] = new float*[cells_in_x_dir];  
  26.         cellUpdateCounter[y] = new int[cells_in_x_dir];  
  27.         for (int x=0; x<cells_in_x_dir; x++)  
  28.             {  
  29.             gradientStrengths[y][x] = new float[gradientBinSize];  
  30.             cellUpdateCounter[y][x] = 0;  
  31.   
  32.             for (int bin=0; bin<gradientBinSize; bin++)  
  33.                 gradientStrengths[y][x][bin] = 0.0;  
  34.             }  
  35.         }  
  36.   
  37.     // nr of blocks = nr of cells - 1  
  38.     // since there is a new block on each cell (overlapping blocks!) but the last one  
  39.     int blocks_in_x_dir = cells_in_x_dir - 1;  
  40.     int blocks_in_y_dir = cells_in_y_dir - 1;  
  41.   
  42.     // compute gradient strengths per cell  
  43.     int descriptorDataIdx = 0;  
  44.     int cellx = 0;  
  45.     int celly = 0;  
  46.   
  47.     for (int blockx=0; blockx<blocks_in_x_dir; blockx++)  
  48.         {  
  49.         for (int blocky=0; blocky<blocks_in_y_dir; blocky++)              
  50.             {  
  51.             // 4 cells per block ...  
  52.             for (int cellNr=0; cellNr<4; cellNr++)  
  53.                 {  
  54.                 // compute corresponding cell nr  
  55.                 int cellx = blockx;  
  56.                 int celly = blocky;  
  57.                 if (cellNr==1) celly++;  
  58.                 if (cellNr==2) cellx++;  
  59.                 if (cellNr==3)  
  60.                     {  
  61.                     cellx++;  
  62.                     celly++;  
  63.                     }  
  64.   
  65.                 for (int bin=0; bin<gradientBinSize; bin++)  
  66.                     {  
  67.                     float gradientStrength = descriptorValues[ descriptorDataIdx ];  
  68.                     descriptorDataIdx++;  
  69.   
  70.                     gradientStrengths[celly][cellx][bin] += gradientStrength;  
  71.   
  72.                     } // for (all bins)  
  73.   
  74.   
  75.                 // note: overlapping blocks lead to multiple updates of this sum!  
  76.                 // we therefore keep track how often a cell was updated,  
  77.                 // to compute average gradient strengths  
  78.                 cellUpdateCounter[celly][cellx]++;  
  79.   
  80.                 } // for (all cells)  
  81.   
  82.   
  83.             } // for (all block x pos)  
  84.         } // for (all block y pos)  
  85.   
  86.   
  87.     // compute average gradient strengths  
  88.     for (int celly=0; celly<cells_in_y_dir; celly++)  
  89.         {  
  90.         for (int cellx=0; cellx<cells_in_x_dir; cellx++)  
  91.             {  
  92.   
  93.             float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];  
  94.   
  95.             // compute average gradient strenghts for each gradient bin direction  
  96.             for (int bin=0; bin<gradientBinSize; bin++)  
  97.                 {  
  98.                 gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;  
  99.                 }  
  100.             }  
  101.         }  
  102.   
  103.   
  104.     cout << "descriptorDataIdx = " << descriptorDataIdx << endl;  
  105.   
  106.     // draw cells  
  107.     for (int celly=0; celly<cells_in_y_dir; celly++)  
  108.         {  
  109.         for (int cellx=0; cellx<cells_in_x_dir; cellx++)  
  110.             {  
  111.             int drawX = cellx * cellSize.width;  
  112.             int drawY = celly * cellSize.height;  
  113.   
  114.             int mx = drawX + cellSize.width/2;  
  115.             int my = drawY + cellSize.height/2;  
  116.   
  117.             rectangle(visual_image,  
  118.                 Point(drawX*scaleFactor,drawY*scaleFactor),  
  119.                 Point((drawX+cellSize.width)*scaleFactor,  
  120.                 (drawY+cellSize.height)*scaleFactor),  
  121.                 CV_RGB(100,100,100),  
  122.                 1);  
  123.   
  124.             // draw in each cell all 9 gradient strengths  
  125.             for (int bin=0; bin<gradientBinSize; bin++)  
  126.                 {  
  127.                 float currentGradStrength = gradientStrengths[celly][cellx][bin];  
  128.   
  129.                 // no line to draw?  
  130.                 if (currentGradStrength==0)  
  131.                     continue;  
  132.   
  133.                 float currRad = bin * radRangeForOneBin + radRangeForOneBin/2;  
  134.   
  135.                 float dirVecX = cos( currRad );  
  136.                 float dirVecY = sin( currRad );  
  137.                 float maxVecLen = cellSize.width/2;  
  138.                 float scale = viz_factor; // just a visual_imagealization scale,  
  139.                 // to see the lines better  
  140.   
  141.                 // compute line coordinates  
  142.                 float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;  
  143.                 float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;  
  144.                 float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;  
  145.                 float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;  
  146.   
  147.                 // draw gradient visual_imagealization  
  148.                 line(visual_image,  
  149.                     Point(x1*scaleFactor,y1*scaleFactor),  
  150.                     Point(x2*scaleFactor,y2*scaleFactor),  
  151.                     CV_RGB(0,0,255),  
  152.                     1);  
  153.   
  154.                 } // for (all bins)  
  155.   
  156.             } // for (cellx)  
  157.         } // for (celly)  
  158.   
  159.   
  160.     // don't forget to free memory allocated by helper data structures!  
  161.     for (int y=0; y<cells_in_y_dir; y++)  
  162.         {  
  163.         for (int x=0; x<cells_in_x_dir; x++)  
  164.             {  
  165.             delete[] gradientStrengths[y][x];              
  166.             }  
  167.         delete[] gradientStrengths[y];  
  168.         delete[] cellUpdateCounter[y];  
  169.         }  
  170.     delete[] gradientStrengths;  
  171.     delete[] cellUpdateCounter;  
  172.   
  173.     return visual_image;  
  174.   
  175.     }  
  176.   
  177.   
  178. int main()  
  179. {  
  180.       
  181.     HOGDescriptor hog;  
  182.     hog.winSize=Size(80,128);  
  183.     vector<float> des;  
  184.     Mat src = imread("d:/1.jpg");  
  185.     Mat dst ;  
  186.     resize(src,dst,Size(80,128));  
  187.     imshow("src",src);  
  188.     hog.compute(dst,des);  
  189.     Mat d = get_hogdescriptor_visual_image(dst,des,hog.winSize,hog.cellSize,3,2.0);  
  190.     imshow("dst",d);  
  191.     waitKey();  
  192.   
  193.      return 0;  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值