Example usage for org.opencv.imgproc Imgproc connectedComponentsWithStats

List of usage examples for org.opencv.imgproc Imgproc connectedComponentsWithStats

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc connectedComponentsWithStats.

Prototype

public static int connectedComponentsWithStats(Mat image, Mat labels, Mat stats, Mat centroids,
            int connectivity, int ltype) 

Source Link

Usage

From source file:OCV_ConnectedComponentsWithStats.java

License:Open Source License

@Override
public void run(ImageProcessor ip) {
    // src// ww w.  j  a va  2 s  .c o m
    int imw = ip.getWidth();
    int imh = ip.getHeight();
    byte[] src_arr = (byte[]) ip.getPixels();
    Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1);

    // dst
    String titleDst = WindowManager
            .getUniqueName(impSrc.getTitle() + "_Connect" + String.valueOf(TYPE_INT[type_ind]));
    ImagePlus impDst = new ImagePlus(titleDst, new FloatProcessor(imw, imh));
    float[] dst_arr = (float[]) impDst.getChannelProcessor().getPixels();
    Mat dst_mat_32s = new Mat(imh, imw, CvType.CV_32S);
    Mat dst_mat_32f = new Mat(imh, imw, CvType.CV_32F);
    Mat stats_mat = new Mat();
    Mat cens_mat = new Mat();

    // run
    src_mat.put(0, 0, src_arr);
    int output_con = Imgproc.connectedComponentsWithStats(src_mat, dst_mat_32s, stats_mat, cens_mat,
            TYPE_INT[type_ind], CvType.CV_32S);
    dst_mat_32s.convertTo(dst_mat_32f, CvType.CV_32F);
    dst_mat_32f.get(0, 0, dst_arr);

    // show data
    if (1 < output_con) {
        showData(dst_arr, imw, imh, output_con, stats_mat, cens_mat);
    }

    // finish
    if (1 < output_con && enOutImg) {
        impDst.show();
    } else {
        impDst.close();
    }
}