opencv for java 遮挡检测(适用于图片像素较小)

新手
因为此代码用到了驾驶室内的摄像头分析,所以图片像素较小,运行速率影响较小。
若图片像素很大,这方案运行速率影响很大,不建议使用。

//首先是第一层检测,用灰度与边缘检测,分四个方向进行了检测
public  boolean detect_Shelter(String image_path)
{

        im = imread(image_path);

        Mat dst;
        Mat img_lefttop;
        Mat img_righttop;
        Mat img_leftbottom;
        Mat img_rightbottom;

        dst = new Mat();
        Mat test = new Mat();
        cvtColor(im, dst,COLOR_RGB2GRAY);
        Canny(dst, test, 0, 255, 3);
		//这里的范围与阈值10都可根据自己的情况进行调整,下面四个方向的测试相同
        Range rangerows = new Range(9,dst.rows()/5*3);
        Range rangecols = new Range(9,dst.cols()/5*3);
        img_lefttop=new Mat(test,rangerows,rangecols);
        //System.out.println("img_lefttop"+img_lefttop);
        if(Core.countNonZero(img_lefttop)<10)
        {
        //因为第一层检测会导致大量的夜晚环境误报遮挡,所以需要第二层像素点检测。
            if(detect_Shelter_RGB(im))
            {
                return true;
            }
        }


        rangerows = new Range(9,dst.rows()/5*3);
        rangecols = new Range(dst.cols()/5*2-1,dst.cols());
        img_righttop = new Mat(test, rangerows, rangecols);
        //System.out.println("img_righttop"+img_righttop);
        if(Core.countNonZero(img_righttop)<10)
        {
            if(detect_Shelter_RGB(im))
            {
                return true;
            }
        }

        rangerows = new Range(dst.rows()/5*2-1,dst.rows());
        rangecols = new Range(9,dst.cols()/5*3);
        img_leftbottom = new Mat(test, rangerows, rangecols);
        //System.out.println("img_leftbottom"+img_leftbottom);
        if(Core.countNonZero(img_leftbottom)<10)
        {
            if(detect_Shelter_RGB(im))
            {
                return true;
            }
        }

        rangerows = new Range(dst.rows()/5*2-1,dst.rows());
        rangecols = new Range(dst.cols()/5*2-1,dst.cols());
        img_rightbottom = new Mat(test, rangerows, rangecols);
        //System.out.println("img_rightbottom"+img_rightbottom);
        if(Core.countNonZero(img_rightbottom)<10)
        {
            if(detect_Shelter_RGB(im))
            {
                return true;
            }
        }
        return false;


}
//遮挡第二层判断,像素点比较。遮挡与黑暗环境的区别就是,遮挡会有一部分像素较为高的点,黑暗环境则像素都很低。
    public  boolean detect_Shelter_RGB(Mat im)
    {
        int num = 0;
        float rate;
        double[] pixel = new double[3];
        int x=0; int y= 0; int z =0;//测试用,正式可删除
        for (int i = 0; i < im.rows(); i++) {
            for (int j = 0; j < im.cols(); j++) {
                pixel = im.get(i,j).clone();
                double b = pixel[0];
                double g = pixel[1];
                double r = pixel[2];
                //下面三个判断用来测验RGB大于150的点有多少,正式使用时可删除
                if (b>=150)
                {
                    x++;
                }
                if (g>=150)
                {
                    y++;
                }
                if (r>=150)
                {
                    z++;
                }

                //像素阈值越小,越黑,遮挡会有部分比较亮的点,阈值可自行更改
                if (b >=150 && g >=150 && r >150)
                {

                    num++;
                }
            }
        }
        //用来测试RGB各个通道像素大于150的点有多少,逐步确定150是否合适。
        System.out.println("BBBBB"+x);
        System.out.println("GGGGG"+y);
        System.out.println("RRRRR"+z);
        rate = (float) num / (float) (im.rows() * im.cols());
        System.out.println("num"+num);
        System.out.println("im.rows() * im.cols()"+im.rows() * im.cols());
        System.out.println("rate"+rate);
        //亮点占比 大于阈值就是遮挡
        if (rate > 0.035) {
            System.out.println("摄像头遮挡");
            return true;
        }
        System.out.println("比例:"+rate);
        return false;
    }

版权声明:本文为JWQ_CSDN原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。