0
我正在對像素進行一些平均,我知道這個循環可以用於任何二維數組。但如果src是一個Mat圖像(OpenCV),這怎麼能工作呢? 我有這麼多的困難通過這些參數在墊圖像功能 我的問題是否有意義?如果輸入(src)是Mat圖像,該循環如何工作?
#include <iostream>
const unsigned dim = 3;
typedef int array_type [dim][dim];
這裏是循環:
void average(array_type& dest, const array_type& src)
{
// corners:
dest[ 0 ][ 0 ] =
(src[ 0 ][ 0 ] + src[ 0 ][ 1 ] + src[ 1 ][ 0 ] + src[ 1 ][ 1 ])/4;
dest[dim-1][ 0 ] =
(src[dim-1][ 0 ] + src[dim-2][ 0 ] + src[dim-1][ 1 ] + src[dim-2][ 1 ])/4;
dest[ 0 ][dim-1] =
(src[ 0 ][dim-1] + src[ 1 ][dim-1] + src[ 0 ][dim-2] + src[ 1 ][dim-2])/4;
dest[dim-1][dim-1] =
(src[dim-1][dim-1] + src[dim-2][dim-1] + src[dim-1][dim-2] + src[dim-2][dim-2])/4;
// top, bottom, and sides:
for (unsigned i = 1; i < dim - 1; ++i)
{
const unsigned j = dim - 1;
dest[0][i] =
(src[0][i-1] + src[0][i] + src[0][i+1] + src[ 1 ][i-1] + src[ 1 ][i] + src[ 1 ][i+1])/6;
dest[j][i] =
(src[j][i-1] + src[j][i] + src[j][i+1] + src[j-1][i-1] + src[j-1][i] + src[j-1][i+1])/6;
dest[i][0] =
(src[i-1][0] + src[i][0] + src[i+1][0] + src[i-1][ 1 ] + src[i][ 1 ] + src[i+1][ 1 ])/6;
dest[i][j] =
(src[i-1][j] + src[i][j] + src[i+1][j] + src[i-1][j-1] + src[i][j-1] + src[i+1][j-1])/6;
}
// middle:
for (unsigned i = 1; i < dim - 1; ++i)
{
for (unsigned j = 1; j < dim - 1; ++j)
{
dest[i][j] = (src[i-1][j-1] + src[i-1][j] + src[i-1][j+1] +
src[ i ][j-1] + src[ i ][j] + src[ i ][j+1] +
src[i+1][j-1] + src[i+1][j] + src[i+1][j+1])/9 ;
}
}
}