java opencv 基本操作6

ROI操作,可以用于裁剪图片,合并图片
 public void testRoi() {
        Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");

        int h = src.rows();
        int w = src.cols();

        //获取ROI, 相当于裁剪图片
        Rect rect = new Rect(h / 2, w / 2, 50, 50);
        Mat roi = new Mat(src, rect);
        GeneralUtils.saveByteImg(roi, "C:\\图片\\test\\roi.jpg");

        //修改roi区域为红色
        Mat image = roi.clone();
        roi.setTo(new Scalar(0, 0, 255));
        GeneralUtils.saveByteImg(roi, "C:\\图片\\test\\updateRoi.jpg");

    }
直方图 calcHist
    //绘制直方图
    //calcHist
    @Test
    public void test17() {
        Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");

        int dims = src.channels();
        MatOfInt bins = new MatOfInt(256);
        MatOfFloat hranges = new MatOfFloat(0, 255);
        if (dims == 3) {
            List<Mat> bgr_plane = new ArrayList<>();
            Core.split(src, bgr_plane);
            Mat b_hist = new Mat();
            Mat g_hist = new Mat();
            Mat r_hist = new Mat();

            //计算蓝色通道,绿色通道,红色通道的直方图
            Imgproc.calcHist(Arrays.asList(bgr_plane.get(0)), new MatOfInt(0), new Mat(), b_hist, bins, hranges);
            Imgproc.calcHist(Arrays.asList(bgr_plane.get(1)), new MatOfInt(0), new Mat(), g_hist, bins, hranges);
            Imgproc.calcHist(Arrays.asList(bgr_plane.get(2)), new MatOfInt(0), new Mat(), r_hist, bins, hranges);


            //显示直方图
            int hist_w = 512;
            int hist_h = 400;
            int bin_w = (int) (hist_w / bins.get(0, 0)[0]);

            Mat histImage = Mat.zeros(hist_h, hist_w, CvType.CV_8UC3);
            //归一化直方图
            Core.normalize(b_hist, b_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());

            for (int i = 1; i < bins.get(0,0)[0]; i++) {
                Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - b_hist.get(i - 1 , 0)[0]),
                        new Point(bin_w * i, hist_h - b_hist.get(i, 0)[0]), new Scalar(255, 0, 0), 2, 8, 0);
                Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - g_hist.get(i - 1 , 0)[0]),
                        new Point(bin_w * i, hist_h - g_hist.get(i, 0)[0]), new Scalar(0, 255, 0), 2, 8, 0);
                Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - r_hist.get(i - 1 , 0)[0]),
                        new Point(bin_w * i, hist_h - r_hist.get(i, 0)[0]), new Scalar(0, 0, 255), 2, 8, 0);

            }

            GeneralUtils.saveByteImg(histImage, "C:\\图片\\test\\histImage.jpg");

        }
    }
直方图均衡化 equalizeHist 
   //equalizeHist 直方图均衡化,,用于提高图像的质量
    public void testEqualizeHist() {
        Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");

        Mat gray = new Mat();
        Mat dst = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.equalizeHist(gray, dst);
        GeneralUtils.saveByteImg(dst, "C:\\图片\\test\\equalizeHist.jpg");

    }

图片相识度比较 compareHist

public void testComareHist() {
        Mat src1 = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");
        Mat src2 = GeneralUtils.converMat("C:\\图片\\test\\0002.jpg");

        //转成hsv色彩空间
        Mat hsv1 = new Mat();
        Mat hsv2 = new Mat();
        Imgproc.cvtColor(src1, hsv1, Imgproc.COLOR_BGR2HSV);
        Imgproc.cvtColor(src2, hsv2, Imgproc.COLOR_BGR2HSV);

       
        //计算直方图
        MatOfInt histSize = new MatOfInt(60);
        MatOfFloat h_ranges = new MatOfFloat(0, 255);

        Mat hist1 = new Mat();
        Mat hist2 = new Mat();
        Imgproc.calcHist(Arrays.asList(hsv1), new MatOfInt(0), new Mat(), hist1, histSize, h_ranges);
        Imgproc.calcHist(Arrays.asList(hsv2), new MatOfInt(0), new Mat(), hist2, histSize, h_ranges);

        //归一化
        Core.normalize(hist1, hist1, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        Core.normalize(hist2, hist2, 0, 1, Core.NORM_MINMAX, -1, new Mat());

        //比较直方图
        double comp = Imgproc.compareHist(hist1, hist2, 0);
        double comp1 = Imgproc.compareHist(hist1, hist2, 1);

        System.out.println("图片相似度0:" + comp);
        System.out.println("图片相似度1:" + comp1);
    }

文中所用到的GeneralUtils如下:GeneralUtils

《百病自测》

求关注!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

古智云开

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

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

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

打赏作者

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

抵扣说明:

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

余额充值