Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <cmath>
  5.  
  6. #include <opencv2/opencv.hpp>
  7.  
  8. #include <proj_api.h>
  9.  
  10. #include "defines.h"
  11. #include "utils.h"
  12.  
  13. #define STEP1_WIN_NAME "Heightmap"
  14. #define STEP2_WIN_NAME "Edges"
  15. #define ZOOM 1
  16.  
  17.  
  18. struct MouseProbe {
  19. cv::Mat & heightmap_8uc1_img_;
  20. cv::Mat & heightmap_show_8uc3_img_;
  21. cv::Mat & edgemap_8uc1_img_;
  22.  
  23. MouseProbe( cv::Mat & heightmap_8uc1_img, cv::Mat & heightmap_show_8uc3_img, cv::Mat & edgemap_8uc1_img )
  24. : heightmap_8uc1_img_( heightmap_8uc1_img ), heightmap_show_8uc3_img_( heightmap_show_8uc3_img ), edgemap_8uc1_img_( edgemap_8uc1_img )
  25. {
  26. }
  27. };
  28.  
  29.  
  30. // variables
  31.  
  32. // function declarations
  33. void flood_fill( cv::Mat & src_img, cv::Mat & dst_img, const int x, const int y );
  34.  
  35.  
  36. /**
  37. * Mouse clicking callback.
  38. */
  39. void mouse_probe_handler( int event, int x, int y, int flags, void* param ) {
  40. MouseProbe *probe = (MouseProbe*)param;
  41.  
  42. switch ( event ) {
  43.  
  44. case CV_EVENT_LBUTTONDOWN:
  45. printf( "Clicked LEFT at: [ %d, %d ]\n", x, y );
  46. flood_fill( probe->edgemap_8uc1_img_, probe->heightmap_show_8uc3_img_, x, y );
  47. break;
  48.  
  49. case CV_EVENT_RBUTTONDOWN:
  50. printf( "Clicked RIGHT at: [ %d, %d ]\n", x, y );
  51. break;
  52. }
  53. }
  54.  
  55.  
  56. void create_windows( const int width, const int height ) {
  57. cv::namedWindow( STEP1_WIN_NAME, 0 );
  58. cv::namedWindow( STEP2_WIN_NAME, 0 );
  59.  
  60. cv::resizeWindow( STEP1_WIN_NAME, width*ZOOM, height*ZOOM );
  61. cv::resizeWindow( STEP2_WIN_NAME, width*ZOOM, height*ZOOM );
  62.  
  63. } // create_windows
  64.  
  65.  
  66. /**
  67. * Perform flood fill from the specified point (x, y) for the neighborhood points if they contain the same value,
  68. * as the one that came in argument 'value'. Function recursicely call itself for its 4-neighborhood.
  69. *
  70. * edgemap_8uc1_img - image, in which we perform flood filling
  71. * heightmap_show_8uc3_img - image, in which we display the filling
  72. * value - value, for which we'll perform flood filling
  73. */
  74. void fill_step( cv::Mat & edgemap_8uc1_img, cv::Mat & heightmap_show_8uc3_img, const int x, const int y, const uchar value ) {
  75. int width, height;
  76.  
  77. } //fill_step
  78.  
  79.  
  80. /**
  81. * Perform flood fill from the specified point (x, y). The function remembers the value at the coordinate (x, y)
  82. * and fill the neighborhood using 'fill_step' function so long as the value in the neighborhood points are the same.
  83. * Execute the fill on a temporary image to prevent the original image from being repainted.
  84.  
  85. * edgemap_8uc1_img - image, in which we perform flood filling
  86. * heightmap_show_8uc3_img - image, in which we display the filling
  87. */
  88. void flood_fill( cv::Mat & edgemap_8uc1_img, cv::Mat & heightmap_show_8uc3_img, const int x, const int y ) {
  89. cv::Mat tmp_edgemap_8uc1_img;
  90.  
  91. } //flood_fill
  92.  
  93.  
  94. /**
  95. * Find the minimum and maximum coordinates in the file.
  96.  * Note that the file is the S-JTSK coordinate system.
  97. */
  98. void get_min_max( const char *filename, float *a_min_x, float *a_max_x, float *a_min_y, float *a_max_y, float *a_min_z, float *a_max_z ) {
  99. FILE *f = NULL;
  100. float x, y, z;
  101. float min_x, min_y, min_z, max_x, max_y, max_z;
  102. int l_type;
  103.  
  104. f = fopen (filename,"r");
  105. if (f != NULL)
  106. {
  107. printf("fopen ok \n");
  108. bool first = true;
  109. while(!feof(f)) {
  110. fread(&x, sizeof(float), 1, f);
  111. fread(&y, sizeof(float), 1, f);
  112. fread(&z, sizeof(float), 1, f);
  113. fread(&l_type, sizeof(int), 1, f);
  114.  
  115. //printf("Line: %.2f, %.2f, %.2f - %d \n", x,y,z,l_type);
  116.  
  117. if (first) {
  118. min_x = x;
  119. min_y = y;
  120. min_z = z;
  121.  
  122. max_x = x;
  123. max_y = y;
  124. max_z = z;
  125.  
  126. first = false;
  127. continue;
  128. }
  129.  
  130. if (x < min_x) {
  131. min_x = x;
  132. } else if (x > max_x) {
  133. max_x = x;
  134. }
  135.  
  136.  
  137.  
  138. if (y < min_y) {
  139. min_y = y;
  140. } else if (y > max_y) {
  141. max_y = y;
  142. }
  143.  
  144. if (z < min_z) {
  145. min_z = z;
  146. } else if (z > max_z) {
  147. max_z = z;
  148. }
  149. }
  150.  
  151. *a_min_x = min_x;
  152. *a_min_y = min_y;
  153. *a_min_z = min_z;
  154.  
  155. *a_max_x = max_x;
  156. *a_max_y = max_y;
  157. *a_max_z = max_z;
  158.  
  159.  
  160. fclose (f);
  161. }
  162.  
  163. }
  164.  
  165.  
  166. /**
  167. * Fill the image by data from lidar.
  168. * All lidar points are stored in a an array that has the dimensions of the image. Then the pixel is assigned
  169. * a value as an average value range from at the corresponding array element. However, with this simple data access, you will lose data precission.
  170. * filename - file with binarny data
  171. * img - input image
  172. */
  173. void fill_image( const char *filename, cv::Mat & heightmap_8uc1_img, float min_x, float max_x, float min_y, float max_y, float min_z, float max_z ) {
  174. FILE *f = NULL;
  175. int delta_x, delta_y, delta_z;
  176. float fx, fy, fz;
  177. int x, y, l_type;
  178. int stride;
  179. int num_points = 1;
  180. float range = 0.0f;
  181. float *sum_height = NULL;
  182. int *sum_height_count = NULL;
  183.  
  184. // zjistime sirku a vysku obrazu
  185. delta_x = round( max_x - min_x + 0.5f );
  186. delta_y = round( max_y - min_y + 0.5f );
  187. delta_z = round( max_z - min_z + 0.5f );
  188.  
  189. stride = delta_x;
  190.  
  191. // 1:
  192. // We allocate helper arrays, in which we store values from the lidar
  193. // and the number of these values for each pixel
  194.  
  195. // 2:
  196. // go through the file and assign values to the field
  197. // beware that in S-JTSK the beginning of the co-ordinate system is at the bottom left,
  198. // while in the picture it is top left
  199.  
  200. // 3:
  201. // assign values from the helper field into the image
  202.  
  203. }
  204.  
  205.  
  206. void make_edges( const cv::Mat & src_8uc1_img, cv::Mat & edgemap_8uc1_img ) {
  207. cv::Canny( src_8uc1_img, edgemap_8uc1_img, 1, 80 );
  208. }
  209.  
  210.  
  211. /**
  212. * Transforms the image so it contains only two values.
  213. * Threshold may be set experimentally.
  214. */
  215. void binarize_image( cv::Mat & src_8uc1_img ) {
  216. int x, y;
  217. uchar value;
  218.  
  219. }
  220.  
  221.  
  222. void dilate_and_erode_edgemap( cv::Mat & edgemap_8uc1_img ) {
  223. }
  224.  
  225.  
  226. void process_lidar( const char *txt_filename, const char *bin_filename, const char *img_filename ) {
  227. float min_x, max_x, min_y, max_y, min_z, max_z;
  228. float delta_x, delta_y, delta_z;
  229. MouseProbe *mouse_probe;
  230.  
  231. cv::Mat heightmap_8uc1_img; // image of source of lidar data
  232. cv::Mat heightmap_show_8uc3_img; // image to detected areas
  233. cv::Mat edgemap_8uc1_img; // image for edges
  234.  
  235. get_min_max( bin_filename, &min_x, &max_x, &min_y, &max_y, &min_z, &max_z );
  236.  
  237. printf( "min x: %f, max x: %f\n", min_x, max_x );
  238. printf( "min y: %f, max y: %f\n", min_y, max_y );
  239. printf( "min z: %f, max z: %f\n", min_z, max_z );
  240.  
  241. delta_x = max_x - min_x;
  242. delta_y = max_y - min_y;
  243. delta_z = max_z - min_z;
  244.  
  245. printf( "delta x: %f\n", delta_x );
  246. printf( "delta y: %f\n", delta_y );
  247. printf( "delta z: %f\n", delta_z );
  248.  
  249. // create images according to data from the source file
  250. /*
  251. heightmap_8uc1_img = cv::Mat( cvSize( cvRound( delta_x + 0.5f ), cvRound( delta_y + 0.5f ) ), CV_8UC1 );
  252. heightmap_show_8uc3_img = cv::Mat( cvSize( cvRound( delta_x + 0.5f ), cvRound( delta_y + 0.5f ) ), CV_8UC3 );
  253. edgemap_8uc1_img = cv::Mat( cvSize( cvRound( delta_x + 0.5f ), cvRound( delta_y + 0.5f ) ), CV_8UC3 );
  254.  
  255. create_windows( heightmap_8uc1_img.cols, heightmap_8uc1_img.rows );
  256. mouse_probe = new MouseProbe( heightmap_8uc1_img, heightmap_show_8uc3_img, edgemap_8uc1_img );
  257.  
  258. cv::setMouseCallback( STEP1_WIN_NAME, mouse_probe_handler, mouse_probe );
  259. cv::setMouseCallback( STEP2_WIN_NAME, mouse_probe_handler, mouse_probe );
  260.  
  261. printf( "Image w=%d, h=%d\n", heightmap_8uc1_img.cols, heightmap_8uc1_img.rows );
  262. */
  263.  
  264. // fill the image with data from lidar scanning
  265. //fill_image( bin_filename, heightmap_8uc1_img, min_x, max_x, min_y, max_y, min_z, max_z );
  266. //cv::cvtColor( heightmap_8uc1_img, heightmap_show_8uc3_img, CV_GRAY2RGB );
  267.  
  268. // create edge map from the height image
  269. //make_edges( heightmap_8uc1_img, edgemap_8uc1_img );
  270.  
  271. // binarize image, so we can easily process it in the next step
  272. //binarize_image( edgemap_8uc1_img );
  273.  
  274. // implement image dilatation and erosion
  275. //dilate_and_erode_edgemap( edgemap_8uc1_img );
  276.  
  277. //cv::imwrite( img_filename, heightmap_8uc1_img );
  278.  
  279. // wait here for user input using (mouse clicking)
  280. /*
  281. while ( 1 ) {
  282. cv::imshow( STEP1_WIN_NAME, heightmap_show_8uc3_img );
  283. //cv::imshow( STEP2_WIN_NAME, edgemap_8uc1_img );
  284. int key = cv::waitKey( 10 );
  285. if ( key == 'q' ) {
  286. break;
  287. }
  288. }
  289. */
  290. }
  291.  
  292.  
  293. int main( int argc, char *argv[] ) {
  294. char *txt_file, *bin_file, *img_file;
  295.  
  296. if ( argc < 4 ) {
  297. printf( "Not enough command line parameters.\n" );
  298. exit( 1 );
  299. }
  300.  
  301. txt_file = argv[ 1 ];
  302. bin_file = argv[ 2 ];
  303. img_file = argv[ 3 ];
  304.  
  305. process_lidar( txt_file, bin_file, img_file );
  306.  
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement