0

I have 256 binary matrices B(x,y) and I try to pool them in the group of 8 matrices. Considering a single pixel at a particular position (x,y), a set of binary values from matrices in the pool consisting of 8 matrices can be used to construct a binary code of 8 bits. The following formula illustrates that : see the formula

The pool of 256 matrices in the group of 8 matrices will result in 32 matrices. I wrote the code in java and the code run properly, but I want to reduce the time complexity, where it takes about 30 seconds to get the result! The size of each matrix is 300 X 100,what I can change or use to get the same result with much less time??

        //index-Strat
    int start = 0;
    //index-end
    int end = 7;
    //pool evey eghit matrices togather, and produces 32 matrices
    for (int j = 0; j < 32; j++)
    {
        //call the pooling function 
        Mat bit - 8 = new Mat();
        bit - 8 = pool(start, end);
        //add 8-bit matrix to pool array
        p.add(bit - 8);
        //increamt the indexs to pool the next 8 matrix 
        start = end + 1;
        end = start + 7;
    }

 //---------------------------
    Mat pool(int s, int e)

    {   
        Mat bit - 8 = new Mat(binary.get(0).size(), binary.get(0).type());
        //apply the Bit Plane 
        for (int i = 0; i < bit - 8. rows(); i++)
        {
            for (int j = 0; j < bit - 8. cols(); j++)
            {
                double[] sum = new double[1];
                for (int k = 0; k < 8; k++)
                {
                    double[] v = new double[1];
                    v = binary.get(s + k).get(i, j);
                    double new_value = v[0] * Math.pow(2, k);
                    sum[0] = sum[0] + new_value;
                }

                bit - 8. put(i, j, sum);
            }

            return bit - 8
        }

1 Answers1

1

I doubt that it fully explains your long computation time, but you can avoid a lot of object allocations in pool(int, int) by allocating the v and sum array outside the nested for loops, and using int[] instead:

int[] v = new int[1];
int[] sum = new int[1];
for (int i = 0; i < rows; ++i) {
  for (int j = 0; j < cols; ++j) {
    sum[0] = 0;
    for (int k = 0; k < 8; ++k) {
      binary.get(s + k).get(i, j, v);
      sum[0] += v[0] << k;
    }
    bitMinus8.put(i, j, sum);
  }
}

This reduces the number of allocated arrays from (rows*cols)*(8 + 2) to just 2.

Object creation is reasonably cheap, but avoiding repeated creation can speed things up.


Note that saying:

double[] v = new double[1];
v = <something>;

is redundant: you allocate a new array, and then immediately overwrite it, discarding the allocated array. You can simply write:

double[] v = <something>;

Looking at the docs, I believe that you can also avoid the explicit element-wise sum:

Mat bitMinus8 = new Mat(binary.get(0).size(), binary.get(0).type());
for (int k = 0; k < 8; ++k) {
  Core.addWeighted(binary.get(s + k), 1 << k, bitMinus8, 1, 0, bitMinus8);
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243