1 /*******************************************************************************
3 * clr2float_conv - Colorful image to float array converter desktop utility.
5 * Created for Eurobot 2010 competition.
7 * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
9 * Usage: ./clr2float_conv filenames... [-t value]
11 * Names of picture files to convert.
13 * Threshold. Value, which will be displayed to 0.0, while less values
14 * will display to negative floats >= -1.0 and greater to positive floats <= 1.0.
16 * Output: Files with the same names as the input files, but with '.bin' extension.
18 ******************************************************************************/
21 #include <opencv/highgui.h>
22 #include "clr2float.h"
25 /******************************************************************************/
27 //extension of output files
28 #define OUTPUT_EXT "bin"
30 /******************************************************************************/
32 /** Stores a filename with changed extension to dest.
33 * If filename contains a dot, the string after the last one is replaced by ext.
34 * If no dot is found, the ext is appended to the filename.
35 * @param destLen Max length of the result string.
36 * @return 0 if ok, (!0) in case of an error. */
37 int changeFilenameExt(const char *filename, const char *ext, char *dest, unsigned int destLen) {
38 if(!filename) return 1;
41 memcpy(dest, filename, (strlen(filename) < destLen ? strlen(filename) : destLen));
43 char *pch = strrchr(dest, '.'); // search for the last dot
44 if(!pch) pch = dest+strlen(dest);
45 if((unsigned)(pch-dest) > destLen-strlen(ext)-1) pch = dest+destLen-strlen(ext)-1;
47 strcpy(pch+1, ext); // change ext
51 /******************************************************************************/
53 /** Processes the given file and stores to a new one. */
54 int processFile(const char *filename, unsigned char threshold) {
55 FILE *pInput, *pOutput;
56 char destFilename[100];
57 IplImage *clrImage = NULL;
58 CvMat *outputMat = NULL;
60 int outputLen, writtenLen;
62 // store output filename to destFilename
63 changeFilenameExt(filename, OUTPUT_EXT, destFilename, sizeof(destFilename));
65 // open input and output files
66 pInput = fopen(filename, "r");
68 fprintf(stderr, "Error: File %s does not exist!\n", filename);
71 pOutput = fopen(destFilename, "w");
73 fprintf(stderr, "Error: Cannot write to file %s!\n", destFilename);
77 // load input image and generate a float <-1,1> format
78 clrImage = cvLoadImage(filename);
79 if(!clrImage) return 1;
80 if(!(outputLen=clr2float(clrImage, &outputMat, threshold, NULL))) {
81 cvReleaseImage(&clrImage);
85 // write the data to the file
86 output = outputMat->data.fl;
87 writtenLen = fwrite(output, sizeof(output[0]), outputLen, pOutput);
88 if(writtenLen != outputLen)
89 fprintf(stderr, "File \"%s\" processed, but an error occured when writting the output to \"%s\".\n", filename, destFilename);
91 fprintf(stderr, "File \"%s\" processed. Output successfully written to \"%s\".\n", filename, destFilename);
93 cvReleaseImage(&clrImage);
99 /******************************************************************************/
101 /** Processes the program arguments and for every input generates converted output. */
102 int main(int argc, char *argv[]) {
104 int thr=130; //some default value of threshold
106 // scan for program arguments
107 while ((opt = getopt(argc, argv, "t:")) != -1) {
110 if(optarg) thr = atoi(optarg); //threshold
114 // "undefined" program arguments = input filenames
115 for(int i = optind; i < argc; i++)
116 processFile(argv[i], thr);