void PerformSuperpixelSLIC(
            double[] kseedsl,
            double[] kseedsa,
            double[] kseedsb,
            double[] kseedsx,
            double[] kseedsy,
            int[] klabels,
            int STEP,
            @SuppressWarnings("unused") double[] edgemag,
            double M) {
        int sz = m_width * m_height;
        int numk = kseedsl.length;
        //----------------
        int offset = STEP;
        //----------------

        double[] clustersize = new double[numk];
        double[] inv = new double[numk];//to store 1/clustersize[k] values

        double[] sigmal = new double[numk];
        double[] sigmaa = new double[numk];
        double[] sigmab = new double[numk];
        double[] sigmax = new double[numk];
        double[] sigmay = new double[numk];
        double[] distvec = new double[sz];

        for (int i_ = 0; i_ < numk; i_++) {
            clustersize[i_] = 0;
            inv[i_] = 0;
            sigmal[i_] = 0;
            sigmaa[i_] = 0;
            sigmab[i_] = 0;
            sigmax[i_] = 0;
            sigmay[i_] = 0;

        }
        for (int i_ = 0; i_ < distvec.length; i_++) {
            distvec[i_] = Double.MAX_VALUE;
        }

        double invwt = 1.0 / ((STEP / M) * (STEP / M));

        int x1, y1, x2, y2;
        double l, a, b;
        double dist;
        double distxy;
        for (int itr=0; itr<10; itr++) {
            //distvec.assign(sz, DBL_MAX);
            for (int i_ = 0; i_ < distvec.length; i_++) {
                distvec[i_] = Double.MAX_VALUE;
            }
            for (int n=0; n<numk; n++) {
                y1 = (int) Math.max(0, kseedsy[n] - offset);
                y2 = (int) Math.min(m_height, kseedsy[n] + offset);
                x1 = (int) Math.max(0, kseedsx[n] - offset);
                x2 = (int) Math.min(m_width, kseedsx[n] + offset);

                for (int y = y1; y < y2; y++) {
                    for (int x = x1; x < x2; x++) {                
                        int i = y * m_width + x;
                        
                        l = m_lvec[i];
                        a = m_avec[i];
                        b = m_bvec[i];

                        dist =    (l - kseedsl[n]) * (l - kseedsl[n])
                                + (a - kseedsa[n]) * (a - kseedsa[n])
                                + (b - kseedsb[n]) * (b - kseedsb[n]);

                        distxy =  (x - kseedsx[n]) * (x - kseedsx[n])
                                + (y - kseedsy[n]) * (y - kseedsy[n]);

                        //------------------------------------------------------------------------
                        dist += distxy * invwt;//dist = sqrt(dist) + sqrt(distxy*invwt);//this is more exact
                        //------------------------------------------------------------------------

                        if (dist < distvec[i]) {
                            distvec[i] = dist;
                            klabels[i] = n;
                        }
                    }
                }
            }
            //-----------------------------------------------------------------
            // Recalculate the centroid and store in the seed values
            //-----------------------------------------------------------------
            //instead of reassigning memory on each iteration, just reset.

            for (int i_ = 0; i_ < numk; i_++) {
                clustersize[i_] = 0;
                sigmal[i_] = 0;
                sigmaa[i_] = 0;
                sigmab[i_] = 0;
                sigmax[i_] = 0;
                sigmay[i_] = 0;
            }

            //------------------------------------
            //edgesum.assign(numk, 0);
            //------------------------------------
            
            int ind = 0;
            for (int r = 0; r < m_height; r++) {
                for (int c = 0; c < m_width; c++) {
                    sigmal[klabels[ind]] += m_lvec[ind];
                    sigmaa[klabels[ind]] += m_avec[ind];
                    sigmab[klabels[ind]] += m_bvec[ind];
                    sigmax[klabels[ind]] += c;
                    sigmay[klabels[ind]] += r;
                    //------------------------------------
                    //edgesum[klabels[ind]] += edgemag[ind];
                    //------------------------------------
                    clustersize[klabels[ind]] += 1.0;
                    ind++;
                }
            }            

            for (int k = 0; k < numk; k++) {
                if (clustersize[k] <= 0) {
                    clustersize[k] = 1;
                }
                inv[k] = 1.0 / clustersize[k];//computing inverse now to multiply, than divide later
            }
            
            for (int k = 0; k < numk; k++) {
                kseedsl[k] = sigmal[k] * inv[k];
                kseedsa[k] = sigmaa[k] * inv[k];
                kseedsb[k] = sigmab[k] * inv[k];
                kseedsx[k] = sigmax[k] * inv[k];
                kseedsy[k] = sigmay[k] * inv[k];
                //------------------------------------
                //edgesum[k] *= inv[k];
                //------------------------------------
            }            
        }
    }