java opencv 基本操作11

连通域标记图像函数:ConnectedComponents

    /**
     * 连通域标记图像ConnectedComponents,用于轮廓分析
     * 对于一张二值化的图像,后续处理方式有两种。第一种方式就是利用findContours、drawContours等函数进行轮廓分析
     * (opencv以对轮廓的处理为主)。第二种方式就是计算连通域进行区域分析
     * connectedComponents(
     *         Mat   image,             // 输入二值图像
     *         Mat   labels,            // 输出的标记图像,背景index=0
     *         int   connectivity = 8,  // 连通域,默认是8连通
     *         int   ltype = CvType.CV_32S     // 输出的labels类型,默认是CV_32S
     * )
     */
    @Test
    public void testConnectedComponents() {
        Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");

        //自动阈值
        Mat gray = new Mat();
        Mat binary = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

        //计算连通域
        Mat labels = new Mat(src.size(), CvType.CV_32S);
        int num = Imgproc.connectedComponents(binary, labels, 8, CvType.CV_32S);
        System.out.println("标记数:" + num);

        //定义颜色
        List<Integer[]> colors = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            int r = random.nextInt(255);
            int g = random.nextInt(255);
            int b = random.nextInt(255);
            colors.add(new Integer[]{r, g, b});
        }

        //标记联通域
        Mat dst = new Mat(src.size(), src.type());
        dst.setTo(new Scalar(255, 255, 255));
        int width = src.cols();
        int height = src.rows();
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                int label = (int) labels.get(row, col)[0];
                if (label == 0) continue;
                dst.put(row, col, colors.get(label)[0], colors.get(label)[1], colors.get(label)[2]);
            }
        }
        GeneralUtils.saveByteImg(dst, "C:\\图片\\test\\connectedComponents.jpg");

    }

带有统计信息的连通域标记图像函数:connectedComponentsWithStats

    /**
     * 带有统计信息的连通域标记图像
     * connectedComponentsWithStats(
     *         Mat   image,        // 输入二值图像
     *         Mat  labels,       // 输出的标记图像,背景index=0
     *         Mat  stats,        // 统计信息,包括每个组件的位置、宽、高与面积
     *         Mat  centroids,    // 每个组件的中心位置坐标cx, cy
     *         int          connectivity, // 连通域,默认是8连通
     *         int          ltype,        // 输出的labels类型,默认是CV_32S
     *         int          ccltype       // 连通组件算法
     * )
     */
    @Test
    public void testConnectedComponentsWithStats() {
        Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");

        //自动阈值
        Mat gray = new Mat();
        Mat binary = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

        //计算连通域
        Mat labels = new Mat(src.size(), CvType.CV_32S);
        Mat status = new Mat();
        Mat centroids = new Mat();
        int num = Imgproc.connectedComponentsWithStats(binary, labels, status, centroids, 8, 4);
        System.out.println("标记数:" + num);

        //定义颜色
        List<Scalar> colors = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            int r = random.nextInt(255);
            int g = random.nextInt(255);
            int b = random.nextInt(255);
            colors.add(new Scalar(r, g, b));
        }

        //标记联通域
        Mat dst = src.clone();
        dst.setTo(new Scalar(255, 255, 255));

        for (int i = 1; i < num; i++) {
            //中心位置
            double cx = centroids.get(i, 0)[0];
            double cy = centroids.get(i, 1)[0];

            int x = (int) status.get(i, Imgproc.CC_STAT_LEFT)[0];
            int y = (int) status.get(i, Imgproc.CC_STAT_TOP)[0];
            int w = (int) status.get(i, Imgproc.CC_STAT_WIDTH)[0];
            int h = (int) status.get(i, Imgproc.CC_STAT_HEIGHT)[0];

            //绘制中心位置
            Imgproc.circle(dst, new Point(cx, cy), 2, new Scalar(0, 0, 255), 2, 8, 0);

            //绘制外接矩形
            Imgproc.rectangle(dst, new Point(x, y), new Point(x + w, y + h), colors.get(i), 1, 8, 0);

        }
        GeneralUtils.saveByteImg(dst, "C:\\图片\\test\\connectedComponentsWithStats.jpg");

    }
《中医基础理论》
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古智云开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值