我正在编写一个程序,其中我在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;
}
输出: enter image description here enter image description here
答案 0 :(得分:0)
这里有2个错误:
initializeImage
函数,正如@ rafix07已经指出的那样。您需要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)
或setColor(newValue,x,y,channel)
),可以提供另一项巨大的改进。单维数组将为您提供进一步优化的简便方法(例如使用指针)
答案 1 :(得分:0)
我发现了问题。正如@Stefano Buro建议的那样,你必须将图像输出为一维数组,而不是我正在做的3D数组。