如何将2D平行向量的单个元素重新分配给1D向量



嗨,我正在为C++类的入门做作业,我对某个部分完全感到困惑。基本上,任务是打开一个包含单个整数的文件(数据表示高程平均值的网格),用这些值填充2D矢量,找到矢量的最小值和最大值,将矢量的每个元素转换为包含该值的RGB表示的1D并行矢量(灰度),并将数据导出为PPM文件。我已经成功地达到了将矢量的值转换为RGB并行矢量的地步。

我的问题是,我不完全确定如何将新的RGB矢量分配给矢量的原始元素。这是我目前拥有的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main () {
// initialize inputs
int rows;
int columns;
string fname;
// input options
cout << "Enter number of rows" << endl;
cin >> rows;
cout << "Enter number of columns" << endl;
cin >> columns;
cout << "Enter file name to load" << endl;
cin >> fname;
ifstream inputFS(fname);
// initialize variables
int variableIndex;
vector<vector<int>> dataVector (rows, vector<int> (columns));
int minVal = 0;
int maxVal = 0;
// if file is open, populate vector with data from file
if(inputFS.is_open()) {
for (int i = 0; i < dataVector.size(); i++) {
for (int j = 0; j < dataVector.at(0).size(); j++) {
inputFS >> variableIndex;
dataVector.at(i).at(j) = variableIndex;
}
}
}
// find max and min value within data set
for (int i = 0; i < dataVector.size(); i++) {
for (int j = 0; j < dataVector.at(0).size(); j++) {
if (dataVector.at(i).at(j) < minVal) {
minVal = dataVector.at(i).at(j);
}
if (dataVector.at(i).at(j) > minVal) {
maxVal = dataVector.at(i).at(j);
}
}
}
// initialize variables and new color vector
// -------PART I NEED HELP ON-----------
int range = maxVal - minVal;
int remainderCheck = 0;
double color = 0;
vector<int> colorVector = 3;
for (int i = 0; i < dataVector.size(); i++) {
for (int j = 0; j < dataVector.at(0).size(); j++) {
remainderCheck = dataVector.at(i).at(j) - minVal;
if (remainderCheck / range == 0) {
cout << "Color 0 error" << endl;
// still need to find the RGB value for these cases
}
else {
color = remainderCheck / range;
fill(colorVector.begin(),colorVector.end()+3,color);
dataVector.at(i).at(j) = colorVector; // <-- DOESN'T WORK
}
}
}
}

我对C++的了解非常有限,如果有任何帮助,我们将不胜感激。此外,如果您对处理同一代码块中的/运算符问题的其他评论有任何建议,我也将不胜感激。

以下是该特定部分的实际说明:

步骤3-计算地图每个部分的颜色并存储输入数据文件包含地图中每个单元的高程值。现在,您需要计算用于表示这些评估值的颜色(在白色和黑色之间的灰度级中)。灰色阴影应按比例缩放到地图的高程。

传统上,图像在电子系统(如电视和计算机)中通过RGB颜色模型来表示和显示,RGB颜色模型是一种附加颜色模型,其中红色、绿色和蓝色光以各种方式添加在一起,以再现广泛的颜色阵列。在该模型中,颜色通过0到255之间的三个整数(R、G和B)值表示。例如,(0255)表示蓝色,(255255,0)表示黄色。在RGB颜色中,如果三个RGB值中的每一个都相同,我们就会得到一个灰色阴影。因此,从黑色(0,0,0)到中灰色(128128128),再到白色(255255255),有256种可能的灰色色调。

若要使阴影为灰色,应使用二维矢量中的最小值和最大值将每个整数(高程数据)缩放到0和255之间的值(包括0和255)。这可以通过以下等式来实现:

color=(仰角-最小仰角)(最大仰角-最小高度)*255

检查你的数学以确保你的缩放比例正确。检查您的代码以确保您的算术运算按您的要求运行。回想一下,如果a和b是声明为整数的变量,那么如果a==128且b==256,则表达式a/b将为0。

在计算灰色阴影时,将该值存储在R、G和B的三个平行向量中。将R、G、B的值相同将产生灰色。矢量的结构应反映具有高程数据的矢量。

您的教授要求您制作三个额外的vector<vector<int>>:R、G和B各1

typedef std::vector <int>      row_type;
typedef std::vector <row_type> image_type;
image_type dataVector( rows, row_type( columns ) );
image_type R         ( rows, row_type( columns ) );
image_type G         ( rows, row_type( columns ) );
image_type B         ( rows, row_type( columns ) );

此外,无论何时做类似fill(foo.begin(),foo.end()...)的事情,都要小心。试图将填充到容器(foo.end()+3)末尾之外是未定义的行为

像以前一样将数据集加载到dataVector中,找到最小值和最大值,然后为每个元素找到灰度值(在[0255]中)。将该值分配给RGB中的每个对应元素。

一旦有了这三个平方向量,就可以使用它们来创建PPM文件。

最新更新