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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:45:51+00:00 2026-06-02T01:45:51+00:00

Got the book ‘Learning OpenCV’ by O’Reilly not long ago, and since then I

  • 0

Got the book ‘Learning OpenCV’ by O’Reilly not long ago, and since then I have been busy converting all of the example code I see there from OpenCV to JavaCV, usually followed by a little of my own modification too. All the while, I’m trying to keep to the pure OpenCV (C language) code as much as possible and avoid Java. For example, I implemented all of the interface elements directly through the OpenCV highgui package in JavaCV, rather than via Java Swing. By doing this I hope to both learn the OpenCV library and some C in relatively short order, as well as establishing a library of useful functions that I will be able to convert to C easily if I decide to later switch to pure OpenCV.

Anyway, I have little knowledge of C and sometimes get into trouble when dealing with pointers. The book recommends the following code as an optimal means by which to iterate through a 3-channel matrix:

float sum( const CvMat* mat ) {
    float s = 0.0f;
    for(int row=0; row<mat->rows; row++ ) {
        const float* ptr = (const float*)(mat->data.ptr + row * mat->step);
        for( col=0; col<mat->cols; col++ ) {
            s += *ptr++;
        }
    }
    return( s );
}

Here is the included explanation for this code:

When computing the pointer into the matrix, remember that the matrix element data
is a union. Therefore, when de-referencing this pointer, you must indicate the correct
element of the union in order to obtain the correct pointer type. Th en, to off set that
pointer, you must use the step element of the matrix. As noted previously, the step
element is in bytes. To be safe, it is best to do your pointer arithmetic in bytes and > then cast to the appropriate type, in this case float. Although the CVMat structure has > the concept of height and width for compatibility with the older IplImage structure, we > use the more up-to-date rows and cols instead. Finally, note that we recompute ptr for > every row rather than simply starting at the beginning and then incrementing that pointer
every read. Th is might seem excessive, but because the CvMat data pointer could just
point to an ROI within a larger array, there is no guarantee that the data will be > contiguous across rows.

However I am having trouble converting it to JavaCV. The ptr field (pointer) seems to be a float, which confuses me. I presume that it’s not actually a ‘pointer’, but rather a value to which the value of each pixel is added? Or is it actually a pointer, which the s value finds the total sum of for all columns within a given row?

Anyway, I would be appreciative if someone were to post for me some JavaCV code for an equivelent loop. I know there are other ways of accessing every pixel in a CvMat, but AFAIK they are all less efficient or inaccurate.

  • 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-02T01:45:53+00:00Added an answer on June 2, 2026 at 1:45 am

    The particular example you provide would be optimally converted to Java as

    float sum(CvMat mat) {
        final int rows = mat.rows();
        final int cols = mat.cols();
        final int step = mat.step()/4;
        FloatBuffer buf = mat.getFloatBuffer();
        float s = 0.0f;
        for (int row = 0; row < rows; row++) {
            buf.position(row * step);
            for (int col = 0; col< cols; col++) {
                s += buf.get();
            }
        }
        return s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with web2py following its official web2py book and got to
Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
I am learning ruby from a book I got called The Pragmatic Programmers Guide
Got into confusion I have to do book shelf for tablet. Which holds books
I am learning JS currently with a js book. A rollover effects example got
I got a book for python 2.5 but up until now I've been using
I've got a very long text (an HTML page actually) (almost an entire book)
A few days ago, I decided to start learning C#. So, I got a
I'm learning queues from a book. I've got into a problem while learning circular
I have something like below. Book name (can be variable length, got from an

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.