gpt4 book ai didi

java - 如何在Java中计算共现矩阵?

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:35 24 4
gpt4 key购买 nike

我对共现公式有疑问。如何在Java中实现图像的GLCM计算?

具体来说,我试图弄清楚如何计算一个像素具有强度x的次数以及其紧邻右侧的像素具有强度y的次数。我还需要将获得的值存储在生成的共现矩阵的第 x 行和第 y 列中。

预期行为如下所示:

GLCM Alghoritm

这是我到目前为止得到的:

代码(尚未完成)

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。

最佳答案

这段代码完成了工作:

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. 所提出的解决方案产生与位移 vector “向右一个像素”相对应的共生矩阵。如果您希望使用不同的位移 vector 计算 GLCM,则需要调整代码。例如,以下代码片段返回与“向上偏移一个像素”相对应的 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]++;
    }
    }

关于java - 如何在Java中计算共现矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52013396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com