Search code examples
opencvimage-processingjavacv

rect.contain() does not show correct results


in the below code i am trying to test if a point is inside a rectangle. i created "Rect rect = new Rect(2, 2, 3, 3);" and as far as i know, the this rectangle is starts from point 2,2 to 4,4 which means it should contain 9 elements. actually i got 9 elements but why i am not geeting the elements starting from 2,2 upto 4,4

i expected the output to be:

27, 37, 37
49, 58, 58
75, 79, 79

code:

    Rect rect = new Rect(2, 2, 3, 3);

    for (int i = 0; i < gsMat.height(); i++) {
        for (int j = 0; j < gsMat.width(); j++) {
            Point pnt = new Point(j, i);
            if (pnt.inside(rect)) {

                Log.D(TAG, "main", "outside : " + pnt);
                Log.D(TAG, "main", "outside : " + gsMat.get(pnt.y, pnt.x)[0] );
            }
        }
    }

    5: Info: MainClass -> MainClass: image.Size(): 7x7

    6: Info: MainClass -> MainClass: image.dump(): 
    [23, 23, 23, 26, 26, 26, 33, 33, 33, 49, 49, 49, 74, 74, 74, 80, 80, 80, 70, 70, 70;
    22, 22, 22, 28, 28, 28, 45, 45, 45, 69, 69, 69, 82, 82, 82, 70, 70, 70, 61, 61, 61;
    27, 27, 27, 37, 37, 37, 65, 65, 65, 96, 96, 96, 97, 97, 97, 68, 68, 68, 62, 62, 62;
    49, 49, 49, 58, 58, 58, 79, 79, 79, 102, 102, 102, 100, 100, 100, 75, 75, 75, 77, 77, 77;
    75, 75, 75, 79, 79, 79, 83, 83, 83, 91, 91, 91, 97, 97, 97, 95, 95, 95, 105, 105, 105;
    81, 81, 81, 89, 89, 89, 89, 89, 89, 95, 95, 95, 114, 114, 114, 130, 130, 130, 140, 140, 140;
    77, 77, 77, 89, 89, 89, 95, 95, 95, 105, 105, 105, 131, 131, 131, 153, 153, 153, 161, 161, 161]
    7: Debug: MainClass -> main: outside : {2.0, 2.0}
    8: Debug: MainClass -> main: outside : 65.0
    9: Debug: MainClass -> main: outside : {3.0, 2.0}
    10: Debug: MainClass -> main: outside : 96.0
    11: Debug: MainClass -> main: outside : {4.0, 2.0}
    12: Debug: MainClass -> main: outside : 97.0
    13: Debug: MainClass -> main: outside : {2.0, 3.0}
    14: Debug: MainClass -> main: outside : 79.0
    15: Debug: MainClass -> main: outside : {3.0, 3.0}
    16: Debug: MainClass -> main: outside : 102.0
    17: Debug: MainClass -> main: outside : {4.0, 3.0}
    18: Debug: MainClass -> main: outside : 100.0
    19: Debug: MainClass -> main: outside : {2.0, 4.0}
    20: Debug: MainClass -> main: outside : 83.0
    21: Debug: MainClass -> main: outside : {3.0, 4.0}
    22: Debug: MainClass -> main: outside : 91.0
    23: Debug: MainClass -> main: outside : {4.0, 4.0}
    24: Debug: MainClass -> main: outside : 97.0

Solution

  • according to your printed image size (7x7) but your dumped matrix data of 21 elements per row, you probably have a 3 channel data type which means your matrix in the first channel (.get(y,x)[0] )looks like

    [23, 26, 33, 49, 74, 80, 70;
    22, 28, 45, 69, 82; 70, 61;
    27, 37, 65, 96, 97, 68, 62;
    49, 58, 79, 102, 100, 75, 77
    75, 79, 83, 91, 97, 95, 105;
    81, 89, 89, 95, 114, 130, 140;
    77, 89, 95, 105, 131, 153, 161]
    

    where your printed elements in the rect (x=2, y=2, width=3, height=3) are exactly what is expected.

    [65, 96, 97;
    79, 102, 100;
    83, 91, 97]
    

    so regarding your 3 channel mat, you dont have covered 3*3 values but 3*3*3 = 27 values! which is 3*3 elements with 3 channels/values (e.g. RGB values) each.