C++;使用三维动态阵列和向量 我对C++很陌生,对此感到有点沮丧。下面,在pixelsVector中,我将每个像素的RGB浮点值存储在像素中,并希望转储 所有的值都用像素数组转换成一个字节数组,这样我就可以输出到一个图像文件。高度和宽度指的是图像尺寸。下面的代码工作正常,但我需要指定 pixelsArray在运行时的大小,因为它可能并不总是500x500图像 // WIDTH and HEIGHT specified at run-time vector<vector<Pixel>> pixelsVector (WIDTH, vector<Pixel> (HEIGHT)); ... unsigned char pixelsArray[500][500][3]; for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { // Returns RGB components vector<float> pixelColors = pixelArray[i][j].getColor(); for (int k = 0; k < 3; k++) { pixels[i][j][k] = pixelColors.at(k); } } } // write to image file fwrite(pixelsArray, 1, 500*500*3, file); //运行时指定的宽度和高度 矢量像素向量(宽度、矢量(高度)); ... 无符号字符像素数组[500][500][3]; 对于(int i=0;i
如果我把高度和宽度而不是500和500放在上面,我会得到一个错误,因为它们不是常量。现在使用3D向量似乎确实可行,但fwrite不会将向量作为其第一个参数。我尝试使用三指针数组,但是 它似乎根本不起作用——也许我用错了。在这里,它使用三维矢量来表示像素光线:C++;使用三维动态阵列和向量 我对C++很陌生,对此感到有点沮丧。下面,在pixelsVector中,我将每个像素的RGB浮点值存储在像素中,并希望转储 所有的值都用像素数组转换成一个字节数组,这样我就可以输出到一个图像文件。高度和宽度指的是图像尺寸。下面的代码工作正常,但我需要指定 pixelsArray在运行时的大小,因为它可能并不总是500x500图像 // WIDTH and HEIGHT specified at run-time vector<vector<Pixel>> pixelsVector (WIDTH, vector<Pixel> (HEIGHT)); ... unsigned char pixelsArray[500][500][3]; for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { // Returns RGB components vector<float> pixelColors = pixelArray[i][j].getColor(); for (int k = 0; k < 3; k++) { pixels[i][j][k] = pixelColors.at(k); } } } // write to image file fwrite(pixelsArray, 1, 500*500*3, file); //运行时指定的宽度和高度 矢量像素向量(宽度、矢量(高度)); ... 无符号字符像素数组[500][500][3]; 对于(int i=0;i,c++,3d,dynamic-arrays,C++,3d,Dynamic Arrays,如果我把高度和宽度而不是500和500放在上面,我会得到一个错误,因为它们不是常量。现在使用3D向量似乎确实可行,但fwrite不会将向量作为其第一个参数。我尝试使用三指针数组,但是 它似乎根本不起作用——也许我用错了。在这里,它使用三维矢量来表示像素光线: vector<vector<Pixel>> pixelsVector (WIDTH, vector<Pixel> (HEIGHT)); ... vector< vector< vector
vector<vector<Pixel>> pixelsVector (WIDTH, vector<Pixel> (HEIGHT));
...
vector< vector< vector<unsigned char> > > pixelsArray;
for (int i = 0; i < HEIGHT; i++)
{
pixels.push_back(vector< vector<unsigned char> >());
for (int j = 0; j < WIDTH; j++)
{
pixels[i].push_back(vector<unsigned char>());
vector<float> pixelColors;
pixelColors = pixelArray[i][j].getColor();
for (int k = 0; k < 3; k++)
{
pixels[i][j][k] = pixelColors.at(k);
}
}
}
// Error
fwrite(pixelsArray, 1, 500*500*3, file);
vector pixelsVector(宽度、向量(高度));
...
矢量<矢量<矢量>>像素射线;
对于(int i=0;i());
对于(int j=0;j
建议?您可以使用Boost.MultiArray insead of vectors of vectors,它允许您使用.data()方法访问底层内存
看起来你是在试图操纵图像,所以你可能想从最后一个代码片段中使用.< /P>< P>:
vector<vector<Pixel>> pixelsVector (WIDTH, vector<Pixel> (HEIGHT));
此时有一个内部向量,但它是空的,大小为0
vector<float> pixelColors;
pixelColors = pixelArray[i][j].getColor();
在这里,您试图分配给空的最内层向量中不存在的元素。您可以预先适当调整大小,也可以对每个值使用push_back
方法
此外,您是否确定float
值是介于0到255(或更一般地说,介于0到UCHAR_MAX)之间的整数,而不是介于0到1之间的整数
也许您需要缩放这些值
}
}
// Error
fwrite(pixelsArray, 1, 500*500*3, file);
如果pixelsArray
是字节的(非空)向量,则可以使用&pixelsArray[0]
获取指向第一个字节的指针
现在,我知道,上面只剖析了一些错误,并没有直接告诉你什么是对的。:-)
但是需要更多的信息来给出执行此操作的示例代码,例如(1)您的浮点值是多少,以及(2)您希望在文件中显示什么
不管怎样,希望这有帮助
阿尔夫
是内置库C++的增强库吗?John:不,但它和图书馆一样可以跨平台。
vector<float> pixelColors;
pixelColors = pixelArray[i][j].getColor();
for (int k = 0; k < 3; k++)
{
pixels[i][j][k] = pixelColors.at(k);
}
}
}
// Error
fwrite(pixelsArray, 1, 500*500*3, file);