private int EnforceLabelConnectivity(
            int[] labels, // input label that needs to be corrected to remove stray labels
            int width,
            int height,
            int[] nlabels,// new labels
            int numlabels,// the number of labels changes in the end if segments are removed
            int K) // The number of superpixel desired by the user 
    	{
        int sz = width * height;
        for (int i=0; i<sz; i++) {
           nlabels[i] = -1;
        }

        int SUPSZ = sz / K;
        //------------------
        // labeling
        //------------------
        int lab = 0;
        int i = 0;
        int adjlabel = 0;//adjacent label
        int[] xvec = new int[sz];//worst case size
        int[] yvec = new int[sz];//worst case size
        int[] count = new int[1];
        {
            for (int h = 0; h < height; h++) {
                for (int w = 0; w < width; w++) {
                    if (nlabels[i] < 0) {
                        nlabels[i] = lab;
                        //-------------------------------------------------------
                        // Quickly find an adjacent label for use later if needed
                        //-------------------------------------------------------
                        {
                            for (int n = 0; n < 4; n++) {
                                int x = w + dx4[n];
                                int y = h + dy4[n];
                                if ((x >= 0 && x < width) && (y >= 0 && y < height)) {
                                    int nindex = y * width + x;
                                    if (nlabels[nindex] >= 0) {
                                        adjlabel = nlabels[nindex];
                                    }
                                }
                            }
                        }
                        xvec[0] = w;
                        yvec[0] = h;

                        count[0] = 1;
                        
                        List<Integer> h_stack = new ArrayList<Integer>();
                        List<Integer> w_stack = new ArrayList<Integer>();
                        h_stack.add(h);
                        w_stack.add(w);

                        int s, x, y, h1, w1, ind;
                        while(!h_stack.isEmpty()) {
                            s = h_stack.size()-1;
                            h1 = h_stack.remove(s);
                            w1 = w_stack.remove(s);
                            for (int i1=0; i1<4; i1++) {
                                y = h1 + dy4[i1];
                                x = w1 + dx4[i1];
                                if ((y < height && y >= 0) && (x < width && x >= 0)) {
                                    ind = y * width + x;
                                    if (nlabels[ind] < 0 && labels[ind] == labels[h1 * width + w1]) {
                                        xvec[count[0]] = x;
                                        yvec[count[0]] = y;
                                        count[0]++;
                                        nlabels[ind] = lab;
                                        h_stack.add(y);
                                        w_stack.add(x);
                                    }
                                }
                            }
                        }

                        //-------------------------------------------------------
                        // If segment size is less then a limit, assign an
                        // adjacent label found before, and decrement label count.
                        //-------------------------------------------------------
                        if (count[0] <= (SUPSZ >> 2)) {
                            for (int c = 0; c < count[0]; c++) {
                                int ind1 = yvec[c] * width + xvec[c];
                                nlabels[ind1] = adjlabel;
                            }
                            lab--;
                        }
                        lab++;
                    }
                    i++;
                }
            }
        }
        numlabels = lab;
        return numlabels;
    }