I am starting to use OpenCV for JAVA. I want to access individual pixel values ββof an image matrix. Since the JAVA jar for OpenCV does not offer nice features like C ++, I ran into some problems. After many searches, I found two different methods, although they are not explained properly (even in the documentation). We can do this either using the get () and put () functions, or by converting the mat data into a primitive java type, such as arrays. I tried both, but got different output results! Please help explain what I'm doing wrong. I use them incorrectly or some other stupid problem. I'm still a beginner, so please excuse me if this is a stupid question. :)
CASE 1: Using the 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 an 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 have to do the same. They both access individual pixel values ββ(all 3 channels) and make up half. After starting, I get no error. But the output images that I get are different in these two cases. Can someone explain what the problem is? Maybe I donβt understand how the get () function works? Is it because of byte casting ()? Please, help.
Thanks!
java android image-processing opencv computer-vision
gargsl
source share