PageRenderTime 217ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/test/opencv/hog_test.cpp

https://gitlab.com/B3h3m0th/ccv
C++ | 106 lines | 84 code | 6 blank | 16 comment | 10 complexity | 6408b9891fa9eddab623a76b03b5dc5a MD5 | raw file
  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. unsigned int get_current_time()
  7. {
  8. struct timeval tv;
  9. gettimeofday(&tv, NULL);
  10. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  11. }
  12. #define HOG_BORDER_SIZE (2)
  13. static void icvCreateHOG( CvMat* img, int* i32c8 )
  14. {
  15. CvMat* dx = cvCreateMat( img->rows, img->cols, CV_32FC1 );
  16. CvMat* dy = cvCreateMat( img->rows, img->cols, CV_32FC1 );
  17. CvMat* angle = cvCreateMat( img->rows, img->cols, CV_32FC1 );
  18. CvMat* magnitude = cvCreateMat( img->rows, img->cols, CV_32FC1 );
  19. CvMat* bin = cvCreateMat( img->rows, img->cols, CV_32SC1 );
  20. cvSobel( img, dx, 1, 0, 1 );
  21. cvSobel( img, dy, 0, 1, 1 );
  22. cvCartToPolar( dx, dy, magnitude, angle, 1 );
  23. cvConvertScale( angle, bin, 8.0 / 360.0, -0.5 );
  24. int &rows = img->rows, &cols = img->cols;//, &step = img->step;
  25. int x, y, i, j;
  26. float* magptr = magnitude->data.fl + HOG_BORDER_SIZE + HOG_BORDER_SIZE * cols;
  27. int* binptr = bin->data.i + HOG_BORDER_SIZE + HOG_BORDER_SIZE * cols;
  28. int* iptr = i32c8;
  29. // uchar* imgptr = img->data.ptr + step * HOG_BORDER_SIZE + HOG_BORDER_SIZE;
  30. for ( y = HOG_BORDER_SIZE; y < rows - HOG_BORDER_SIZE; y++ )
  31. {
  32. float hog[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  33. for ( i = -HOG_BORDER_SIZE; i <= HOG_BORDER_SIZE; i++ )
  34. for ( j = -HOG_BORDER_SIZE; j <= HOG_BORDER_SIZE; j++ )
  35. {
  36. int k = i * cols + j;
  37. hog[binptr[k]] += magptr[k];
  38. }
  39. for ( i = 0; i < 8; ++i )
  40. iptr[i] = (int) hog[i];
  41. // memcpy( fptr, hog, 4 * 8 );
  42. /*
  43. for ( i = 0; i < 8; ++i )
  44. fptr[i] = *imgptr;
  45. fptr[8] = *imgptr;
  46. ++imgptr;
  47. */
  48. iptr += 8;
  49. ++binptr;
  50. ++magptr;
  51. for ( x = HOG_BORDER_SIZE + 1; x < cols - HOG_BORDER_SIZE; x++ )
  52. {
  53. for ( i = -HOG_BORDER_SIZE; i <= HOG_BORDER_SIZE; i++ )
  54. {
  55. int k = i * cols - HOG_BORDER_SIZE - 1;
  56. hog[binptr[k]] -= magptr[k];
  57. hog[binptr[k + HOG_BORDER_SIZE * 2 + 1]] += magptr[k + HOG_BORDER_SIZE * 2 + 1];
  58. }
  59. for ( i = 0; i < 8; ++i )
  60. iptr[i] = (int) hog[i];
  61. // memcpy( fptr, hog, 4 * 8 );
  62. /*
  63. for ( i = 0; i < 8; ++i )
  64. fptr[i] = *imgptr;
  65. fptr[8] = *imgptr;
  66. ++imgptr;
  67. */
  68. iptr += 8;
  69. ++binptr;
  70. ++magptr;
  71. }
  72. binptr += HOG_BORDER_SIZE * 2;
  73. magptr += HOG_BORDER_SIZE * 2;
  74. // imgptr += step - cols + HOG_BORDER_SIZE * 2;
  75. }
  76. cvReleaseMat( &bin );
  77. cvReleaseMat( &magnitude );
  78. cvReleaseMat( &angle );
  79. cvReleaseMat( &dx );
  80. cvReleaseMat( &dy );
  81. }
  82. int main(int argc, char** argv)
  83. {
  84. assert(argc == 3);
  85. IplImage* image = cvLoadImage(argv[1]);
  86. CvMat* gray = cvCreateMat(image->height, image->width, CV_8UC1);
  87. CvMat* x = cvCreateMat(image->height - 4, image->width - 4, CV_32SC1);
  88. cvCvtColor(image, gray, CV_BGR2GRAY);
  89. int* hog = (int*)malloc(sizeof(int) * (image->width - 4) * (image->height - 4) * 8);
  90. unsigned int elapsed_time = get_current_time();
  91. icvCreateHOG(gray, hog);
  92. printf("elapsed time : %d\n", get_current_time() - elapsed_time);
  93. for (int i = 0; i < x->rows; i++)
  94. for (int j = 0; j < x->cols; j++)
  95. x->data.i[i * x->cols + j] = hog[i * x->cols * 8 + j * 8];
  96. cvSaveImage(argv[2], x);
  97. cvReleaseMat(&gray);
  98. cvReleaseImage(&image);
  99. return 0;
  100. }