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;
}
pixelData? Looks like you only read the content pointed by it.imageis a 1 channeluint16image ? And even if so - you should use the stride/step to access the pixels instead ofcolini * cols + jin case there is padding (or usecv::Mat::ptrinstead).