How to iterate through a cvMat matrix in JavaCV?

与世无争的帅哥 提交于 2020-01-01 12:16:12

问题


I have an IplImage that I converted in a Matrix, and now I want to iterate cell by cell.

CvMat mtx = new CvMat(iplUltima);
for (int i = 0; i < 100; i++) {
     //I need something like mtx[0][i] = someValue;
}

回答1:


¡¡I DID IT!! I share it:

CvMat mtx = new CvMat(iplUltima);   

for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
         opencv_core.cvSet2D(mtx, i, j, CvScalar.ONE);
    }
}
iplUltima = new IplImage (mtx); 

Where i = row and j = column




回答2:


First, you need to import the following from JavaCV:

import com.googlecode.javacv.cpp.opencv_core.CvMat;

import static com.googlecode.javacv.cpp.opencv_core.CV_32F;

Main Program:

int rows = 2;
int cols = 2;

CvMat Tab = CvMat.create( rows, cols, CV_32F );

// Manually fill the table
Tab.put(0, 0, 1);
Tab.put(0, 1, 2);
Tab.put(1, 0, -3);
Tab.put(1, 1, 4);

// Iterate through its elements and print them 
for(int i=0;i<rows;i++){
   for (int j =0;j<cols;j++){
    System.out.print(" "+ Tab.get(i,j) );
    }
   System.out.println("\n");
}



回答3:


I don't have Java installed, I can't check this solution, but I think it should work fine.

CvMat mtx = new CvMat(iplUltima);
val n     = mtx.rows * mtx.cols * mtx.channels

for (i <- 0 until n) {
    // Put your pixel value, for example 200
    mtx.put(i, 200)
}

Here is the reference about pixel access in javaCV.



来源:https://stackoverflow.com/questions/10641292/how-to-iterate-through-a-cvmat-matrix-in-javacv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!