rect.contain()没有显示正确的结果

时间:2015-04-30 15:06:43

标签: opencv image-processing javacv

在下面的代码中我试图测试一个点是否在矩形内。我创建了“Rect rect = new Rect(2,2,3,3);”据我所知,这个矩形从点2,2到4,4开始,这意味着它应该 包含9个元素。实际上我有9个元素,但为什么我不打算从2,2到4,4

开始

我希望输出为:

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

    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

1 个答案:

答案 0 :(得分:1)

根据您的打印图像大小(7x7),但每行21个元素的转储矩阵数据,您可能有一个3通道数据类型,这意味着您的矩阵在第一个通道(.get(y,x)[0] )看起来像

[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]

你在rect中的打印元素(x = 2,y = 2,width = 3,height = 3)正是预期的。

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

所以关于你的3通道垫,你没有覆盖3 * 3值,但3 * 3 * 3 = 27值!这是3 * 3个元素,每个元素有3个通道/值(例如RGB值)。