Tom-Alexander/regression-js

Linear Regression for Color.

Closed this issue · 1 comments

Hi guys,

Can anyone please help me what is the correct way to use this library for Color Linear Regression.

I have two different rgb colors.
One is light grey:-
Expected RGB Values: [243, 243, 243]
Actual RGB Values: [233, 223, 231]

One is dark grey:-
Expected RGB Values: [85, 85, 85]
Actual RGB Values: [79, 77, 78]

I was using openCV in native iOS to get it working but now i moved to React Native and looking for this solution.

Here is the native code i used before to make it working:-

NSArray *patchColors = @[@[@233, @223, @231], @[@79, @77, @78]];
NSArray *greyColors = @[@[@243, @243, @243], @[@85, @85, @85]];

// Expected Log:-
a=3.948074, b=1.025974
a=1.671219, b=1.082192
a=4.450974, b=1.032680

// Get the Formula: y = slope*x + intercept

NSMutableArray * linearRegression(NSArray *patchColors, NSArray *greyColors) {
    
    NSUInteger count = [patchColors count]; // number of grey standards
    CvPoint * points=(CvPoint*)malloc( count * sizeof(points[0]));
    CvMat point_mat;
    
    NSMutableArray * result = [[NSMutableArray alloc] init];
    
    for(int channel=0; channel<3; channel++) { // 3 color channels
        for(int i=0; i<count; i++) {
            points[i].x = [ [ [patchColors objectAtIndex:i] objectAtIndex:channel] intValue];
            points[i].y = [ [ [greyColors objectAtIndex:i] objectAtIndex:channel] intValue];
        }
        float line[4];// to store the results
        point_mat = cvMat( 1, 2, CV_32SC2, points );
        cvFitLine(&point_mat,CV_DIST_L2 ,0,0.01,0.01, line);
        float slope = line[1]/line[0];
        float intercept = line[3] - slope*line[2];
        LinearPolynomial *formula = [[LinearPolynomial alloc] init];
        [formula setIntercept:intercept];
        [formula setSlope:slope];
        [result addObject:formula];
        printf("a=%f, b=%f \n", intercept, slope);
    }
    return result;

}

Thanks

This library is written in javascript, not objective-c. You will need to re-write the linear model.