将1D数组转换为2D数组。图像输入为1D阵列

时间:2013-10-17 11:52:14

标签: c++ image-processing

图像以1D阵列数据传入。需要将其迁移到2D数据。 2D数组的大小必须为640行和1176列,并且最终必须使用.pgm标头写入文件。

void saveFile(unsigned short* mImStack)  // Image to be accessed
{
    std::ofstream f("D:\\tester.txt", ios::out);

    int rows = 0;
    int cols = 0;

    int t_rows = (mImageRaw->m_ImageHeight); // height of image (640)
    int t_cols = (mImageRaw->m_ImageWidth); // width of image (1176)

    int ncells = t_rows * t_cols; // total cells of the 1D array which has the image data

    unsigned short** mImage2D = new unsigned short*[cols];  // defining a 2D array
    for (int i = 0; i < t_cols; ++i)
    {
        mImage2D[i] = new unsigned short[rows];
    }
    for (int n = 0; n < ncells; n++)
    {
        mImage2D[rows][cols] = mImStack[n];  // Error ocuring here as Access violation reading location 
        cols++;
        if (cols == t_cols && rows != t_rows + 1)
        {
            rows++;
            cols = 0;
        }
    }

    f << "P2\n" << t_rows << " " << t_cols << "\n4096\n" << mImage2D[rows][cols];
    f.close();
}

0 个答案:

没有答案