4

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;
}
gal007
  • 6,911
  • 8
  • 47
  • 70
  • this may help you: http://stackoverflow.com/questions/9920449/converting-opencv-matrix-looping-to-javacv – Larry Foobar May 17 '12 at 21:43
  • Thanks! But in the example iterate a FloatBuffer not the CvMat. Wath if I need to assign a value tu a matrix position? – gal007 May 17 '12 at 23:24

3 Answers3

3

¡¡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

gal007
  • 6,911
  • 8
  • 47
  • 70
  • Please tell how to change it into CvScalar.RED or something else which mean 0-255 .I did this but didn't change it into green or red any color what I want – GPrathap Nov 22 '14 at 03:24
  • Hi! Sorry! I never again returned to program with this tecnology, so I don't remember very much. However, I remember a good book: http://books.google.com.ar/books?id=seAgiOfu2EIC&pg=PA209&lpg=PA209&dq=cvSet2D+opencv&source=bl&ots=hTE6bmeCMi&sig=Y8BwgDQOmtzcOuTLga2S0mkDFak&hl=es-419&sa=X&ei=zbdwVL-WHMKkgwT35YOIDQ&ved=0CFMQ6AEwBg#v=onepage&q=cvSet2D%20opencv&f=false – gal007 Nov 22 '14 at 16:26
  • Thank you, I'll figure it out – GPrathap Nov 23 '14 at 01:07
1

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");
}
VaSko
  • 29
  • 3
-1

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.

Larry Foobar
  • 11,092
  • 15
  • 56
  • 89