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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:13:30+00:00 2026-05-13T01:13:30+00:00

I’m a tad confused. I am just getting started with OpenCV and its image

  • 0

I’m a tad confused.

I am just getting started with OpenCV and its image data is pointed to by a char pointer. I can’t quite work out how that works considering the actual data itself could be any number of data types, e.g. uint, float, double. As far as I knew, a pointer had to be of the same type as the pointer it represents.

It’s probably worth noting that openCV is a C library and my background is C++, so I am unaware of how these problems of needing variable types are solved in C.

For example the follwing code taken from Learning OpenCV illustrates my confusion:

void saturate_sv( IplImage* img ) {
    for( int y=0; y<img->height; y++ ) {
    uchar* ptr = (uchar*) (
    img->imageData + y * img->widthStep
    );
       for( int x=0; x<img->width; x++ ) {
           ptr[3*x+1] = 255;
           ptr[3*x+2] = 255;
       }
    }
}

So this works, but when I try to operate on a iplImage of type IPL_DEPTH_64F and use ptr[3*x+1] = 1 The results are incorrect. So to distil my problems: how can I work on integer or floating point data through char pointers and specifically how could I rectify the above example to work with a double precision data.

Thanks

  • 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-13T01:13:31+00:00Added an answer on May 13, 2026 at 1:13 am
    1. IPL_DEPTH_64F or double images will take care of the data from 0 – 1.
    2. If you’re used to C++ you should check out OpenCV2.0 which has several C++ classes and most importantly, one class, i.e. Mat to handle images, matrices, etc.

    Here’s a simple way to access elements in your image efficiently:

    IplImage* img = cvCreateImage(cvSize(300,300),IPL_DEPTH_64F,1);
    for( int y=0; y<img->height; y++ ) 
        {
           double* ptr = reinterpret_cast<double*>(img->imageData + y * img->widthStep);
           for( int x=0; x<img->width; x++ ) 
           {
              ptr[x] = double(255);
           }
        }
    cvNamedWindow("SO");
    cvShowImage("SO",img);
    cvWaitKey();
    cvDestroyAllWindows();
    cvReleaseImage(&img);
    

    Since you’re working with a double image, it makes more sense to:

    1. Work with a double pointer so you can easily assign elements in a row with ptr[x]
    2. Do the pointer arithmetic in bytes (img->imageData + y * img->widthStep) and the cast it to a double pointer

    Also, it’s important that you do the pointer arithmetic in bytes (or uchar, i.e. unsigned char) since OpenCV tends to pad the rows of the images with extra bytes for efficiency (especially for double images).

    So even if a double element is 8 bytes, and you have, say, 300 rows, a row is not guaranteed to end at 8*300 or 2400 bytes since OpenCV might pad the end.

    Therefore, this prevents you from initializing a pointer to the first element of the image and then using ptr[y*img->height+x] to access elements since each row might have more than 8*(y*img->height) bytes.

    That’s why the example code calculates the pointer to each row each time using img->widthStep which represents the true size of each row in bytes.

    OpenCV 2.0

    If you use the Mat class, you can do the same thing along these lines:

    cv::Mat img(300,300,CV_64FC1);
    for( int y=0; y<img.rows; y++ ) 
        {
           double* ptr = reinterpret_cast<double*>(img.data + y * img.step);
           for( int x=0; x<img.cols; x++ ) 
           {
              ptr[x] = double(255);
           } 
        }
    cv::namedWindow("SO");
    cv::imshow("SO",img);
    cv::waitKey();
    

    where img.step is the distance between successive rows in bytes

    And if you want to directly access the element (slower):

    img.at<double>(y,x)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.