[英]Can't resolve issue with initializing a 2D array dynamically
我正在編寫一個程序,其中在3D數組中有一個灰度圖像:
FILE *file;
const int BytesPerPixel = 1;
const int Size = 512;
fread(Imagedata, sizeof(unsigned char), Size*Size*BytesPerPixel, file);
fclose(file);
我想用零填充此圖像,以使圖像尺寸每側增加一個像素/行/列。 這是我在main()中編寫的代碼:
const int padSize = 1;
const int newLength = Size + (2 * padSize), newBreadth = Size + (2 * padSize);
unsigned char*** paddedImage = padImageWithZeros(Imagedata, newLength, newBreadth, BytesPerPixel, padSize);
函數def在我的Header文件中,該文件包含在source.cpp中:
unsigned char*** initializeImage(const int length, const int breadth, const int channels)
{
unsigned char*** newImage = new unsigned char**[channels];
for (int i = 0; i < channels ; ++i)
{
newImage[i] = new unsigned char*[length];
for (int j = 0; j < length; ++j)
{
newImage[i][j] = new unsigned char[breadth];
for (int k = 0; k < breadth; ++k)
{
newImage[j][k][i] = (unsigned char)0;
}
}
}
return newImage;
}
unsigned char*** padImageWithZeros(unsigned char Imagedata[512][512][1],const int newLength,const int newBreadth, const int BytesPerPixel, const int padSize)
{
//Initializing a 3 channel padded Image
unsigned char*** paddedImage = initializeImage(newLength, newBreadth, BytesPerPixel);
//Storing the original image in the padded image
for (int channel = 0; channel < BytesPerPixel; channel++)
{
cout << "Channel = " << channel << endl;
for (int i = padSize; i < newLength; i++)
{
for (int j = padSize; j < newBreadth ; j++)
{
cout << paddedImage[i][j][channel] << endl;
cout << "Yes" << endl;
paddedImage[i][j][channel] = Imagedata[i - padSize][j - padSize][channel];
}
}
}
return paddedImage;
}
但是,在我檢查初始化后剛刪除的paddedImage矩陣后,它看起來像這樣:
並在Visual Studio中運行代碼會在以下位置顯示訪問沖突錯誤
paddedImage[i][j][channel] = Imagedata[i - padSize][j - padSize][channel];
我有一種感覺,我沒有正確初始化paddedImage,但是我不確定是什么問題。 請幫忙。
編輯:更新的代碼:main()中的函數調用:
const int padSize = 1;
const int newLength = Size + (2 * padSize), newBreadth = Size + (2 * padSize);
unsigned char*** paddedImage = padImageWithZeros(Imagedata, newLength, newBreadth, BytesPerPixel, padSize);
標頭中的函數定義。 H:
unsigned char*** initializeImage(const int length, const int breadth, const int channels)
{
unsigned char*** newImage = new unsigned char**[length];
for (int i = 0; i < length; ++i)
{
newImage[i] = new unsigned char*[breadth];
for (int j = 0; j < breadth; ++j)
{
newImage[i][j] = new unsigned char[channels];
for (int k = 0; k < channels; ++k)
{
newImage[i][j][k] = (unsigned char)0;
}
}
}
return newImage;
}
unsigned char*** padImageWithZeros(unsigned char Imagedata[512][512][1], const int newLength, const int newBreadth, const int BytesPerPixel, const int padSize)
{
//Initializing a 3 channel padded Image
unsigned char*** paddedImage = initializeImage(newLength, newBreadth, BytesPerPixel);
//Storing the original image in the padded image
for (int channel = 0; channel < BytesPerPixel; channel++)
{
cout << "Channel = " << channel << endl;
for (int i = padSize; i < newLength - padSize; i++)
{
for (int j = padSize; j < newBreadth - padSize; j++)
{
cout << paddedImage[i][j][channel] << endl;
cout << "Yes" << endl;
paddedImage[i][j][channel] = Imagedata[i - padSize][j - padSize][channel];
}
}
}
return paddedImage;
}
這里有2個錯誤:
initializeImage
函數。 您正在初始化image[channel][lenght]breadth]
時,需要一個image[lenght][breadth][channel]
(有關該功能所需的修復方法,請參見他的回答) 您應該使用類似以下內容的循環:
//Storing the original image in the padded image
for (int channel = 0; channel < BytesPerPixel; channel++)
{
cout << "Channel = " << channel << endl;
for (int i = padSize; i < newLength - padSize; i++)
{
for (int j = padSize; j < newBreadth - padSize; j++)
{
cout << paddedImage[i][j][channel] << endl;
cout << "Yes" << endl;
paddedImage[i][j][channel] = Imagedata[i - padSize][j - padSize][channel];
}
}
}
請查看for循環終止條件。 在您的解決方案中,您正在從原始圖像中復制newLength-padSize
元素,而您需要復制newLength-(2*padSize)
。
我要補充一點,您處理圖像的方式不是緩存友好的。 至少應為image[column][row][channels]
。 可以通過分配尺寸length * breadth * channels
的單個維度數組,然后使用一些成員函數來檢索所需的像素或顏色分量getColor(x,y,channel)
如getColor(x,y,channel)
或setColor(newValue,x,y,channel)
getColor(x,y,channel)
來提供另一個巨大的改進setColor(newValue,x,y,channel)
。 一維數組將在將來為您提供進一步優化的簡便方法(例如,使用指針)
我發現了問題。 正如@Stefano Buro所建議的那樣,您必須將圖像輸出為1D數組,而不是我正在做的3D數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.