1

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;
}
3
  • Why do you cast away the constness of pixelData ? Looks like you only read the content pointed by it. Commented Feb 8, 2023 at 9:40
  • Also - are you sure image is a 1 channel uint16 image ? And even if so - you should use the stride/step to access the pixels instead of col in i * cols + j in case there is padding (or use cv::Mat::ptr instead). Commented Feb 8, 2023 at 9:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 8, 2023 at 12:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.