First time here? Check out the FAQ!

Ask Your Question
1

How to use norm with Java?

asked Aug 29 '18

accdev gravatar image

I have a code that is written in both Python and C++ that I am now converting to Java. I use the norm function to compare 2 points of an RGB image. In Python and C++ getting these pixel values is simple, and you can just plug them into the norm function. In java, when getting these values the data type changes from Mat to double[] array. The norm function however takes Mat as its arguments. Is it possible to convert the double array to Mat, or to get the pixel values as a Mat?

This is the current methods I am using:

double[] right_pt = image_to_read.get(middlexint+radiint, middleyint); // get pixel value point1
double[] pti = image_to_read.get(middlexint+radiint-i,middleyint);    // get pixel value point2
double e_dist = Core.norm(right_pt, pti);  // calculate euclidean distance
Preview: (hide)

1 answer

Sort by » oldest newest most voted
2

answered Aug 30 '18

berak gravatar image

updated Aug 30 '18

well, the java wrappers are indeed somewhat clumsy here (compared to c++),

but you could either work around it with 1-pixel Mat's like:

Mat left  = img.submat(yleft,  yleft+1,  xleft,  xleft+1);
Mat right = img.submat(yright, yright+1, xright, xright+1);
double dist = Core.norm(left, right);

or just compute it manually:

double db = right_pt[0] - pti[0];
double dg = right_pt[1] - pti[1];
double dr = right_pt[2] - pti[2];
double dist = Math.sqrt(db*db + dg*dg + dr*dr);
Preview: (hide)

Question Tools

1 follower

Stats

Asked: Aug 29 '18

Seen: 2,587 times

Last updated: Aug 30 '18