0

I am trying to find the average of an array of images in android using OpenCV.

However the result is a completely white image.

public static Mat averageAll(Mat[] matList){
        int type = matList[0].type();
        Mat avg = Mat.zeros(matList[0].height(),matList[0].width(),CV_32FC4);
        Log.i("avg",""+avg.height()+" "+avg.width()+" "+avg.depth()+" "+avg.type());
        for(int i=0;i<matList.length;i++){
            Log.i(""+i,""+matList[i].height()+" "+matList[i].width()+" "+matList[i].depth()+" "+matList[i].type());
            matList[i].convertTo(matList[i],CV_32FC4);
            Core.add(matList[i],avg,avg);
        }
        Core.divide(avg,new Scalar(matList.length),avg);
        avg.convertTo(avg,type);
        return avg;
    }

This is how I create the Mats from bitmaps

//imagesList is an arrayList of bitmaps
Mat[] matList = new Mat[imagesList.size()]
for(int k = 0; k<imagesList.size();k++){
    Mat m = new Mat();
    Utils.bitmapToMat(imagesList.get(k),m);
    matList[k]=m;
}
Mat alignedM = ImageProcessor.averageAll(matList);
xSooDx
  • 493
  • 1
  • 5
  • 19
  • Without the type of your _Mat_ in _matList_ I can't say much. What do you expect the result to be? Please provide more information. – Rick M. Jun 08 '17 at 12:44
  • The type of the Mat list would be the same as the result of `Utils.bitmapToMap()` which I think is a CV_8U. Before computing, I covert them to a CV_32FC4. The images are RGB jpgs. The expected result is an image who's pixel values are the average of the corresponding pixels of each image in the matList. – xSooDx Jun 08 '17 at 14:45
  • If they are RGB, shouldn't they be **CV_8UC3** instead of **CV_32FC4**? – Rick M. Jun 08 '17 at 15:01
  • In that case there is a size mismatch error. – xSooDx Jun 08 '17 at 15:02
  • Yes, there should be. But I don't really get why you are converting the images, what is the point? – Rick M. Jun 08 '17 at 15:05
  • There is a size mismatch if I don't. I mainly need to find a correct type, such that there is no error, and the resulting values are not capped as a white color due to overflow. – xSooDx Jun 08 '17 at 15:07
  • You should ideally debug and check if the values are being added correctly or not. If the images are in **CV_8UC3** or **CV_8UC4** I would leave them in that and then check. Check [this](https://stackoverflow.com/questions/8377091/what-are-the-differences-between-cv-8u-and-cv-32f-and-what-should-i-worry-about) – Rick M. Jun 08 '17 at 15:17
  • They are but are getting capped at 255. – xSooDx Jun 08 '17 at 16:04
  • Try using `Imgproc.accumulate` instead. [cvAccumulate](http://docs.opencv.org/java/2.4.9/org/opencv/imgproc/Imgproc.html#accumulate) – Rick M. Jun 08 '17 at 16:13

1 Answers1

0

I found the problem.

The issue was with Core.divide(avg,new Scalar(matList.length),avg);. The Scalar class is actually a 4 element vector.

Converting it to

Core.divide(avg,new Scalar(matList.length,matList.length,matList.length,matList.length),avg);

gave the desired output.

xSooDx
  • 493
  • 1
  • 5
  • 19