Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:10:26+00:00 2026-06-04T15:10:26+00:00

I’m currently trying to migrate a bit of legacy code from iPhone to Android.

  • 0

I’m currently trying to migrate a bit of legacy code from iPhone to Android. This code uses the OpenCV library to do some image processing. Overall it goes well, but I’m stuck on one line of code I have no idea how can be converted into Java code:

Scalar dMean;
Scalar scalar;
std::vector<Mat> channels;
split(mat, channels);
for(int i=0; i<3; ++i) {
   channels[i] += dMean[i];
}

The question is – what should be used instead of the += operator in Java code to add a Scalar object to a Mat?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:10:28+00:00Added an answer on June 4, 2026 at 3:10 pm

    Note: take this answer with a grain of salt, I haven’t fully tested this 😉

    OPTION1:

    The most direct way, and if you are only going to process a few pixels, is by using your_mat.put(row, col, data) and your_mat.get(row, col).

    Because the put() method does not accept Scalar objects as the data parameter, you have to convert the Scalar to something that put() accepts.

    So if your Scalar is (1,2,3) maybe an int array int[] scalar = {1,2,3}; should do the trick.

    int[] scalar = ... // convert from Scalar object
    
    // assuming the result of get() is an int[], sum both arrays:
    int[] data = your_mat.get(row, col) + scalar // <- pseudo-code alert :D
    
    your_mat.put(row, col, data);
    

    OPTION2:

    But the recommended way, for a lot of pixel processing, is to first convert a Mat to a Java primitive, process the primitive and then convert it back to Mat. This is to avoid too many JNI calls, this method does 2 JNI calls while the former makes one per put/get.

    The corresponding Java primitive array type depends on the Mat type:

    CV_8U and CV_8S -> byte[];
    CV_16U and CV_16S -> short[];
    CV_32S -> int[];
    CV_32F -> float[];
    CV_64F-> double[];
    

    So the code will be something like this:

    // assuming Mat it's of CV_32S type
    int buff[] = new int[your_mat.total() * your_mat.channels()];
    
    your_mat.get(0, 0, buff);
    
    // buff is now Mat converted to int[]
    
    your_mat.put(0, 0, buff); // after processing buff, convert it back to Mat type
    

    OPTION 3:

    Ok so those solutions are pretty ugly, this one is not the most effective but it’s a little less ugly, in a Java way:

    List<Integer> scalarList = ... // your conversion from a Scalar to a List
    List<Integer> channelsList = new ArrayList<Integer>();
    
    Converters.Mat_to_vector_int(channels, channelsList); // this is an existing OpenCV4Android converter
    
    // process channelsList and scalarList, store in channelsList
    
    channels = Converters.vector_int_to_Mat(channelsList); // after processing, convert it back to Mat type
    

    Now that I think about it option 3 is quite similar to option 2, if OpenCV’s Converters work similar internally as the option 2 conversion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.