2

I'm trying to use the OpenCV library (Java version). I found some code written in C++and I'm trying to rewrite it to Java. However, I can't understand one construction.

Here is the C++ code:

void scaleDownImage(cv::Mat &originalImg,cv::Mat &scaledDownImage )
{      
    for(int x=0;x<16;x++)
    {  
        for(int y=0;y<16 ;y++)
        {
            int yd =ceil((float)(y*originalImg.cols/16));
            int xd = ceil((float)(x*originalImg.rows/16));
            scaledDownImage.at<uchar>(x,y) = originalImg.at<uchar>(xd,yd);

        }
    }
}

I can't understand how to translate this line:

scaledDownImage.at<uchar>(x,y) = originalImg.at<uchar>(xd,yd);
1
  • The generic specifier is placed before the method name in Java. Jan 15, 2014 at 16:48

3 Answers 3

3

have a look at the Mat accessor functions here: http://docs.opencv.org/java/org/opencv/core/Mat.html#get(int,%20int)

so, to translate your example:

scaledDownImage.at<uchar>(r,c) = originalImg.at<uchar>(rd,cd); // c++

would be :

byte [] pixel = new byte[1]; // byte[3] for rgb
originalImg.get( rd, cd, pixel );
scaledDownImage.put( r,c, pixel );

note, that it's (row,col), not (x,y) !

1
  • Thanks. Will try to use this.
    – Lubchak
    Jan 15, 2014 at 9:53
0

uchar = unsigned char.

This statement:

scaledDownImage.at<uchar>(x,y)

returns unsigned char(I guess, pointer) at positions(x,y).

So, in java it will be like:

unsigned char scaledDownImageChar = scaledDownImage.charAt(x, y);
scaledDownImageChar = originalImg.charAt(x, y);

This is not real code, just an example.

0

It is called template, search generics for java.

Basically, the "at" method´s code uses some type T
(don´t know if it is called T or something else)
which could be int, float, char, any class...
just something which is unspecified at the moment

And in this case, you´re calling the method with T being an uchar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.