--- /dev/null
+/*******************************************************************************
+ *
+ * clr2float - Colorful image to float array converter library.
+ *
+ * Created for Eurobot 2010 competition.
+ *
+ * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
+ *
+ ******************************************************************************/
+
+extern "C" {
+#include <stdlib.h>
+#include <stdio.h>
+}
+#include "clr2float.h"
+
+/******************************************************************************/
+
+#define TABLE_LEN 256
+
+/******************************************************************************/
+
+float transformTable[TABLE_LEN]; //look-up table
+unsigned char currentThr = -1;
+
+/******************************************************************************/
+
+/** Initializes the transform table.
+ * It can then be used to transform from {0..255} to <-1,1>.
+ * @param threshold Value which will transform to zero.
+ * @return 0 when table already initialized, 1 if ok */
+int initTable(unsigned char threshold) {
+ float divisor;
+ if(threshold == currentThr) return 0;
+
+ divisor = (float)(threshold > 255-threshold ? threshold : 255-threshold);
+
+ for(int i=0; i<TABLE_LEN; i++) {
+ transformTable[i] = (i-threshold) / divisor;
+ }
+ return 1;
+}
+
+/******************************************************************************/
+
+/** Transforms a colorful image to its float (in <-1,1>) representation.
+ * @param src Image in BGR format.
+ * @param dest Pointer to a destination array.
+ * @param threshold Level of gray which will transform to zero.
+ * @param thresholded If not NULL, BW thresholded version of src is stored here.
+ * @return Length of the destination data, or 0 if an error occurs. */
+int clr2float(const IplImage *src, float **dest, unsigned char threshold, IplImage *thresholded) {
+ IplImage *gray;
+ int len;
+ unsigned char *srcArr;
+
+ if(!src) return 0;
+ if(initTable(threshold) < 0) return 0;
+
+ gray = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1); // create new 1-channel 1-byte image
+ cvCvtColor(src, gray, CV_BGR2GRAY); // convert to grayscale
+ if(thresholded) cvThreshold(gray, thresholded, threshold, 255, CV_THRESH_BINARY); // threshold (if wanted)
+
+ len = src->width * src->height;
+ srcArr = (unsigned char *)src->imageData;
+ if(*dest) free(*dest);
+ *dest = (float *)malloc(len * sizeof(float));
+
+ for(int i=0; i<len; i++) {
+ (*dest)[i] = transformTable[srcArr[i]];
+ }
+
+ return len;
+}
+
--- /dev/null
+/*******************************************************************************
+ *
+ * clr2float - Colorful image to float array converter library.
+ *
+ * Created for Eurobot 2010 competition.
+ *
+ * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
+ *
+ ******************************************************************************/
+
+#ifndef __CLR_2_FLOAT_H__
+#define __CLR_2_FLOAT_H__
+
+#include <cv.h>
+
+/******************************************************************************/
+
+/** Transforms a colorful image to its float (in <-1,1>) representation.
+ * @param src Image in BGR format.
+ * @param dest Pointer to a destination array.
+ * @param threshold Level of gray which will transform to zero.
+ * @param thresholded If not NULL, BW thresholded version of src is stored here.
+ * @return Length of the destination data, or 0 if an error occurs. */
+int clr2float(const IplImage *src, float **dest, unsigned char threshold, IplImage *thresholded=NULL);
+
+/******************************************************************************/
+
+#endif /* __CLR_2_FLOAT_H__ */
+