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 7174023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:56:48+00:00 2026-05-28T15:56:48+00:00

I’m trying to do efficiently convert a BufferedImage to an IplImage… could you give

  • 0

I’m trying to do efficiently convert a BufferedImage to an IplImage… could you give me any hint about the jni part?

Right now I do these steps:

I get the rgbs from the BufferedImage and I send them to the jni code where I do the following:

IplImage* getIplImageFromIntArray(JNIEnv* env, jintArray array_data,
    jint width, jint height) {
int *pixels = env->GetIntArrayElements(array_data, 0);
if (pixels == 0) {      
    return 0;
}
IplImage *image = loadPixels(pixels, width, height);
env->ReleaseIntArrayElements(array_data, pixels, 0);
if (image == 0) {   
    return 0;
}
return image;}};

and

IplImage* loadPixels(int* pixels, int width, int height) {
int x, y;
IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
unsigned char* base = (unsigned char*) (img->imageData);
unsigned char* ptr;
for (y = 0; y < height; y++) {
    ptr = base + y * img->widthStep;
    for (x = 0; x < width; x++) {
        // blue
        ptr[3 * x] = pixels[x + y * width] & 0xFF;
        // green
        ptr[3 * x + 1] = pixels[x + y * width] >> 8 & 0xFF;
        // blue
        ptr[3 * x + 2] = pixels[x + y * width] >> 16 & 0xFF;
    }
}
return img;}

But that is really slow… thank you for your help!

  • 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-05-28T15:56:49+00:00Added an answer on May 28, 2026 at 3:56 pm

    With JavaCV:

    IplImage.createFrom(aBufferedImage)
    

    EDIT: There are a couple of reasons why your code is slow. The BufferedImage class was designed before NIO buffers came into fashion, so it uses standard Java arrays as backing buffer. Java arrays cannot be (safely) accessed directly from JNI, so by default, with the way you call GetIntArrayElements(), memory gets temporarily allocated and the array data gets copied into that new allocated memory, while ReleaseIntArrayElements() also copies back the data into the array, and frees the temporary allocated memory. And you also allocate a new IplImage on every call and make another copy when copying everything from that temporary buffer to the temporary IplImage. In a nutshell, your code allocates memory twice on the heap and copies the data three times. The recommended way to achieve the desired effect is instead to copy the data only once from inside Java, using direct NIO buffers. You can check what this looks like by peering into the relevant portion of the JavaCV’s source code, namely IplImage.copyFrom(BufferedImage):

    http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/googlecode/javacv/cpp/opencv_core.java#881

    It branches into a lot of special cases, but basically it loops over all pixels to produce the copy. Adding a couple of threads to loop in parallel on multiple cores could also improve performance further…

    After copying the data from Java, you may then use it directly from your native code if desired.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.