Ask Your Question

gargsl's profile - activity

2016-05-20 04:12:48 -0600 received badge  Nice Question (source)
2015-06-13 23:24:21 -0600 received badge  Famous Question (source)
2014-10-31 12:31:16 -0600 received badge  Notable Question (source)
2014-07-22 01:53:33 -0600 received badge  Popular Question (source)
2013-06-13 09:43:25 -0600 received badge  Student (source)
2013-06-11 12:20:59 -0600 commented question Using get() and put() to access pixel values in JAVA

One more note: Using second method is possible only if the matrix is continuous. We can check this by isContinuous() flag. Though, this will be the case most of the time, its a good idea to do a check before.

2013-06-11 12:03:02 -0600 answered a question How to edit/reading pixels from Mat(Vec3b) using Java

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.

2013-06-11 11:59:30 -0600 commented question Using get() and put() to access pixel values in JAVA

Posting the answer here as its not letting me answer my own question. It was happening because of byte() casting. I changed the data type of mat image in second case to CV_64FC3 so that I can use double[] instead of byte[] and it solved the problem.

Mat A = Highgui.imread(image_addr); \\"image_addr" is the address of the image
Mat C = A.clone();
A.convertTo(A, CvType.CV_64FC3); \\New line added. 
int size = (int) (A.total() * A.channels());
double[] temp = new double[size]; \\ use double[] instead of byte[]
A.get(0, 0, temp);
for (int i = 0; i < size; i++)
   temp[i] = (temp[i] / 2);  \\ no more casting required.
C.put(0, 0, temp);

FYI, I also did some time measurement and using second methos is way faster than first method.

2013-06-11 11:57:13 -0600 commented question Using get() and put() to access pixel values in JAVA

As far as I know, there is no other way of working directly with pixels in java openCV. But, I started working with it just two days back so might not be aware of any existing other methods. I wanted to try out both these two methods to know the performance of these two approaches. Please let me know if you know of any other easy way of doing this.

2013-06-10 19:39:25 -0600 received badge  Editor (source)
2013-06-10 19:36:12 -0600 asked a question Using get() and put() to access pixel values in JAVA

I am a beginner in using OpenCV for JAVA. I want to access individual pixel values of an image matrix. Since, JAVA jar for OpenCV doesn't offer nice functions like C++, I ran into some trouble. After lot of searching, I found out two different methods to do that though they are not explained properly (not even in documentation). We can do that either using get() and put() functions or by converting the mat data into a primitive java type such as arrays. I tried both but getting different output results! Please help explaining what am I doing wrong. Am I using them wrong or some other silly problem. I am still a newbie so please forgive if its a stupid question. :)

CASE 1: Using get() function

Mat A = Highgui.imread(image_addr); \\"image_addr" is the address of the image
Mat C = A.clone();
Size sizeA = A.size();
for (int i = 0; i < sizeA.height; i++)
    for (int j = 0; j < sizeA.width; j++) {
        double[] data = A.get(i, j);
        data[0] = data[0] / 2;
        data[1] = data[1] / 2;
        data[2] = data[2] / 2;
        C.put(i, j, data);
    }

CASE 2: Using Array

Mat A = Highgui.imread(image_addr); \\"image_addr" is the address of the image
Mat C = A.clone();
int size = (int) (A.total() * A.channels());
byte[] temp = new byte[size];
A.get(0, 0, temp);
for (int i = 0; i < size; i++)
   temp[i] = (byte) (temp[i] / 2);
C.put(0, 0, temp);

Now according to my understanding they both should do the same thing. They both access the individual pixel values (all 3 channels) and making it half. I am getting no error after running. But, the output image I am getting is different in these two cases. Can someone please explain what is the issue? May be I don't understand exactly how get() function works? Is it because of the byte() casting? Please help.

Thanks!