OpenCV的转换矩阵循环到JavaCV [英] Converting OpenCV matrix looping to JavaCV

查看:304
本文介绍了OpenCV的转换矩阵循环到JavaCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由O'Reilly得到这本书的学习OpenCV的时间不长,从那以后,我一直忙着将所有的例子code我看到有来自OpenCV的到JavaCV,通常后面一点点我自己的修饰过。在这期间,我试图保持纯净的OpenCV(C语言)code尽可能避免的Java。例如,我实现所有的界面元素直接通过OpenCV的highgui包JavaCV,而不是通过Java Swing的。通过这样做,我希望这两个学习OpenCV库,并在相对较短的一些C,以及建立有用的函数库,我就能当我决定后切换到纯OpenCV的转换为C容易。

Got the book 'Learning OpenCV' by O'Reilly not long ago, and since then I have been busy converting all of the example code I see there from OpenCV to JavaCV, usually followed by a little of my own modification too. All the while, I'm trying to keep to the pure OpenCV (C language) code as much as possible and avoid Java. For example, I implemented all of the interface elements directly through the OpenCV highgui package in JavaCV, rather than via Java Swing. By doing this I hope to both learn the OpenCV library and some C in relatively short order, as well as establishing a library of useful functions that I will be able to convert to C easily if I decide to later switch to pure OpenCV.

不过,我对C知之甚少,并与指针打交道时,有时会遇到麻烦。该书提出以下建议code作为一个最佳的途径,由通过3信道矩阵遍历:

Anyway, I have little knowledge of C and sometimes get into trouble when dealing with pointers. The book recommends the following code as an optimal means by which to iterate through a 3-channel matrix:

float sum( const CvMat* mat ) {
    float s = 0.0f;
    for(int row=0; row<mat->rows; row++ ) {
        const float* ptr = (const float*)(mat->data.ptr + row * mat->step);
        for( col=0; col<mat->cols; col++ ) {
            s += *ptr++;
        }
    }
    return( s );
}

下面是此code所包含的解释是:

Here is the included explanation for this code:

在计算指针到矩阵,记住矩阵元数据
  是工会。因此,去的时候引用这个指针,必须指明正确的
  为了获得正确的指针类型的工会的元素。 TH EN,为关闭设置
  指针,则必须使用矩阵的步骤元素。正如所指出的previously,步骤
  元素是以字节为单位。为了安全起见,最好是做以字节为单位的指针算术和>然后转换为合适的类型,在这种情况下浮动。虽然结构与CvMat有>的高度和宽度与旧的IplImage结构兼容性的概念,我们>使用越涨最新行列数代替。最后请注意,我们重新计算PTR为>每一行,而不是简单地从头开始,然后递增该指针
  每次读。钍是似乎过多,但由于数据与CvMat指针可以只
  点到一个更大的阵列中的ROI,也不能保证该数据将>连续跨越行

When computing the pointer into the matrix, remember that the matrix element data is a union. Therefore, when de-referencing this pointer, you must indicate the correct element of the union in order to obtain the correct pointer type. Th en, to off set that pointer, you must use the step element of the matrix. As noted previously, the step element is in bytes. To be safe, it is best to do your pointer arithmetic in bytes and > then cast to the appropriate type, in this case float. Although the CVMat structure has > the concept of height and width for compatibility with the older IplImage structure, we > use the more up-to-date rows and cols instead. Finally, note that we recompute ptr for > every row rather than simply starting at the beginning and then incrementing that pointer every read. Th is might seem excessive, but because the CvMat data pointer could just point to an ROI within a larger array, there is no guarantee that the data will be > contiguous across rows.

但我无法将其转换为JavaCV。本场PTR(指针)似乎是一个浮动,这混淆了我。我presume,它也不是一个'指针',而是其中添加的每个像素值的价值?或者,它实际上是一个指针,其价值发现一个给定行内的所有列的总和?

However I am having trouble converting it to JavaCV. The ptr field (pointer) seems to be a float, which confuses me. I presume that it's not actually a 'pointer', but rather a value to which the value of each pixel is added? Or is it actually a pointer, which the s value finds the total sum of for all columns within a given row?

无论如何,我会AP preciative如果有人张贴了我一些JavaCV code为equivelent循环。我知道有一个访问CvMat中的每一个像素的其他方式,但据我所知,他们都是低效率的或不准确的。

Anyway, I would be appreciative if someone were to post for me some JavaCV code for an equivelent loop. I know there are other ways of accessing every pixel in a CvMat, but AFAIK they are all less efficient or inaccurate.

推荐答案

您所提供的特定例子是最佳转换为Java为

The particular example you provide would be optimally converted to Java as

float sum(CvMat mat) {
    final int rows = mat.rows();
    final int cols = mat.cols();
    final int step = mat.step()/4;
    FloatBuffer buf = mat.getFloatBuffer();
    float s = 0.0f;
    for (int row = 0; row < rows; row++) {
        buf.position(row * step);
        for (int col = 0; col< cols; col++) {
            s += buf.get();
        }
    }
    return s;
}

这篇关于OpenCV的转换矩阵循环到JavaCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆