【问题标题】:Looping through two matrices in java在java中循环遍历两个矩阵
【发布时间】:2020-04-10 11:25:46
【问题描述】:

我在 Java 和一般编程方面是一个新手,我有一个项目,用于从 Android 的图像中识别文本。我在 Android Studio 中工作,目前我有两个矩阵——一个是从设备库中获取的源图像的值,一个是模板图像的值。矩阵中的值基本上是像素的颜色,以灰度工作,分为6个区间:255-214的颜色值等于0,172-213的颜色值等于0.2,129-171的颜色值等于0.4等。

我需要在两个矩阵中逐行进行并对其进行一些计数 - 在源矩阵的第一行中取第一个值,在模板矩阵的第一行中取第一个值并在公式中使用它:

nIntensity = 1 - abs(template_value - source_value)

并对一行中的所有值执行此操作,然后对这些值求和并转到第二行并执行相同操作(导致 x 矩阵行的 x nIntensity 值数组)。我一开始尝试的是使用嵌套循环,但我用它们有点过头了:

for(int sRow = 0; sRow < sourceMatrix.length; sRow++) {
    for(int sColumn = 0; sColumn < 1; sColumn++) {
        for(int pRow = 0; pRow < patternMatrix.length; pRow++) {
            for(int pColumn = 0; pColumn < 1; pColumn++) {
                nIntensity += (1 - abs(patternMatrix[pRow][pColumn] - sourceMatrix[sRow][sColumn]));}}}}

这导致 nIntensity 变量的值太高 - 对于 sourceMatrix 和 patternMatrix 有 56 个值等于 1.0 我应该得到数字 56 的结果,而不是我得到 3192,这意味着它通过所有的次数太多循环。使用sColumn &lt; 1pColumn &lt; 1,我试图从一开始就变得简单,只取两个矩阵的第一行,然后再深入研究并使用多行。

在做了一些研究之后,我试着用代码让它变得非常简单:

for(int sRow = 0; sRow < sourceMatrix.length; sRow++) {
    for (int sColumn = 0; sColumn < 1; sColumn++) {
         nIntensity += (1 - abs(patternMatrix[sRow][sColumn] - sourceMatrix[sRow][sColumn]));
         System.out.println("Result: " + nIntensity);}}

当我在每个循环后打印结果实际上得到结果 56 时,却因异常而崩溃

原因:java.lang.ArrayIndexOutOfBoundsException:length=56;索引=56

【问题讨论】:

  • patternMatrix 有多大?如果是 56x56,则最后一项实际上是 patternMatrix[55][55],因为第一项是 patternMatrix[0][0](而不是 [1][1])。
  • 澄清我的第一条评论:sourceMatrix 不允许大于模式矩阵,因为您正在迭代 sourceMatrix 长度并且会超出模式矩阵的范围
  • 它基于图案图像的大小,在这种情况下,它对于图案和源矩阵都是 56x96。
  • Arvind 解决方案中的边界检查是一个很好的检查。但是,如果您所说的有关尺寸的内容是正确的,则有些事情是错误的。你确定它们的尺寸都一样吗?您可以通过使用调试器或记录.length 值来检查。如果翻转或其他东西,可能是其中一个矩阵。
  • 我实际上使用了两个完全相同的图像来转换为矩阵的模式和源,但我会检查它们是否有问题,谢谢!

标签: java matrix


【解决方案1】:

您以错误的方式嵌套循环。

替换

for(int sRow = 0; sRow < sourceMatrix.length; sRow++) {
    for(int sColumn = 0; sColumn < 1; sColumn++) {
        for(int pRow = 0; pRow < patternMatrix.length; pRow++) {
            for(int pColumn = 0; pColumn < 1; pColumn++) {
                nIntensity += (1 - abs(patternMatrix[pRow][pColumn] - sourceMatrix[sRow][sColumn]));}}}}

for(int sRow = 0, pRow=0; sRow < sourceMatrix.length && pRow < patternMatrix.length; sRow++, pRow++) {
    for(int sColumn = 0; sColumn < 1; sColumn++) {
        nIntensity += (1 - abs(patternMatrix[pRow][pColumn] - sourceMatrix[sRow][sColumn]));
    }
}

此外,在以下代码块中,您没有检查patternMatrix 的边界。替换此代码块

for(int sRow = 0; sRow < sourceMatrix.length; sRow++) {
    for (int sColumn = 0; sColumn < 1; sColumn++) {
         nIntensity += (1 - abs(patternMatrix[sRow][sColumn] - sourceMatrix[sRow][sColumn]));
         System.out.println("Result: " + nIntensity);}}

for(int sRow = 0; sRow < sourceMatrix.length && sRow < patternMatrix.length; sRow++) {
    for (int sColumn = 0; sColumn < 1; sColumn++) {
         nIntensity += (1 - abs(patternMatrix[sRow][sColumn] - sourceMatrix[sRow][sColumn]));
         System.out.println("Result: " + nIntensity);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2010-11-22
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多