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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:41:33+00:00 2026-06-07T05:41:33+00:00

Hello I have a basic question about opencv. If I try to allocate memory

  • 0

Hello I have a basic question about opencv. If I try to allocate memory with the cv::Mat class I could do the following:

cv::Mat sumimg(rows,cols,CV_32F,0);
float* sumimgrowptr = sumimg.ptr<float>(0);

but then I get a bad pointer (Null) back. In the internet some person use this:

cv::Mat* ptrsumimg = new cv::Mat(rows,cols,CV_32F,0);
float* sumimgrowptr = ptrsumimg->ptr<float>(0);

and also here I get a Null pointer back ! but If I finally do this :

            cv::Mat sumimg;
            sumimg.create(rows,cols,CV_32F);
            sumimg.setTo(0);
            float* sumimgrowptr = sumimg.ptr<float>(0);

then everything is fine ! so I wannted to know what is wrong in what I am doing ?

  • 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-07T05:41:34+00:00Added an answer on June 7, 2026 at 5:41 am

    The main problem is here

    cv::Mat sumimg(rows,cols,CV_32F,0);
    

    OpenCV provides multiple constructors for matrices. Two of them are declares as follows:

    cv::Mat(int rows, int cols, int type, SomeType initialValue);
    

    and

    cv::Mat(int rows, int cols, int type, char* preAllocatedPointerToData);
    

    Now, if you declare a Mat as follows:

    cv::Mat sumimg(rows,cols,CV_32F,5.0);
    

    you get a matrix of floats, allocated and initialized with 5.0. The constructor called is the first one.

    But here

    cv::Mat sumimg(rows,cols,CV_32F,0);
    

    what you send is a 0, which in C++ is a valid pointer address. So the compiler calls the constructor for the preallocated data. It does not allocate anything, and when you want to access its data pointer, it is, no wonder, 0 or NULL.

    A solution is to specify that the fourth parameter is a float:

    cv::Mat sumimg(rows,cols,CV_32F,0.0);
    

    But the best is to avoid such situations, by using a cv::Scalar() to init:

    cv::Mat sumimg(rows,cols,CV_32F, cv::Scalar::all(0) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very very basic java question: For the class below: public class Hello
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
Hello I have one question about using genetic in correct way. please look at
I have basic hello word example of Prime Faces. I have created dynamic web
I am new to PHP, so I have a basic question. I have this
Hello i have a few questions about mvc pattern and frameworks in general. I
I'm new to Objective C and have a pretty basic question. So I have
Dup of Some Basic PHP Questions Hello, I have a heap of tinyint fields
Hello I have the following bit of code which seems to work, but I'm
I have a question about some code I'm testing to start understanding posix threads.

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.