PageRenderTime 127ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/process_test.cpp

http://marine-detection.googlecode.com/
C++ | 199 lines | 116 code | 50 blank | 33 comment | 11 complexity | a788ed5d94997306e972a9114f976ae9 MD5 | raw file
  1. #ifdef _win32
  2. #include <Windows.h>
  3. #endif
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "inttypes_custom.h"
  8. #ifdef _WIN32
  9. #include <winsock.h>
  10. #include <sys/timeb.h>
  11. #else
  12. #include <sys/time.h>
  13. #include <unistd.h>
  14. #endif
  15. //local image processing header
  16. extern "C"{
  17. #include "process_image.h"
  18. }
  19. //opencv headers
  20. #include "cv.h"
  21. #include "highgui.h"
  22. #ifdef _WIN32
  23. #include <sys/timeb.h>
  24. #include <sys/types.h>
  25. #include <winsock.h>
  26. void gettimeofday(struct timeval* t,void* timezone)
  27. {
  28. struct _timeb timebuffer;
  29. _ftime( &timebuffer );
  30. t->tv_sec=timebuffer.time;
  31. t->tv_usec=1000*timebuffer.millitm;
  32. }
  33. #endif
  34. #define ORIGINAL_IMAGES_DIR "images/original/"
  35. #define CMO_IMAGES_DIR "images/cmo/"
  36. #define PROCESSED_IMAGES_DIR "images/results/"
  37. #define FILE_NAME_PREFIX "DSC_im_"
  38. #define FILE_NAME_SUFFIX ".JPG"
  39. #define TEMP_BUFFER_SIZE 255
  40. //#define IMAGE_WIDTH 1024
  41. //#define// IMAGE_HEIGHT 768
  42. //#define IMAGE_SIZE (IMAGE_WIDTH*IMAGE_HEIGHT)
  43. int main(int argc, char **argv)
  44. {
  45. /* read in files from original images directory */
  46. FILE *in_file;
  47. FILE *out_file;
  48. char filename[TEMP_BUFFER_SIZE];
  49. char temp[TEMP_BUFFER_SIZE];
  50. char header[] = "P5\n1024 768\n255\n";
  51. timeval t1,t2;
  52. CvSize imgSize;
  53. char c;
  54. IplImage *tempBuf=0;
  55. IplImage *in_image;
  56. IplImage *processed_image;
  57. int nelems;
  58. double procTime, totalTime=0;
  59. int i=1;
  60. int ibyte2 = 0;
  61. //create a window to display images
  62. //cvNamedWindow("display",1);
  63. //allocate the image buffer
  64. //tempBuf=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
  65. //in_image = (uint8_t*)malloc(IMAGE_SIZE);
  66. //process_buffer = (uint8_t*)malloc(process_image_buffer_size(IMAGE_WIDTH, IMAGE_HEIGHT));
  67. //process_branches = (uint8_t*)calloc(1, 4*IMAGE_SIZE);
  68. do
  69. {
  70. filename[0] = '\0';
  71. sprintf(filename, "%s%s%u%s", ORIGINAL_IMAGES_DIR, FILE_NAME_PREFIX, i, FILE_NAME_SUFFIX);
  72. printf("Opening file %s\n", filename);
  73. in_file = fopen(filename, "rb");
  74. if (in_file <= 0)
  75. {
  76. printf("Error opening file %s\n", filename);
  77. break;
  78. }
  79. else
  80. {
  81. printf("Opened successfully\n", filename);
  82. //fgets(temp, TEMP_BUFFER_SIZE, in_file); // P5
  83. // read image data
  84. //nelems = fread(in_image, (size_t)1, (size_t)IMAGE_SIZE, in_file);
  85. tempBuf=cvLoadImage( filename, 1 );
  86. imgSize=cvGetSize(tempBuf);
  87. in_image = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
  88. cvCopy( tempBuf, in_image, NULL );
  89. processed_image = cvCreateImage(imgSize,IPL_DEPTH_8U,1);
  90. if (tempBuf == 0)
  91. {
  92. printf("Error reading image data\n");
  93. }
  94. else
  95. {
  96. //memcpy(tempBuf->imageData,in_image,sizeof(IMAGE_WIDTH*IMAGE_HEIGHT));
  97. // apply algorithm
  98. //t1 = timeGetTime();
  99. gettimeofday(&t1,NULL);
  100. process_image(in_image, processed_image);
  101. gettimeofday(&t2,NULL);
  102. //t2 = timeGetTime();
  103. procTime = (t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)/1000000.0;
  104. printf("Processing time: %f s\n", procTime);
  105. totalTime = totalTime + procTime;
  106. filename[0] = '\0';
  107. sprintf(filename, "%s%u%s", PROCESSED_IMAGES_DIR, i, FILE_NAME_SUFFIX);
  108. //write processed image data
  109. //out_file = fopen(filename, "wb");
  110. if (filename == NULL)
  111. {
  112. printf("Error opening file %s\n", filename);
  113. }
  114. else
  115. {
  116. //fprintf(out_file, "%s", header);
  117. nelems = cvSaveImage(filename,processed_image);
  118. if (nelems == 0)
  119. {
  120. printf("Error writing image data. Tried to write %u bytes, wrote %u bytes.\n", imgSize, nelems);
  121. }
  122. else
  123. {
  124. printf("Wrote %s successfully\n", filename);
  125. }
  126. //fclose(out_file);
  127. }
  128. c=cvWaitKey(10);
  129. if( (char)c == 27 )
  130. break;
  131. //for(ibyte2 = 0; ibyte2 < IMAGE_SIZE; ibyte2++){
  132. //tempBuf->imageData[(ibyte2)+0] = processed_image[ibyte2];
  133. //}
  134. //cvShowImage("display",in_image);
  135. }
  136. fclose(in_file);
  137. }
  138. i++;
  139. } while (in_file != NULL);
  140. //i-=2;
  141. printf("Processed %i images\n", i);
  142. printf("Avg processing time=%f s (excludes file I/O)\n", (float)totalTime/(float)i);
  143. printf("Avg FPS=%f (excludes file I/O)\n", 1000.0*(float)i/(float)totalTime);
  144. //free(in_image);
  145. //free(processed_image);
  146. //free(process_buffer);
  147. //free(process_branches);
  148. return 0;
  149. }