/code/artificial_intelligence/src/image_processing/houghtransform/main.cpp

https://github.com/OpenGenus/cosmos · C++ · 162 lines · 126 code · 19 blank · 17 comment · 17 complexity · 4c43203275c3dbebe796fb88db98aa3d MD5 · raw file

  1. #include <opencv/cv.h>
  2. #include <opencv/highgui.h>
  3. #include <opencv2/core/types_c.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #define WINDOW_NAME "Hough Transformation"
  7. #define THETA_GRANULARITY (4 * 100)
  8. #define RHO_GRANULARITY 1
  9. IplImage *img_read;
  10. IplImage *img_edges;
  11. IplImage *img_sine;
  12. int *img_sine_arr;
  13. int threshold;
  14. CvSize img_attrib_dim;
  15. CvSize img_attrib_sine;
  16. int max_rho;
  17. /*****************************************************************
  18. * create and destroy gui items
  19. *****************************************************************/
  20. void create_gui()
  21. {
  22. cvNamedWindow(WINDOW_NAME);
  23. max_rho = sqrt(
  24. img_attrib_dim.width * img_attrib_dim.width + img_attrib_dim.height *
  25. img_attrib_dim.height);
  26. printf("Image diagonal size (max rho): %d\n", max_rho);
  27. img_attrib_sine.width = THETA_GRANULARITY;
  28. img_attrib_sine.height = RHO_GRANULARITY * max_rho * 2;
  29. img_sine_arr = (int *)malloc(img_attrib_sine.width * img_attrib_sine.height * sizeof(int));
  30. img_edges = cvCreateImage(img_attrib_dim, IPL_DEPTH_8U, 1);
  31. img_sine = cvCreateImage(img_attrib_sine, IPL_DEPTH_8U, 1);
  32. cvZero(img_sine);
  33. }
  34. void destroy_gui()
  35. {
  36. cvDestroyWindow(WINDOW_NAME);
  37. cvReleaseImage(&img_read);
  38. cvReleaseImage(&img_edges);
  39. cvReleaseImage(&img_sine);
  40. }
  41. /*****************************************************************
  42. * display a message and wait for escape key to continue
  43. *****************************************************************/
  44. void disp_msg(const char *msg)
  45. {
  46. printf("%s. Press escape to continue.\n", msg);
  47. char c;
  48. do
  49. c = cvWaitKey(500);
  50. while (27 != c);
  51. }
  52. /*****************************************************************
  53. * main.
  54. * 1. read image
  55. * 2. detect edges
  56. * 3. hough transform to detect lines
  57. *****************************************************************/
  58. int main(int argc, char **argv)
  59. {
  60. if (argc < 3)
  61. {
  62. printf("Usage: %s <image> <threshold>\n", argv[0]);
  63. return 1;
  64. }
  65. img_read = cvLoadImage(argv[1]);
  66. if (NULL == img_read)
  67. {
  68. printf("Error loading image %s\n", argv[1]);
  69. return -1;
  70. }
  71. img_attrib_dim = cvGetSize(img_read);
  72. threshold = atoi(argv[2]);
  73. create_gui();
  74. cvShowImage(WINDOW_NAME, img_read);
  75. printf("Original image size: %d x %d\n", img_attrib_dim.width, img_attrib_dim.height);
  76. disp_msg("This is the original image");
  77. // detect edges
  78. cvCvtColor(img_read, img_edges, CV_BGR2GRAY);
  79. cvLaplace(img_edges, img_edges, 3);
  80. cvThreshold(img_edges, img_edges, 200, 255, CV_THRESH_BINARY);
  81. cvShowImage(WINDOW_NAME, img_edges);
  82. disp_msg("This is the edge detected image");
  83. printf("working...\n");
  84. // for each pixel in the edge
  85. int max_dest = 0;
  86. int min_dest = INT_MAX;
  87. for (int y = 0; y < img_edges->height; y++)
  88. {
  89. uchar *row_ptr = (uchar*) (img_edges->imageData + y * img_edges->widthStep);
  90. for (int x = 0; x < img_edges->width; x++)
  91. {
  92. if (*(row_ptr + x) > 0)
  93. for (int theta_div = 0; theta_div < THETA_GRANULARITY; theta_div++)
  94. {
  95. double theta = (CV_PI * theta_div / THETA_GRANULARITY) - (CV_PI / 2);
  96. int rho = (int)round(cos(theta) * x + sin(theta) * y + max_rho);
  97. int *dest = img_sine_arr + rho * img_attrib_sine.width + theta_div;
  98. int d_val = *dest = *dest + 1;
  99. if (d_val > max_dest)
  100. max_dest = d_val;
  101. if (d_val < min_dest)
  102. min_dest = d_val;
  103. }
  104. }
  105. }
  106. // scale the intensity for contrast
  107. float div = ((float)max_dest) / 255;
  108. printf("max_dest: %d, min_dest: %d, scale: %f\n", max_dest, min_dest, div);
  109. for (int y = 0; y < img_sine->height; y++)
  110. {
  111. uchar *row_ptr = (uchar*) (img_sine->imageData + y * img_sine->widthStep);
  112. for (int x = 0; x < img_sine->width; x++)
  113. {
  114. int val = (*((img_sine_arr + y * img_attrib_sine.width) + x)) / div;
  115. if (val < 0)
  116. val = 0;
  117. *(row_ptr + x) = val;
  118. }
  119. }
  120. // find rho and theta at maximum positions (within the threshold)
  121. int possible_edge = round((float)max_dest / div) - threshold;
  122. printf("possible edges beyond %d\n", possible_edge);
  123. for (int y = 0; y < img_sine->height; y++)
  124. {
  125. uchar *row_ptr = (uchar*) (img_sine->imageData + y * img_sine->widthStep);
  126. for (int x = 0; x < img_sine->width; x++)
  127. {
  128. int val = *(row_ptr + x);
  129. if (possible_edge <= val)
  130. {
  131. float theta_rad = (CV_PI * x / THETA_GRANULARITY) - (CV_PI / 2);
  132. float theta_deg = theta_rad * 180 / CV_PI;
  133. printf("val: %d at rho %d, theta %f (%f degrees)\n", val, y - max_rho, theta_rad,
  134. theta_deg);
  135. }
  136. }
  137. }
  138. // display the plotted intensity graph of the hough space
  139. cvShowImage(WINDOW_NAME, img_sine);
  140. disp_msg("this is the hough space image");
  141. destroy_gui();
  142. return 0;
  143. }