繁体   English   中英

使用JavaCV API访问Mat的像素值

[英]Access to the pixel value of a Mat using the JavaCV API

我最近从OpenCV C ++ API切换到JavaCV,我正在尝试执行基本操作,例如迭代Mat。 我正在尝试访问Mat的像素值,但我似乎无法找到方法,而JavaCV项目缺少文档。 使用OpenCV C ++ API,我曾经使用.at()方法访问Mat的像素值。

Mat加载为CV_8UC1 Mat(灰度),如下面的代码所示,我想打印/使用像素的0-255值。

    Mat image = imread("images/Text00.png", CV_8UC1);

    // Make sure it was successfully loaded.
    if (image == null) {
        System.out.println("Image not found!");
        System.exit(1);
    }

    System.out.println(image.rows());
    System.out.println(image.cols());

    for (int y = 0; y < image.rows(); y++) {

        for (int x = 0; x < image.cols(); x++) {

            // How to print the 0 to 255 value of each pixel in the Mat image.
        }
    }

类似但不适用的答案:

关于JavaCV GitHub讨论中看似无关的线程 ,我在谷歌搜索一天后找到了我的问题的答案。 请注意,可能有其他更有效的方法来做到这一点,但这是我目前发现的唯一方法。 该解决方案由JavaCV提供的新“索引器”包表示(有关更多详细信息,请参阅此内容内容)。

它的用法非常简单:在声明像DoubleIndexer idx = Mat.createIndexer()你可以调用idx.get(i, j)来更容易地获取矩阵的元素。

这是我更新的代码(你可以看到,我使用了UByteBufferIndexer,因为我的Mat是CV_8UC1 Mat):

    Mat image = imread("images/Text00.png", CV_8UC1);

    // Make sure it was successfully loaded.
    if (image == null) {
        System.out.println("Image not found!");
        System.exit(1);
    }

    UByteBufferIndexer sI = image.createIndexer();

    for (int y = 0; y < image.rows(); y++) {

        for (int x = 0; x < image.cols(); x++) {

            System.out.println( sI.get(y, x) );
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM