Convert FloatImage to opencv Mat

18 views
Skip to first unread message

Ibtissam Bouazzati

unread,
Jul 10, 2017, 4:09:51 PM7/10/17
to javacv
Hi ! 
As the title says, I want to convert a FloatImage to opencv Mat using the code below :

int w = value.getWidth(); //  value is the FloatImage 
int h = value.getHeight();

Mat image = new Mat(h,w, CV_32FC3);
FloatIndexer idx = image.createIndexer();
float[] rgb = {0.0f,0.0f,0.0f};
int indice =0;
for (int i =0;i<h;i++) {
  
for (int j=0;j<w;j++) {
    rgb
[0] = value.getPixelArray().getElem(indice); 
    rgb
[1] = value.getPixelArray().getElem(indice+1);
    rgb
[2] = value.getPixelArray().getElem(indice+2);
    idx
.put(indice, rgb);
    indice 
= indice+3;
  
}
}

But I get this Error : 

Error: java.lang.IndexOutOfBoundsException: 294912
at org.bytedeco.javacpp.indexer.Indexer.checkIndex(Indexer.java:90)
at org.bytedeco.javacpp.indexer.FloatRawIndexer.put(FloatRawIndexer.java:90)
at org.bytedeco.javacpp.indexer.FloatRawIndexer.put(FloatRawIndexer.java:95)
at org.bytedeco.javacpp.indexer.FloatIndexer.put(FloatIndexer.java:114)
at org.hipi.examples.Hbasefeatures$FeaturesMapper.map(Hbasefeatures.java:84)
at org.hipi.examples.Hbasefeatures$FeaturesMapper.map(Hbasefeatures.java:56)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

Can anyone help me to resolve this ? Or have you any other idea to convert floatImage to opencv Mat ? 

Ps : I use :
  • Java  
  • HIPI 
  • opencv 3.2.0 (bytedeco)

Samuel Audet

unread,
Jul 10, 2017, 8:30:37 PM7/10/17
to Ibtissam Bouazzati, javacv
What you did should work, but try to use the 3D put method instead, like this:
    idx.put(i, j, 0, r);
    idx.put(i, j, 1, g);
    idx.put(i, j, 2, b);
It's more efficient that way anyway.

Samuel

Ibtissam Bouazzati

unread,
Jul 11, 2017, 8:55:55 AM7/11/17
to javacv, bouazzati...@gmail.com
It works with the 3D put method. 
Thank you for your answer.
Reply all
Reply to author
Forward
0 new messages