/src/process_test.cpp
C++ | 199 lines | 116 code | 50 blank | 33 comment | 11 complexity | a788ed5d94997306e972a9114f976ae9 MD5 | raw file
- #ifdef _win32
- #include <Windows.h>
- #endif
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "inttypes_custom.h"
-
- #ifdef _WIN32
- #include <winsock.h>
- #include <sys/timeb.h>
- #else
- #include <sys/time.h>
- #include <unistd.h>
- #endif
-
- //local image processing header
- extern "C"{
- #include "process_image.h"
- }
- //opencv headers
- #include "cv.h"
- #include "highgui.h"
-
-
-
- #ifdef _WIN32
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <winsock.h>
-
- void gettimeofday(struct timeval* t,void* timezone)
- {
- struct _timeb timebuffer;
- _ftime( &timebuffer );
- t->tv_sec=timebuffer.time;
- t->tv_usec=1000*timebuffer.millitm;
- }
- #endif
-
- #define ORIGINAL_IMAGES_DIR "images/original/"
- #define CMO_IMAGES_DIR "images/cmo/"
- #define PROCESSED_IMAGES_DIR "images/results/"
- #define FILE_NAME_PREFIX "DSC_im_"
- #define FILE_NAME_SUFFIX ".JPG"
- #define TEMP_BUFFER_SIZE 255
- //#define IMAGE_WIDTH 1024
- //#define// IMAGE_HEIGHT 768
- //#define IMAGE_SIZE (IMAGE_WIDTH*IMAGE_HEIGHT)
-
-
-
- int main(int argc, char **argv)
- {
-
- /* read in files from original images directory */
- FILE *in_file;
- FILE *out_file;
- char filename[TEMP_BUFFER_SIZE];
- char temp[TEMP_BUFFER_SIZE];
- char header[] = "P5\n1024 768\n255\n";
- timeval t1,t2;
-
- CvSize imgSize;
- char c;
- IplImage *tempBuf=0;
- IplImage *in_image;
- IplImage *processed_image;
-
- int nelems;
- double procTime, totalTime=0;
- int i=1;
- int ibyte2 = 0;
-
-
- //create a window to display images
- //cvNamedWindow("display",1);
- //allocate the image buffer
-
-
- //tempBuf=cvCreateImage(imgSize,IPL_DEPTH_8U,1);
- //in_image = (uint8_t*)malloc(IMAGE_SIZE);
-
- //process_buffer = (uint8_t*)malloc(process_image_buffer_size(IMAGE_WIDTH, IMAGE_HEIGHT));
- //process_branches = (uint8_t*)calloc(1, 4*IMAGE_SIZE);
-
- do
- {
- filename[0] = '\0';
- sprintf(filename, "%s%s%u%s", ORIGINAL_IMAGES_DIR, FILE_NAME_PREFIX, i, FILE_NAME_SUFFIX);
-
- printf("Opening file %s\n", filename);
-
- in_file = fopen(filename, "rb");
-
- if (in_file <= 0)
- {
- printf("Error opening file %s\n", filename);
- break;
-
- }
- else
- {
- printf("Opened successfully\n", filename);
-
- //fgets(temp, TEMP_BUFFER_SIZE, in_file); // P5
- // read image data
- //nelems = fread(in_image, (size_t)1, (size_t)IMAGE_SIZE, in_file);
-
- tempBuf=cvLoadImage( filename, 1 );
- imgSize=cvGetSize(tempBuf);
- in_image = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
- cvCopy( tempBuf, in_image, NULL );
- processed_image = cvCreateImage(imgSize,IPL_DEPTH_8U,1);
-
-
- if (tempBuf == 0)
- {
- printf("Error reading image data\n");
- }
- else
- {
- //memcpy(tempBuf->imageData,in_image,sizeof(IMAGE_WIDTH*IMAGE_HEIGHT));
-
-
- // apply algorithm
-
- //t1 = timeGetTime();
- gettimeofday(&t1,NULL);
- process_image(in_image, processed_image);
- gettimeofday(&t2,NULL);
- //t2 = timeGetTime();
-
- procTime = (t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)/1000000.0;
- printf("Processing time: %f s\n", procTime);
- totalTime = totalTime + procTime;
-
-
- filename[0] = '\0';
- sprintf(filename, "%s%u%s", PROCESSED_IMAGES_DIR, i, FILE_NAME_SUFFIX);
-
- //write processed image data
- //out_file = fopen(filename, "wb");
- if (filename == NULL)
- {
- printf("Error opening file %s\n", filename);
- }
- else
- {
- //fprintf(out_file, "%s", header);
-
-
- nelems = cvSaveImage(filename,processed_image);
- if (nelems == 0)
- {
- printf("Error writing image data. Tried to write %u bytes, wrote %u bytes.\n", imgSize, nelems);
- }
- else
- {
- printf("Wrote %s successfully\n", filename);
- }
-
- //fclose(out_file);
- }
- c=cvWaitKey(10);
-
- if( (char)c == 27 )
- break;
-
- //for(ibyte2 = 0; ibyte2 < IMAGE_SIZE; ibyte2++){
-
- //tempBuf->imageData[(ibyte2)+0] = processed_image[ibyte2];
-
- //}
- //cvShowImage("display",in_image);
-
-
- }
-
- fclose(in_file);
-
- }
- i++;
-
- } while (in_file != NULL);
-
- //i-=2;
- printf("Processed %i images\n", i);
- printf("Avg processing time=%f s (excludes file I/O)\n", (float)totalTime/(float)i);
- printf("Avg FPS=%f (excludes file I/O)\n", 1000.0*(float)i/(float)totalTime);
-
- //free(in_image);
- //free(processed_image);
- //free(process_buffer);
- //free(process_branches);
-
- return 0;
- }
-