细化算法实现
邻域说明
前提说明
*蓝色为前景,白色为背景
我们想要决定是剥离p1还是将其作为生成骨架的一部分。为此,我们按时钟顺序排列p0的8个邻居,并定义了两个函数:
- B(p0)= p0的非零邻居的数量
- A(p0)= 序列p1 , p2,p3,p4,p5,p6,p7,p8,p1中的0-1个模式的数量(0指代背景, 1指代前景)
B(p0)= 2,A(p0)= 1
B(p0)= 2,A(p0)= 2
算法说明
Hilditch算法有两个版本,一个使用4x4窗口,另一个使用3x3窗口。这里我们关注3x3窗口版本。
算法条件说明
Hilditch的算法包括在模式上执行多次传递,并且在每次传递时,算法检查所有像素并决定在满足以下四个条件时将像素从前景更改为背景:
- 2 <= B(p0)<= 6
- A(P0)= 1
- p1*p3*p7 = 0或A(p1)!= 1
- p1*p3*p5 = 0或A(p3)!= 1
当没有任何变化时停止(不能再删除像素)
让我们分别查看上述每个条件。
条件1说明
条件1:2 <= B(p0)<= 6
这个条件结合了两个子条件,第一个是p1的非零邻居数大于或等于2,第二个小于或等于6第一个条件确保没有删除没有孤立的终点像素(任何具有1个黑色邻居的像素是终点像素),第二个条件确保像素是边界像素。
B(p0)= 1
B(P0)= 0
B(p0)= 7
如图所示,如果B(p0)= 1,那么p1是一个骷髅尖端,不应删除。如果B(p0)= 0,那么p1是一个孤立点,也应该保留(如果它是一个噪声,删除像素不是骨架化过程的工作(这是噪声去除算法的工作)如果B(p0)= 7,则p1不再位于模式的边界上,因此不应该是再生的候选者。
条件2说明
条件2:A(p0)= 1
这是一个连接测试。实际上,如果您考虑下面的图片A(p0)> 1,您可以看到通过将p1更改为0,模式将断开连接。
A(p0)= 2
A (p0)= 2
A(p0)= 3
条件3说明
条件3:p1*p3*p7 = 0或A(p1)!= 1
A(p1)!=1
p1*p3*p7 = 0。
p1*p3*p7!= 0且A(p1)= 1。
这种情况确保2像素宽的垂直线不会被算法完全侵蚀。
条件4说明
条件4:p1*p3*p5 = 0或A(p3)!= 1
其中A(p3)!= 1
p1*p3*p5 = 0
p1*p3*p5!= 0和A(p3)= 1。
这种情况确保了2像素宽的水平线不会被算法完全侵蚀。
Hilditch算法的性质
它是一种并行顺序算法。它是平行的,因为在一次通过时,同时检查所有像素,并且决定是否移除每个检查的像素。它是顺序的,因为刚刚提到的这一步重复几次(直到不再进行更改)。
然而,Hilditch的算法结果不是骨架化的完美算法,因为它不适用于所有模式。实际上,有些模式会被算法完全擦除。
代码实现
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class OpencvTest {
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String[] args) {
OpencvTest opencvTest = new OpencvTest ();
//读入图片
Mat src = Imgcodecs.imread("F:\\opencvPhoto\\photo\\82.jpg");
//灰度化
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
//二值化
Imgproc.threshold(src, src, 120, 255, Imgproc.THRESH_BINARY);
//细化
Mat hilditch = opencvTest .hilditch(src);
Imgcodecs.imwrite("F:\\opencvPhoto\\result\\hilditch.jpg", hilditch);
}
/**
* 细化算法:
* p8 p1 p2
* p7 p0 p3
* p6 p5 p4
* @param mat
*/
static int frontBackground = 0; //前背景
static int backBackground = 255; //后背景
public Mat hilditch(Mat mat) {
Mat dst = mat.clone();
boolean isEnd = true;
while (isEnd) {
Mat temp = new Mat();
dst.copyTo(temp);
isEnd = false;
for (int row = 0; row < mat.rows(); row++) {
for (int col = 0; col < mat.cols(); col++) {
//1、必须为前背景
if (temp.get(row, col)[0] != frontBackground) {
continue;
}
//获得8领域
double[] neighbors = getNeighbors(temp, row, col);
//2、 2 <= 获得非零邻居的数量 <= 6
int notZeroNum = getNeighborsNotZeroNum(neighbors);
if (notZeroNum < 2 || notZeroNum > 6) {
continue;
}
//3、0-1模式数量要为1
if (getZeroAndOneModelCount(neighbors) != 1) {
continue;
}
//4、p1*p3*p7=0 || p1的0-1模型个数!=1
if (neighbors[1] == 0 || neighbors[3] == 0 || neighbors[7] == 0
|| getZeroAndOneModelCount(getNeighbors(temp, row - 1, col)) != 1) {
//5、p1*p3*p5=0 || p3的0-1模型个数!=1
if (neighbors[1] == 0 || neighbors[3] == 0 || neighbors[5] == 0
|| getZeroAndOneModelCount(getNeighbors(temp, row, col + 1)) != 1) {
//符合以上条件的像素点设为后背景色
dst.put(row, col, backBackground);
isEnd = true;
}
}
}
}
}
return dst;
}
/**
* 获得8领域
* p8 p1 p2
* p7 p0 p3
* p6 p5 p4
* @param mat 图片Mat
* @param row 行数
* @param col 列数
* @return
*/
public double[] getNeighbors(Mat mat, int row, int col) {
double[] neighbors = new double[9];
if (row > 0 && row < mat.rows() && col > 0 && col < mat.cols()) {
neighbors[0] = mat.get(row, col)[0];
}
if (row - 1 > 0 && row - 1 < mat.rows() && col > 0 && col < mat.cols()) {
neighbors[1] = mat.get(row - 1, col)[0];
}
if (row - 1 > 0 && row - 1 < mat.rows() && col + 1 > 0 && col + 1 < mat.cols()) {
neighbors[2] = mat.get(row - 1, col + 1)[0];
}
if (row > 0 && row < mat.rows() && col + 1 > 0 && col + 1 < mat.cols()) {
neighbors[3] = mat.get(row, col + 1)[0];
}
if (row + 1 > 0 && row + 1 < mat.rows() && col + 1 > 0 && col + 1 < mat.cols()) {
neighbors[4] = mat.get(row + 1, col + 1)[0];
}
if (row + 1 > 0 && row + 1 < mat.rows() && col > 0 && col < mat.cols()) {
neighbors[5] = mat.get(row + 1, col)[0];
}
if (row + 1 > 0 && row + 1 < mat.rows() && col - 1 > 0 && col - 1 < mat.cols()) {
neighbors[6] = mat.get(row + 1, col - 1)[0];
}
if (row > 0 && row < mat.rows() && col - 1 > 0 && col - 1 < mat.cols()) {
neighbors[7] = mat.get(row, col - 1)[0];
}
if (row - 1 > 0 && row - 1 < mat.rows() && col - 1 > 0 && col - 1 < mat.cols()) {
neighbors[8] = mat.get(row - 1, col - 1)[0];
}
return neighbors;
}
/**
* 获得前背景邻居的数量
* p8 p1 p2
* p7 p0 p3
* p6 p5 p4
* @param neighbors 8领域
* @return
*/
public int getNeighborsNotZeroNum(double[] neighbors) {
int count = 0;
for (int i = 1; i < neighbors.length; i++) {
if (neighbors[i] == frontBackground) {
count++;
}
}
return count;
}
/**
* 按p1->p8的顺序获得0-1模型个数(后背景-前背景)
* p8 p1 p2
* p7 p0 p3
* p6 p5 p4
* @param neighbors 8领域
* @return
*/
public int getZeroAndOneModelCount(double[] neighbors) {
int count = 0;
if (neighbors[1] == backBackground && neighbors[2] == frontBackground) { count++; }
if (neighbors[2] == backBackground && neighbors[3] == frontBackground) { count++; }
if (neighbors[3] == backBackground && neighbors[4] == frontBackground) { count++; }
if (neighbors[4] == backBackground && neighbors[5] == frontBackground) { count++; }
if (neighbors[5] == backBackground && neighbors[6] == frontBackground) { count++; }
if (neighbors[6] == backBackground && neighbors[7] == frontBackground) { count++; }
if (neighbors[7] == backBackground && neighbors[8] == frontBackground) { count++; }
if (neighbors[8] == backBackground && neighbors[1] == frontBackground) { count++; }
return count;
}
}
原图
结果
联结数说明
联结数指在像素p的3*3邻域中,和p连接的图形分量的个数:
上图中,左图的4连通联结数是2,8连通联结数是1,而右图的4联通联结数和8联通联结数都是2。
4连通联结数计算公式是:
8连通联结数计算公式是:
其中:
参考链接:
联结数说明
细化算法实现