Ask Your Question
0

How to edit/reading pixels from Mat(Vec3b) using Java

asked 2013-06-11 02:56:43 -0500

orochi gravatar image

Hi guys, how to edit or reading pixels from Mat with type Vec3b using java. In c++ we can use something like this

matImage.at<Vec3b>(x,y)[0]; // for blue
matImage.at<Vec3b>(x,y)[1]; // for grey
matImage.at<Vec3b>(x,y)[2]; // for red

or we can use something like this

for(int i=0; i<A.rows; i++){
   for(int j=0; j<A.cols; j++){
           A.data[A.channels()*(A.cols*i + j) + 0] = 0;
           A.data[A.channels()*(A.cols*i + j) + 1] = 0;
           A.data[A.channels()*(A.cols*i + j) + 2] = 0;
    }
}

but how to do it using Java? i already search it, i got stuck, i still dont have clear idea how to edit/reading it in Java language.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-06-11 11:43:50 -0500

Daniil Osokin gravatar image

updated 2013-06-13 06:38:55 -0500

Hi! Try something like this:


// assume, that Mat with three channel
byte[] sourceBuffer = new byte[srcMat.total() * srcMat.channels()];
srcMat.get(0, 0, sourceBuffer);
int cols = srcMat.cols();
int rows = srcMat.rows();
for (int y = 0; y < rows; y++){
  for (int x = 0; x < cols * 3; x += 3){
    for (int channelId = 0; channelId < 3; channelId++){
          sourceBuffer[y * cols + x + channelId] = 0;
    }
  }
}
srcMat.put(0, 0, destinationBuffer);
edit flag offensive delete link more

Comments

hi @Daniil Osokin, thanks for the answer, but i've another question, for the sourceBuffer, are we only need srcMat.total()? because from this tutorial http://answers.opencv.org/question/5/how-to-get-and-modify-the-pixel-of-mat-in-java/, he multiply it with srcMat.channel(). what is the difference?

orochi gravatar imageorochi ( 2013-06-12 10:27:21 -0500 )edit

Mat.total() returns number of pixels, so here should be srcMat.total() * srcMat.channels().

Daniil Osokin gravatar imageDaniil Osokin ( 2013-06-13 06:51:23 -0500 )edit

Thank you @Daniil Osokin, if you have any good documentation or tutorial or example about developing an app using opencv and java, can you share it?

orochi gravatar imageorochi ( 2013-06-13 08:44:16 -0500 )edit

You can look through android tutorials (http://opencv.org/android). In general developing is very similar with java apps. Here the docs for java: http://docs.opencv.org/java/

Daniil Osokin gravatar imageDaniil Osokin ( 2013-06-14 02:19:40 -0500 )edit
0

answered 2013-06-11 12:03:02 -0500

gargsl gravatar image

Hey, I was stuck on this yesterday too. There are two ways of doing this. Here, I have explained both the methods -

http://answers.opencv.org/question/14961/using-get-and-put-to-access-pixel-values-in-java/

Let me know if you have any other questions.

edit flag offensive delete link more

Comments

hey @gargsl, thank you for the answer, i really appreciate it. actually i have a lot of question regarding opencv and java. if you have any good documentation or tutorial or example about developing an app using opencv and java, can you share it?

orochi gravatar imageorochi ( 2013-06-13 09:01:00 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-06-11 02:56:43 -0500

Seen: 5,491 times

Last updated: Jun 13 '13