【问题标题】:How to compute the co-occurrence matrix in Java?如何在 Java 中计算共现矩阵?
【发布时间】:2018-08-25 01:46:06
【问题描述】:

我有一个关于共现公式的问题。如何在 Java 中实现图像 GLCM 的计算?

具体来说,我试图弄清楚如何计算一个像素的强度为x 的次数,而其右侧像素的强度为y 的次数。我还需要将获得的值存储在结果共现矩阵的x-th 行和y-th 列中。

预期的行为如下所示:

这是我目前得到的:

代码(尚未完成)

public class MainClass {
final static int[][] matrix= {
        {2,4,1,3},
        {7,2,1,6},
        {1,1,2,2},
        {1,2,5,8}
};
static int i;
static int j;
static int x;
static int y;
static int c;
static int d;
static int maxValue = matrix[0][0];
static int minValue = matrix[0][0];

public static void main(String[] args) {
    // TODO Auto-generated method stub

    for(i = 0; i< matrix.length; i++) {
        for(j=0; j < matrix[i].length; j++) {
            System.out.print(matrix[i][j] + "");
            if(matrix[i][j] > maxValue) {
                maxValue=matrix[i][j];
            }
            else if(matrix[i][j] < minValue) {
                minValue=matrix[i][j];
            }                               
        }
        System.out.println();
    }       
    System.out.println("maxValue = "+ maxValue);

    int count = 0;
    for(int i=0; i< matrix.length; i++) {
        for (int j=0; j<matrix[i].length; j++) {
             int x = i;
             int y = j;
             if(matrix[x][y] == 1 & matrix[x][y+1] ==1) {
                 count ++;
             }
             System.out.println(matrix[x][y+1]);
        }
    }
}

输出(错误)

2413
7216
1122
1258
maxValue = 8
4
1
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
 at main.MainClass.main(MainClass.java:45)

我不希望使用第三方库,例如 OpenCV 或 jfeaturelib。

【问题讨论】:

  • matrix[x][y+1] 可能在底部的循环中执行此操作: for (int j=0; j
  • 是的,但是,当我尝试擦除 y+1 并尝试使其共现不起作用时,那我该怎么办?
  • 我现在不知道你在做什么,你的解释也不好。所以我给你一个建议:避开外围;如果你必须去 y+1 让 j 去 j
  • 同意 gpasch

标签: java image-processing feature-extraction glcm


【解决方案1】:

这段代码完成了工作:

public class MainClass {

    final static int[][] I = {
        {1, 1, 5, 6, 8},
        {2, 3, 5, 7, 1},
        {4, 5, 7, 1, 2},
        {8, 5, 1, 2, 5}
    };

    public static int getMaxValue(int[][] array) {
        int maxValue = array[0][0];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] > maxValue) {
                    maxValue = array[i][j];
                }
            }
        }
        return maxValue;
    }

    static int maxValue = getMaxValue(I);

    public static void main(String[] args) {

        System.out.println("I");
        for (int row = 0; row < I.length; row++) {
            for (int col = 0; col < I[row].length; col++) {
                System.out.print(I[row][col] + " ");
            }
            System.out.println();
        }

        System.out.println("maxValue = " + maxValue);

        int[][] GLCM = new int[maxValue + 1][maxValue + 1];

        for (int row = 0; row < I.length; row++) {
            for (int col = 0; col < I[row].length - 1; col++) {
                int x = I[row][col];
                int y = I[row][col + 1];
                GLCM[x][y]++;
            }
        }

        System.out.println("GLCM");
        for (int x = 1; x <= maxValue; x++) {
            for (int y = 1; y <= maxValue; y++) {
                System.out.print(GLCM[x][y] + " ");
            }
            System.out.println();
        }
    }
}

输出

I
1 1 5 6 8 
2 3 5 7 1 
4 5 7 1 2 
8 5 1 2 5 
maxValue = 8
GLCM
1 2 0 0 1 0 0 0 
0 0 1 0 1 0 0 0 
0 0 0 0 1 0 0 0 
0 0 0 0 1 0 0 0 
1 0 0 0 0 1 2 0 
0 0 0 0 0 0 0 1 
2 0 0 0 0 0 0 0 
0 0 0 0 1 0 0 0

备注

  1. 我假设最小强度是1。如果您希望考虑强度值为0 的像素,您应该像这样更改最后两个for 循环:

    for (int x = 0; x <= maxValue; x++) {
        for (int y = 0; y <= maxValue; y++) {
    
  2. 建议的解决方案产生对应于位移矢量“向右一个像素”的共生矩阵。如果您希望使用不同的位移矢量来计算 GLCM,则需要调整代码。例如下面的sn -p返回“向上偏移一个像素”对应的GLCM:

    for (int row = 1; row < I.length; row++) {
        for (int col = 0; col < I[row].length; col++) {
            int x = I[row][col];
            int y = I[row - 1][col];
            GLCM[x][y]++;
        }
    }
    

【讨论】:

  • 谢谢...这就是我想要的 XD,所以基本上我被那 2 行代码卡住了 int x = I[row][col]; int y = I[行][col + 1]; GLCM[x][y]++;非常感谢@Tonechas 先生
  • 谁能帮助我们找到 45 和 135 的 glcm 矩阵。谢谢
  • 对于 45 度,您必须将 int y = I[row - 1][col]; 替换为 int y = I[row - 1][col + 1]; 并相应地更改列限制:for (int col = 0; col &lt; I[row].length - 1; col++)
猜你喜欢
  • 2017-05-30
  • 1970-01-01
  • 2018-07-09
  • 2016-06-04
  • 2017-06-17
  • 2021-12-24
  • 2020-11-08
  • 2015-05-17
相关资源
最近更新 更多