如何在3D矩阵中选择Intrest的Region

时间:2016-02-11 09:21:24

标签: c++ matrix 3d readfile

我是c ++的新手。 我想读取带有无符号16位数据的原始文件 它是一个3d灰度图像 我使用visual studio来读取,处理和保存原始数据。 然后我使用matlab读取最终的原始数据以检查输出图像。 现在我在选择3D图像的ROI时遇到了一些问题。

这是我使用的代码 当我选择ROI时,输出图像出错

using namespace std;
uint16_t image[256][256][83];
uint16_t lowbyte[256][256][83];
uint16_t highbyte[256][256][83];
uint16_t I2[256][256][83];
uint16_t III[256][256][83];
uint16_t MOD[256][256][83];
uint16_t MAX = 0;
uint16_t MIN = 0;
uint16_t temp;

double MAX2 = 0;
double MIN2 = 0;
double I22[256][256][83];
unsigned char I3[256][256][83];
unsigned char num[255];

const int x11 = 112, x22 = 143, y11 = 82, y22 = 125, z11 = 33, z22 = 65;

int main()
{


    FILE *f0 = fopen("d:\\PET11.raw", "r");
    fread(image, sizeof(uint16_t), 256 * 256 * 83, f0);
    fclose(f0);


    for (int i = 0; i < 256; i++){
        for (int j = 0; j < 256; j++){
            for (int k = 0; k < 83; k++){
                lowbyte[i][j][k] = image[i][j][k] % 256;

                highbyte[i][j][k] = image[i][j][k] / 256;
                I2[i][j][k] = lowbyte[i][j][k] * 256 + highbyte[i][j][k];

                if (I2[i][j][k] > MAX)
                    MAX = I2[i][j][k];
                if (I2[i][j][k] < MIN)
                    MIN = I2[i][j][k];

            }
        }
    }




    for (int i = 0; i < 256; i++){
        for (int j = 0; j < 256; j++){
            for (int k = 0; k < 83; k++){
                I22[i][j][k] = (double)I2[i][j][k];

            }
        }
    }
    int count = 0;

    for (int i = 0; i < 256; i++){
        for (int j = 0; j < 256; j++){
            for (int k = 0; k < 83; k++){
                if ((i >= x11) && (i <= x22)){
                    if ((j >= y11) && (j <= y22)){
                        if ((k >= z11) && (k <= z22)){
                            count = count + 1;
                            if (I2[i][j][k] > MAX)
                                MAX = I2[i][j][k];
                            if (I2[i][j][k] < MIN)
                                MIN = I2[i][j][k];


                        }
                    }

                }
            }
        }
    }

    for (int i = 0; i < 256; i++){
        for (int j = 0; j < 256; j++){
            for (int k = 0; k < 83; k++){
//////////choose ROI of 3D image////////
                    if ((i >= x11) && (i <= x22) && (j >= y11) && (j <= y22) && (k >= z11) && (k <= z22))
//////////////////
                    I3[i][j][k] = (I22[i][j][k] - (double)MIN)*(255 / ((double)MAX - (double)MIN));
                else
                    I3[i][j][k] = I22[i][j][k];

                if (I3[i][j][k] > MAX2)
                    MAX2 = I3[i][j][k];
                if (I3[i][j][k] < MIN2)
                    MIN2 = I3[i][j][k];
            }
        }
    }
    FILE *image3 = fopen("d:\\PET3.raw", "wb");
    for (int i = 0; i <256; i++)
    {
        for (int j = 0; j< 256; j++)
        {
            for (int k = 0; k< 83; k++)
            {
                fwrite(&I3[i][j][k], sizeof(unsigned char), 1, image3);

            }


        }
    }

    fclose(image3);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

就像@Martin所说,你需要以二进制文件打开文件:fopen("d:\\PET11.raw", "rb")

您正在将MIN初始化为零。您应该将其初始化为65535。 此外,似乎不需要MAX2MIN2

对于感兴趣区域之外的值,作业I3[i][j][k]=I22[i][j][k]是可疑的。它将范围0 - 65535.0中的double赋给unsigned char,这是错误的。也许你想把它设置为零?