]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
clr2float - Colorful image to float array converter library.
authorPetr Kubiznak <kubizpet@fel.cvut.cz>
Sun, 4 Apr 2010 09:36:02 +0000 (11:36 +0200)
committerPetr Kubiznak <kubizpet@fel.cvut.cz>
Sun, 4 Apr 2010 09:36:02 +0000 (11:36 +0200)
Provides function to convert standard, colorful image (3D array of bytes) to a float representation of the same, pseudo-thresholded image (1D array of floats in range <-1,1>).

src/camera/rozkuk/clr2float.cxx [new file with mode: 0644]
src/camera/rozkuk/clr2float.h [new file with mode: 0644]

diff --git a/src/camera/rozkuk/clr2float.cxx b/src/camera/rozkuk/clr2float.cxx
new file mode 100644 (file)
index 0000000..c63a5e0
--- /dev/null
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * 
+ * 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;
+}
+
diff --git a/src/camera/rozkuk/clr2float.h b/src/camera/rozkuk/clr2float.h
new file mode 100644 (file)
index 0000000..9f8c90e
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * 
+ * 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__ */
+