How to perform Image RGB Normalization in Android OpenCV?

I am trying to perform image normalization, using OpenCV 2.4.8 for Android. My test device is a samsung S3.

I have written a JNI function as shown below to perform Image Normalization.

My definition of Image Normalization is as follows :

r' is 255* r/(r+g+b)
g' is 255* g/(r+g+b)
b' is 255* b/(r+g+b)

Now I need to implement this. And I have written a function to perform this. This doesnt seem to work. I am confident of my other portions of my App (i.e. they are bug free). Only this part causes a problem.

I may have not written this function properly. Pls help.

I have also attached a screenshot of my result. If you look at this image, only half portion looks normalized (but its actually not normalized, it just looks that way) & the other half is not. To fix this, I tried changing the rows with cols in the for-loop, and the result was the same.

The code for this is also presented.

Looking forward to some help. (FYI - I am totally new to openCV). Thanks for understanding.

void NormalizeRGB(JNIEnv*, jobject, jlong img)
    Mat& imgM  = *(Mat*)img;
    vector<Mat> rgb;
    int cols = imgM.cols;
    int rows = imgM.rows;
    for(int i = 0; i<rows; i++)
        for(int j = 0; j<cols; j++)
            Vec3b bgrPixel =<Vec3b>(i, j);
            float total = bgrPixel.val[0] + bgrPixel.val[1] + bgrPixel.val[2] + 0.00001;
            bgrPixel.val[0] = (uchar) 255*bgrPixel.val[0]/total;
            bgrPixel.val[1] = (uchar) 255*bgrPixel.val[1]/total;
            bgrPixel.val[2] = (uchar) 255*bgrPixel.val[2]/total;
  <Vec3b>(i, j) = bgrPixel;


I found the solution to my problems.

I was trying to use this function to read an RGBA Matrix image. A RGBA Matrix image is of type Vec4.

I was trying to read it as a Vec3 ~ this used to cause the above problem.

With this small change, I was able to get my expected results.

Need Your Help

Fogbugz Duplicate Cases

project-management merge case fogbugz

I am using FogBugz free hosting to manage my project bugs, I also have several customers I create custom software for, been using FogBugz to keep everything organized.

insert new page between existing pages in pdf using matplotlib in python

python pdf matplotlib

Is it possible to insert a new page into an arbitrary position of a multi page pdf file?