/******************************************************************************/
-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;
}
/******************************************************************************/
-/** 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);
/******************************************************************************/