从较大的一个处理3x3矩阵

时间:2011-04-29 02:51:23

标签: c++

for(int i=0;i<getNumRows();++i)
{
    for(int j=0;j<getNumCols();++j)
    {
        int X = hfilter * threebythree(i,j,getData());
        int Y = vfilter * threebythree(i,j,getData());
        //cout<<X<<","<<Y<<endl;
    setValue(i,j, sqrt(X*X+Y*Y));
    }
}
writeToFile(ofilename);
}

Image Image::threebythree(int globali,int globalj,int**data){
Image neighbor;
neighbor.setNumRows(3);
neighbor.setNumCols(3);
neighbor.AllocateMem();
//this algorithm is wrong here, trying to figure out one.
for(int i=0;i<neighbor.getNumRows();++i)
{
    for(int j=0;j<neighbor.getNumCols();++j)

我在这里做的是我试图从更大的矩阵中切出一部分,在这种情况下,getData()返回的内容,比如说,它是100x100矩阵,我想应对周围的3x3邻域从较大矩阵到新创建的矩阵的每个像素,条件是如果3x3邻域超出100X100矩阵的界限,就像数据[0-1] [1]的顶级邻居越界,相应的3x3矩阵中的位置设置为零。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您遇到构建Image类的所有麻烦,为什么不将它用于data

Image Image::threebythree(int globali,int globalj, const Image &data)
{
  Image neighbor;
  neighbor.setNumRows(3);
  neighbor.setNumCols(3);
  neighbor.AllocateMem();

  for(int i=0 ; i<=3 ; ++i)
    for(int j=0 ; j<=3 ; ++j)
    {
      if(i+globali-1 >=0 && j+globalj-1 >=0 &&
         i+globali-1 < data.getNumRows() && j+globalj-1 < data.getNumCols())
        neighbor[i][j] = data[i+globali-1][j+globalj-1];
      else
        neighbor[i][j] = 0;
    }
  return(neighbor);
}