]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
Process all the data as OpenCV matrices instead of float arrays.
authorPetr Kubiznak <kubizpet@fel.cvut.cz>
Fri, 16 Apr 2010 20:54:35 +0000 (22:54 +0200)
committerPetr Kubiznak <kubizpet@fel.cvut.cz>
Fri, 16 Apr 2010 20:54:35 +0000 (22:54 +0200)
src/camera/rozkuk/clr2float.cxx
src/camera/rozkuk/clr2float.h

index c63a5e055beea803aa18f6412c63b101045fc10f..59b791a8502cfa6f688afec5621e66c4f75d69a4 100644 (file)
@@ -20,56 +20,59 @@ extern "C" {
 
 /******************************************************************************/
 
-float transformTable[TABLE_LEN];                       //look-up table
+int initTable(unsigned char threshold, float k=1.0);
+
+/******************************************************************************/
+
+CvMat *lut = NULL;
 unsigned char currentThr = -1;
+float currentK = 1.0;
 
 /******************************************************************************/
 
 /** 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.
+ * @param k Saturation - if k>1.0, differences between positive and negative values grow.
  * @return 0 when table already initialized, 1 if ok */
-int initTable(unsigned char threshold) {
+int initTable(unsigned char threshold, float k) {
        float divisor;  
-       if(threshold == currentThr) return 0;
+       if((threshold == currentThr) && (k == currentK)) return 0;
        
-       divisor = (float)(threshold > 255-threshold ? threshold : 255-threshold);
+       divisor = (float)(threshold > 255-threshold ? threshold : 255-threshold)/k;
        
+       if(!lut) lut=cvCreateMat(1,TABLE_LEN,CV_32FC1);
        for(int i=0; i<TABLE_LEN; i++) {
-               transformTable[i] = (i-threshold) / divisor;
+               cvmSet(lut,0,i,(i-threshold)/divisor); // Set M(i,j)
        }
        return 1;
 }
 
 /******************************************************************************/
 
-/** Transforms a colorful image to its float (in <-1,1>) representation.
+/** Transforms a colorful image to its float representation.
  * @param src Image in BGR format.
- * @param dest Pointer to a destination array.
+ * @param dest Pointer to a destination matrix.
  * @param threshold Level of gray which will transform to zero.
+ * @param k Saturation - if k>1.0, differences between positive and negative values grow.
  * @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) {
+int clr2float(const IplImage *src, CvMat **dest, unsigned char threshold, float k, IplImage *thresholded) {
        IplImage *gray;
-       int len;
-       unsigned char *srcArr;
        
        if(!src) return 0;
-       if(initTable(threshold) < 0) return 0;
+       if(initTable(threshold, k) < 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;
+       if(*dest) cvReleaseMat(dest);
+       *dest = cvCreateMat(src->height,src->width,CV_32FC1);
+       //apply transformation
+       cvLUT(gray, *dest, lut);
+       
+       cvReleaseImage(&gray);
+       return src->width * src->height;
 }
 
index a058497b6a1827407b38831c8b29e837bcfbd809..28aa2c0aaa3c07379be9858b2f20de8cdd88aa02 100644 (file)
 
 /******************************************************************************/
 
-/** Transforms a colorful image to its float (in <-1,1>) representation.
+/** Transforms a colorful image to its float representation.
  * @param src Image in BGR format.
- * @param dest Pointer to a destination array.
+ * @param dest Pointer to a destination matrix.
  * @param threshold Level of gray which will transform to zero.
+ * @param k Saturation - if k>1.0, differences between positive and negative values grow.
  * @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);
+int clr2float(const IplImage *src, CvMat **dest, unsigned char threshold, float k=1.0, IplImage *thresholded=NULL);
 
 /******************************************************************************/