循环遍历两个矩阵在Java中。

huangapple go评论76阅读模式
英文:

Looping through two matrices in java

问题

我在Java和编程方面都是一个新手,对于Android上的图像文本识别项目也不太了解。我正在使用Android Studio进行开发,目前我有两个矩阵 - 一个带有来自设备图库的源图像值,另一个带有模板图像的值。矩阵中的值基本上是像素的颜色,以灰度工作,分为6个区间:颜色值从255到214等于0,颜色值从172到213等于0.2,颜色值从129到171等于0.4,依此类推。

我需要逐行遍历这两个矩阵,并对它们进行一些计算 - 取源矩阵第一行的第一个值和模板矩阵第一行的第一个值,然后在公式中使用:

nIntensity = 1 - abs(template_value - source_value)

对于一行中的所有值都要这样做,然后对这些值求和,然后转到第二行并重复相同的过程(最终得到一个包含x个nIntensity值的数组,其中x是矩阵的行数)。我起初尝试使用嵌套循环,但是我用得有点过头了:

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变量的值过高 - 对于源矩阵和模板矩阵中值均为56的行,我应该得到56,但实际上我得到了3192,这意味着它在所有循环中执行了太多次。通过sColumn < 1pColumn < 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,但是会因为异常而崩溃:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=56; index=56

英文:

I am quite a newbie in java and programming in general and I have a project for text recognition from images for Android. I am working in Android Studio and currently I have two matrices - one with values for source image taken from device gallery and one with values for template image. The values in matrices are basically the colors of pixels, working in grayscale, devided into 6 intervals: color value of 255-214 equals 0, color value of 172-213 equals 0.2, color value of 129-171 equals 0.4 etc.

I need to go line by line in both of the matrices and do some countings with them - take first value in first line of source matrix and first value in first line of template matrix and use it in formula:

nIntensity = 1 - abs(template_value - source_value)

and do this for all the values in one line, then do sum of these values and go to second line and do the same (resulting in an array of x nIntensity values for x matrix lines). What I tried at first was using nested loops, but I kinda overdid it with them:

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

which resulted in too high values for the nIntensity varriable - with 56 values equal to 1.0 in line for both sourceMatrix and patternMatrix I should get the result of number 56, instead I got 3192, implying it goes too many times through all the loops. With the sColumn &lt; 1 and pColumn &lt; 1 I tried to achieve going simple from the start and taking just the first line of both the matrices, before I dig deeper in it and work with multiple lines.

After doing some research, I tried to make it quite simple with code:

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

which when I print the results after every loop actually gets to result 56, however crashes with the exception

> Caused by: java.lang.ArrayIndexOutOfBoundsException: length=56; index=56

答案1

得分: 2

你错误地嵌套了循环。

替换为:

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 < patternMatrix.length; sRow++) {
    for (int sColumn = 0; sColumn < 1; sColumn++) {
         nIntensity += (1 - abs(patternMatrix[sRow][sColumn] - sourceMatrix[sRow][sColumn]));
         System.out.println("Result: " + nIntensity);
    }
}
英文:

You have nested the loops in a wrong way.

Replace

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

with

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

Also, in the following code block, you are not checking bounds for patternMatrix. Replace this code block

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

with

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

huangapple
  • 本文由 发表于 2020年4月10日 19:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定