Java OpenCV腐蚀膨胀内核锚点

内核坐标系为

在这里插入图片描述

测试代码

	public static void main(String[] args) {

		//读入灰度图
        Mat src = Imgcodecs.imread("F:\\opencvPhoto\\photo\\108.jpg", Imgcodecs.IMREAD_GRAYSCALE);
        //二值化
        Imgproc.threshold(src, src, 120, 255, Imgproc.THRESH_BINARY);
        //对比图
        Mat temp = src.clone();
        
        int width = 4; //内核宽度
        int height = 4; //内核高度
        Mat element= Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(width , height ));

        Point point = new Point(-1, -1); //定义内核位置 等价于 Point(width  /2, height / 2);
        //膨胀 
        Imgproc.dilate(src, src, horizontalStructure, point, 1, Core.BORDER_CONSTANT, new Scalar(255));
        Imgcodecs.imwrite("F:\\opencvPhoto\\result\\dst1.jpg", src);
        
		/***************************还原原图*************************/
	
		//定义内核位置
        int x = width / 2;
        int y = width / 2;
        if ((minWidth&1) == 0) {  //偶数
            x -= 1;
        }
        if ((minWidth&1) == 0) { //偶数
            y -= 1;
        }
        point = new Point(x, y);
        //腐蚀
        Imgproc.erode(src, src, horizontalStructure, point, 1, Core.BORDER_CONSTANT, new Scalar(255));
        
		//判断是否与原图一致
        boolean flag = true;
        for (int i = 0; i < temp.rows(); i++) {
            for (int j = 0; j < temp.cols(); j++) {
                if (src.get(i, j)[0] != temp.get(i, j)[0]) {
                    flag = false;
                    break;
                }
            }
            if (!flag) {
                break;
            }
        }
        System.out.println(flag);

    }

代码内核锚点 :(x, y)
在这里插入图片描述
原图
在这里插入图片描述
原图坐标说明
在这里插入图片描述
dst1坐标说明
在这里插入图片描述
后台输出
true

其他说明

边界填充
腐蚀膨胀说明

发布了90 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/wangwenjie1997/article/details/102499396