我正在尝试创建一个带有uint8_t值的10 x 10网格。使用一些坐标,我尝试在这些索引处放置255个值。当我在这些坐标上替换255时,它工作得很好,但是当我试图打印出这些索引上的值时,它似乎不是我想要的值。
float grid_width = 10;
float grid_height = 10;
float grid_resolution = 0.05;
float rows = grid_width/grid_resolution;
float cols = grid_height/grid_resolution;
std::vector<std::vector<float>> landmarks = {{2,2},{8,8},{2,8},{8,2},{3,2},{3.5,2.1},{4.0,3.5},{4.5,2.0},{5.0,3.0}};
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
for(auto i: landmarks)
{
for(auto m = i[0]-(grid_resolution*2) ; m <= i[0] +(grid_resolution*2) ; m=m+grid_resolution )
{
for(auto n = i[1]-(grid_resolution*2); n <= i[1]+(grid_resolution*2); n=n+grid_resolution)
{
int m_co = m/grid_resolution;
int n_co = n/grid_resolution;
grid[m_co][n_co] = 255;
cout<<"m_co: "<< m_co <<" n_co:" << n_co <<endl;
cout<<"grid[m_co][n_co]: "<< unsigned(grid[m_co][n_co]) <<endl; //check the printout over here for index values of 18,22 you will see 255 but when you try it outside the for loops those values are rest to 0
}
}
}
// for(auto m: grid)
// {
// for(auto n: m)
// {
// cout<<unsigned(n)<<" ";
// }
// cout<<endl;
// }
cout<<unsigned(grid[18][18])<<endl; //expected 255 got 255
cout<<unsigned(grid[18][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][22])<<endl; //expected 255 got 0
cout<<unsigned(grid[22][18])<<endl; //expected 255 got 0
cout<<unsigned(grid[17][17])<<endl; //expected 255 got 0
cout<<unsigned(grid[20][18])<<endl; //expected 255 got 255因此,我使用地标向量来获取索引,并尝试在地标的索引上将网格向量中的0替换为255,但我也想在该索引周围的索引上替换0。例如,如果2,2是我的坐标,我想替换1,1和1, 2 ,3和2,1和2,以及2,3和3,1和3,2和3,3到255,但我得不到1,3和2,3,3,255我得到的是0这只是我尝试实现的一个示例
发布于 2021-08-27 03:08:23
我找到了解决方案。旧的逻辑太复杂了,不能与不同的分辨率一起工作。所以我做了一个更简单的,它确实需要处理四舍五入的问题。首先获取每个点,并尝试用索引替换值。
for(auto i: landmarks)
{
for(int m = (i[0]/grid_resolution)-2; m <= (i[0]/grid_resolution)+2; m++)
{
for(int n = (i[1]/grid_resolution)-2; n <= (i[1]/grid_resolution)+2; n++)
{
grid[m][n] = 255;
}
}
}发布于 2021-08-27 02:29:03
在循环中,您打印的是n/grid_resolution。当它打印22时,它实际上是21.99999809265,但是因为iostream的默认精度是6,所以它四舍五入为22。另一方面,当您将它赋给int时,它会向下舍入为21,因此您实际上将255存储在grid[18][21]中。
发布于 2021-08-27 02:19:59
问题是m/grid_resolution的类型是float,而m_co的类型是int:
int main()
{
float grid_width = 10;
float grid_height = 10;
float grid_resolution = 0.1;
float rows = grid_width/grid_resolution;
float cols = grid_height/grid_resolution;
cout.precision(5);
std::vector<std::vector<float>> landmarks = {{2,2},{8,8},{2,8},{8,2},{3,2},{3.5,2.1},{4.0,3.5},{4.5,2.0},{5.0,3.0}};
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
for(auto i: landmarks)
{
for(auto m = i[0]-(grid_resolution*2) ; m <= i[0] +(grid_resolution*2) ; m=m+grid_resolution )
{
for(auto n = i[1]-(grid_resolution*2); n <= i[1]+(grid_resolution*2); n=n+grid_resolution)
{
int m_co = m/grid_resolution;
int n_co = n/grid_resolution;
grid[m_co][n_co] = 255;
// Uncomment the following two lines for showing that are different
//if (m_co != m/grid_resolution || n_co != n/grid_resolution)
// cout << "different" << endl;
cout << "grid["<<m_co<<"]["<<n_co<<"]: "<< unsigned(grid[m_co][n_co]) << endl;
}
}
}
// for(auto m: grid)
// {
// for(auto n: m)
// {
// cout<<unsigned(n)<<" ";
// }
// cout<<endl;
// }
cout<<"grid[18][18]" << unsigned(grid[18][18])<<endl;
cout<<"grid[18][22]" << unsigned(grid[18][22])<<endl;
cout<<"grid[22][22]" << unsigned(grid[22][22])<<endl;
cout<<"grid[22][18]" << unsigned(grid[22][18])<<endl;
cout<<"grid[17][17]" << unsigned(grid[17][17])<<endl;
cout<<"grid[20][18]" << unsigned(grid[20][18])<<endl;
return 0;
}如果运行该代码,将打印m_co和n_co的正确值。
诚挚的问候。
https://stackoverflow.com/questions/68947130
复制相似问题