2
votes

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 < 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]));}}}}

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 < 1 and pColumn < 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 < sourceMatrix.length; sRow++) {
    for (int sColumn = 0; sColumn < 1; sColumn++) {
         nIntensity += (1 - abs(patternMatrix[sRow][sColumn] - sourceMatrix[sRow][sColumn]));
         System.out.println("Result: " + 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
How big is the patternMatrix? If it's 56x56 the last item would actually be patternMatrix[55][55] since the first item is patternMatrix[0][0] (and not [1][1]).JensV
Clarification to my first comment: the sourceMatrix is not allowed to be bigger than the pattern matrix since you are iterating over the sourceMatrix length and will go out of bounds on the patternMatrixJensV
Its based on the size of the pattern image, in this case its 56x96 for both pattern and source matrices.Erunis
The bounds check in the solution of Arvind is a good check. But if what you're saying is true about the dimensions something is off. Are you sure that they both have exactly that size? You can check that by using a debugger or logging the .length values. Maybe one of the matrices if flipped or something.JensV
I am actually using two exactly same images for both pattern and source that get transformed into the matrices, but I will check if something didnt go wrong with them for sure, thanks!Erunis

1 Answers

2
votes

You have nested the loops in a wrong way.

Replace

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]));}}}}

with

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]));
    }
}

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

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);}}

with

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);
    }
}