C++ OpenCV Matrix conversion into coder::array unsigned short

44 Views Asked by At

I read a DICOM image into an OpenCV matrix, I need to store the pixel data into coder::array<unsigned short, 2U> array because I have a C++ code generated function (using MATLAB) that takes this data type as an input.

I tried the below code expecting the pixel data of rows and columns to be stored in each of the array dimentions.

coder::array<unsigned short, 2U> argInit_ImageArray(const cv::Mat& image)
{
    unsigned short rows = image.rows;
    unsigned short cols = image.cols;
    const unsigned short* pixelData = (unsigned short*)image.data;

    coder::array<unsigned short, 2U> result({rows, cols});
    unsigned short *pixelDataPointer = (unsigned short*)pixelData;

   for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result.at(i,j) = pixelDataPointer[i * cols + j];
        }
    }
    return result;
}
0

There are 0 best solutions below