Ask Your Question
0

Pointer to Mat Variables

asked 2017-10-13 04:55:02 -0600

Digital Design gravatar image

updated 2017-10-13 05:41:09 -0600

berak gravatar image

Hello, I want to add a dynamic array of Mat variebles, i.e. a 2D array of images. Simply suppose you are putting e.g. 3 rows and 4 columns of your images on the table. I write the following code:

Mat **img_array;
Mat temp = imread("Lena.tif", CV_LOAD_IMAGE_ANYDEPTH);
img_array = (Mat **)malloc(hor * sizeof(temp));
for (char i1 = 0;i1<hor;i1++)
{
    img_array[i1] = (Mat *)malloc(ver * sizeof(temp));
}
puts("Initializing Image Array...");
for (i1 = 0; i1 < hor; i1++)
    for (char j1 = 0; j1 < ver; j1++)
        img_array[i1][j1] = Mat::zeros(rows, col, CV_8UC3);

hor, ver are the nuber of horizontal and vertical elements of each array and rows, col refer to the number of the pixels of each individual image. When I run the program, the following error is issued: Exception thrown at 0x00007FFCFFA28BCC (opencv_world320d.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred Any Idea?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-10-13 05:46:58 -0600

berak gravatar image

updated 2017-10-13 06:08:54 -0600

opencv is a c++ library, while your attempt at doing this is C, and it won't work.

please AVOID using pointers to cv::Mat, it's already a refcounted smartpointer on it's own. also malloc will only allocate memory, not construct objects (and you also only allocated the outer array, not the inner ones).

use std::vector instead (and, please, read a c++ book):

#include <vector>
using std::vector;

vector< vector< Mat > >(hor, ver); //preallocated 2d array
for (i1 = 0; i1 < hor; i1++)
    for (char j1 = 0; j1 < ver; j1++)
        img_array[i1][j1] = Mat::zeros(rows, col, CV_8UC3);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-13 04:55:02 -0600

Seen: 5,727 times

Last updated: Oct 13 '17