Bus error (core dumped) while declaring cv::Mat

asked 2019-07-02 07:55:40 -0500

Ani_Cv gravatar image

updated 2019-07-02 07:57:07 -0500

Hi, I have been dealing with very simple matrices. The code snippet is below

cv::Size sz, sz1, sz2;
sz2 = cv::Size(4,1);
cv::Mat M(sz2, CV_8U);
int color_array_size = (1/3) * (point_cloud->width * point_cloud->height);
uint32_t arr [point_cloud->width * point_cloud->height] = {0};
uint8_t red_array[color_array_size] ={}, blue_array[color_array_size] ={} , green_array[color_array_size] ={};

sz= cv::Size(point_cloud->width, point_cloud->height);
sz1= cv::Size( point_cloud->width, point_cloud->height);
cv::Mat_ <cv::Vec3f> image(sz, CV_32FC3);
cv::Mat image_color(sz, CV_32F, arr);

The trouble is caused due to cv::Mat M(sz2, CV_8U); building the code is fine. While running the executable Bus error (core dumped) occurs. Some other type of matrices are also causing this annoying trouble. Anyone could suggest something to get rid of this?

edit retag flag offensive close merge delete


you're taking liberties with the type system, and your Mat constructors are broken ;(

  • cv::Mat image_color(sz, CV_32F, arr);

arr is an int not a float array.

  • cv::Mat_ <cv::Vec3f> image(sz, CV_32FC3);

no, it must be the array size, not CV_32FC3 as well for M

berak gravatar imageberak ( 2019-07-02 09:19:29 -0500 )edit

ok, I modified to float arr . Still same. I do not understand your second point.

Ani_Cv gravatar imageAni_Cv ( 2019-07-02 09:44:00 -0500 )edit

strike the 2nd part, i misread something. apologies.

what are you doing with image_color later ? is arr valid all through it's lifetime ?

berak gravatar imageberak ( 2019-07-02 10:05:03 -0500 )edit

how many bytes do you actually try to allocate in (limited) auto memory with constructs like:

float a[somevar*somevar];


imho, you should use std::vector (or at least new), not put all of your (dynamic) arrays in auto memory

berak gravatar imageberak ( 2019-07-02 10:25:49 -0500 )edit


sz2 = cv::Size(4,1);
        float arr1 [4] = {0, 1,2,3};
        cv::Mat M = Mat(sz2, CV_32FC1, arr1);

this is the part that is modified in the above code. Now, it works fine. But still I am not happy. Why is it so that creating matrix M without assigning data to it, complaints where as, after assigning data to it, it works fine. If somebody please explain ?

Ani_Cv gravatar imageAni_Cv ( 2019-07-02 10:58:46 -0500 )edit

I want to allocate (point_cloud->width , 3) data in a 2D float matrix.

Ani_Cv gravatar imageAni_Cv ( 2019-07-02 11:07:54 -0500 )edit

why even use external arrays ?

berak gravatar imageberak ( 2019-07-02 11:23:12 -0500 )edit