内核坐标系为
测试代码
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