Ask Your Question
2

Round/Absolute Value each element of Mat [closed]

asked 2016-07-13 12:36:36 -0600

phillity gravatar image

I am new to opencv and am trying to convert this line of matlab code to java using opencv 3.1:

  • user_feature_matrix_normalize = abs(round(user_feature_matrix.*100)); //matlab code

I think I figured out multiplication and this is what I have so far:

  • Mat user_feature_matrix_normalize = new Mat(); //java opencv code
  • Scalar alpha =new Scalar(100);
  • Core.multiply(user_feature_matrix, alpha, user_feature_matrix_normalize);

Are there any opencv functions I can use to round and take the absolute value of each element of the mat? Looking for any suggestions on how I should try to accomplish this

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by phillity
close date 2016-07-18 12:03:54.026797

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-07-13 17:45:53 -0600

Tetragramm gravatar image

Doing the absolute value is easy, just cv::abs(src, dst). Rounding is slightly harder. You can either iterate over every pixel and use cvRound(), but it's probably better to temporarily go from float to int and then back.

I suggest using convertTo

cv::abs(user_feature_matrix, user_feature_matrix);
user_feature_matrix.convertTo(user_feature_matrix, CV_32S, 100, 0.5);

What this does, is it multiplies the matrix by 100, then adds 0.5, and then converts it to an int. This takes advantage of the fact that rounding is (int)(number+0.5). From here you can convert it back to a float, or just keep using it as an int if that works for you.

edit flag offensive delete link more

Comments

Thank you, this helped a lot!

phillity gravatar imagephillity ( 2016-07-18 12:03:45 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-07-13 12:36:36 -0600

Seen: 11,434 times

Last updated: Jul 13 '16