opencv 二 轮廓识别

基于之前的需求,最近又有新需求啦;需要将临近电梯的房间标记
最近还在做房间朝向、临路等需求,后面再更新

第一篇识的文字贴到这里 opencv 一 轮廓识别 https://www.jianshu.com/p/30dfeab6c0c3

获取电梯扩大矩形

 /**
     * 获取电梯扩大矩形
     * @param src
     * @param collision 临近值
     * @return
     */
    public static List getNearElevator(Mat src,int collision) {
        //rgb 89 86 86
        Mat dtMat = new Mat();
        //寻找电梯rgb
        Core.inRange(src, new Scalar(86, 86, 89), new Scalar(86, 86, 89), dtMat);
        //高斯滤波(忽略图片毛点)
        Imgproc.GaussianBlur(dtMat, dtMat, new Size(9, 9), 9, 9);
        //输出二值后的电梯图片
        Imgcodecs.imwrite("D:\\img\\0.dt.png", dtMat);
        List dtMP = new ArrayList<>();
        //寻找电梯轮廓
        Imgproc.findContours(dtMat, dtMP, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        Mat dtSrc = src.clone();
        List dtRectPointList = new ArrayList<>();
        int dtIndex = 0;
        //遍历电梯矩形轮廓描点
        for (MatOfPoint cnt : dtMP) {
            //提取电梯矩形
            Rect rect = Imgproc.boundingRect(cnt);
            //扩大电梯矩形,用户计算临近电梯房间
            //计算左上角点
            Point startPoint = new Point(rect.x - collision, rect.y - collision);
            List pointList = new ArrayList<>();
            pointList.add(startPoint);
            //计算右上角点
            pointList.add(new Point(rect.x + rect.width + collision, rect.y - collision));
            //计算左下角点
            pointList.add(new Point(rect.x + rect.width + collision, rect.y + rect.height + collision));
            //计算右下角点
            pointList.add(new Point(rect.x - collision, rect.y + rect.height + collision));
            Mat trackDt = src.clone();
            //标记扩大矩形角点
            for (Point pointItem : pointList) {
                Imgproc.drawMarker(trackDt, pointItem, new Scalar(0, 0, 15), Imgproc.MARKER_TILTED_CROSS);
            }
            //将扩大矩形记录
            dtRectPointList.add(new Rect(startPoint, new Size(rect.width + collision, rect.y - collision)));
            Imgcodecs.imwrite("D:\\img\\src" + dtIndex + ".png", trackDt);
            dtIndex++;
        }
        System.out.println("dt" + dtMP.size());
        return dtRectPointList;
    }

根据房间坐标计算是否临近电梯

List dtRectPointList = getNearElevator(src,collision);
 for (Point pointItem : roomPointList) {
              if (!isNearElevator && pointList.contains(pointItem)) {
                  System.out.println("临近电梯:" + roomNumber);
                  isNearElevator = true;
                  break;
               }
  }
原图
image.png
二值电梯图片
image.png
电梯矩形扩大
a1.jpg

你可能感兴趣的:(opencv 二 轮廓识别)