1

我想在 java 中使用 matlab 的 bwmorph(thin) 函数。我在 matlab 页面上找到了算法:http: //www.mathworks.se/help/images/ref/bwmorph.html

并用java写:(为了图像保存,我使用openCV Mat数据结构,基本上是二维数组)

 package pbl;

 import org.opencv.core.CvType;
 import org.opencv.core.Mat;


public class Thinning {

boolean B[][];

public Mat doJaniThinning(Mat Image) {
 B = new boolean[Image.rows()][Image.cols()];
 boolean [][] B_ = new boolean[Image.rows()][Image.cols()];
    for(int i=0; i<Image.rows(); i++) 
        for(int j=0; j<Image.cols(); j++) 
                    B[i][j] = (Image.get(i, j)[0]>100?false:true); //not a mistake, in matlab first invert and then morph

   for(int iter = 0; iter < 1000; iter++) {
            //Iteration #1
            for(int i=0; i<Image.rows(); i++) 
                for(int j=0; j<Image.cols(); j++) 
                    if(B[i][j] && G1(i, j) && G2(i, j) && G3(i, j)) B_[i][j] = false;
                    else B_[i][j] = B[i][j];;

            for(int i=0; i<Image.rows(); i++) 
                for(int j=0; j<Image.cols(); j++) B[i][j] = B_[i][j];
            //Iteration #2
            for(int i=0; i<Image.rows(); i++) 
                for(int j=0; j<Image.cols(); j++) 
                    if(B[i][j] && G1(i, j) && G2(i, j) && G3_(i, j)) B_[i][j] = false;
                    else B_[i][j] = B[i][j];


            for(int i=0; i<Image.rows(); i++) 
                for(int j=0; j<Image.cols(); j++) B[i][j] = B_[i][j];
    }


    Mat r = new Mat(Image.rows(), Image.cols(), CvType.CV_32SC1);

    for(int i=0; i<Image.rows(); i++) 
            for(int j=0; j<Image.cols(); j++) {
                int[] a = new int[1];
                a[0] = B[i][j]?255:0;
                r.put(i, j, a);
       }


        return r;
}

public boolean x(int a, int i, int j) {
    try {
    switch(a) {
        case 1:
            return B[i+1][j];

        case 2:
            return B[i+1][j+1];

        case 3:
            return B[i][j+1];

        case 4:
            return B[i-1][j+1];

        case 5:
            return B[i-1][j];

        case 6:
            return B[i-1][j-1];

        case 7:
            return B[i][j-1];

        case 8:
            return B[i+1][j-1];


    }
    } catch(IndexOutOfBoundsException e) {
    return false;
    }
    return false;
}

public boolean G1(int i, int j) {
    int X = 0;
    for(int q=1; q<=4; q++) {
        if(!x(2*q-1, i, j) && (x(2*q, i, j) || x(2*q+1, i, j))) X++;
    }
    return X==1;
}

public boolean G2(int i, int j) {
  int m = Math.min(n1(i, j), n2(i, j));
  return (m==2 || m==3);
}

public int n1(int i, int j) {
    int r = 0;
    for(int q=1; q<=4; q++) 
        if(x(2*q-1, i, j) || x(2*q, i, j)) r++;
    return r;
}

public int n2(int i, int j) {
    int r = 0;
    for(int q=1; q<=4; q++) 
        if(x(2*q, i, j) || x(2*q+1, i, j)) r++;
    return r;
}

public boolean G3(int i, int j) {
    return !(x(2, i, j) || x(3, i, j) || !x(8, i, j) && x(1, i, j));
}

public boolean G3_(int i, int j) {
    return !(x(6, i, j) || x(7, i, j) || !x(4, i, j) && x(5, i, j));
}





}

它没有用。图像试图变薄:

在此处输入图像描述

Matlab结果:

在此处输入图像描述

我的java代码结果:

在此处输入图像描述

不知道该怎么做,有什么帮助吗?提前致谢

编辑:我更正了 G3 和 G3_,但现在我得到:

在此处输入图像描述

这仍然与 matlab 不同,有点不同,但我需要那个

4

1 回答 1

2

我认为 G3 和 G3_ 是错误的:

public boolean G3(int i, int j) {
    return (x(2, i, j) || x(3, i, j) || !x(8, i, j)) && x(1, i, j);
}

public boolean G3_(int i, int j) {
    return (x(6, i, j) || x(7, i, j) || !x(4, i, j)) && x(5, i, j);
}
于 2013-11-07T16:10:00.303 回答