cope 3x3 matrix from a bigger one
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)
what I am doing here is that I am trying to cut a portion out of the bigger matrix, in this case, what the getData() returns, say, it is 100x100 matrix, I want to cope the a 3x3 neighborhood around each p开发者_如何学编程ixel from the bigger matrix to newly created one, the condition is that if the 3x3 neighborhood goes out of the bound in the 100X100 matrix, like the top neighbor of data[0-1][1] is out of bound, the corresponding position in the 3x3 matrix is set to zero. How can I do that?
If you're going to all the trouble of building an Image class, why not use it for 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);
}
精彩评论