0

最近、この多次元配列内の内側の i ループと j ループを展開しようとしましたが、filter->get(i,j) は常に画像のテクスチャを台無しにします。i および j ループの展開を手伝ってくれる人はいますか? ありがとう。

私の試み:

double
applyFilter(struct Filter *filter, cs1300bmp *input, cs1300bmp *output)
{

     long long cycStart, cycStop;

     cycStart = rdtscll();

    output -> width = input -> width;
    output -> height = input -> height;
int a = filter -> getDivisor();
int n = filter -> getSize();
for (int plane = 0; plane < 3; plane++){
    for(int row = 1; row < (input -> height) - 1 ; row = row + 1) {
        for(int col = 1; col < (input -> width) - 1; col = col + 1) {
            int value = 0;
            int val1, val2;
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < n; i+=2) {
                    val1 = val1 + input -> color[plane][row + i - 1][col + j - 1]
                    * filter -> get(i, j);
                    val2 = val2 + input -> color[plane][row + i][col + j -1] * filter->get(i+1,j);
                }
            }
            value = (val1 + val2) / a;
            if ( value  < 0 ) { value = 0; }
            if ( value  > 255 ) { value = 255; }
            output -> color[plane][row][col] = value;
        }

    }
}

 cycStop = rdtscll();
 double diff = cycStop - cycStart;
 double diffPerPixel = diff / (output -> width * output -> height);
 fprintf(stderr, "Took %f cycles to process, or %f cycles per pixel\n",
  diff, diff / (output -> width * output -> height));

 return diffPerPixel;
}

オリジナル:

int a = filter -> getDivisor();
int n = filter -> getSize();    
for (int plane = 0; plane < 3; plane++){
    for(int row = 1; row < (input -> height) - 1 ; row = row + 1) {
        for(int col = 1; col < (input -> width) - 1; col = col + 1) {
            int value = 0;
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < n; i++) {
                    value = value + input -> color[plane][row + i - 1][col + j - 1]
                    * filter -> get(i, j);
                }
            }
            value = value / a;
            if ( value  < 0 ) { value = 0; }
            if ( value  > 255 ) { value = 255; }
            output -> color[plane][row][col] = value;
4

2 に答える 2

0

内側のループを次のように置き換えてみてください。

int value = 0;
int val1 = 0, val2 = 0;
for (int j = 0; j < n; j++) { 
    int i;
    for (i = 0; i < n; i+=2) {
        val1 += input->color[plane][row+i-1][col+j-1] * filter->get(i,j);
        val2 += input->color[plane][row+i  ][col+j-1] * filter->get(i+1,j);
    } 
    if (i < n)
        val1 += input->color[plane][row+i-1][col+j-1] * filter->get(i,j);
} 
value = (val1 + val2) / a;
于 2013-11-16T02:11:54.303 に答える
0

n が 2 の倍数である場合にのみ、あなたの方法は正しいです。

追加した:

まず最初に、初期化を忘れていたことに気付きましたval1val2これがおそらく問題の主な原因です。

第二に、あなたのコードは3のフィルターサイズ用に特別に書かれたようです:

  • 小さいフィルターの場合、境界線にはまったくアクセスしません。
  • より大きなものについては、画像の外側の位置にアクセス [row + i - 1]input->heightます。

サイズ 3 のフィルターのみを使用する場合は、内側のループを完全に展開します。それ以外の場合は、行と列の値の境界を確認してください。

さて、ループ展開については、Google 検索を行うことをお勧めします。これを適切に行う方法に関する多くの例を見つけることができます。ウィキペディアのページで見つけることができます。

あなたの場合、最も簡単な解決策は次のとおりです。

int value = 0;
int val1=0, val2=0;
for (int j = 0; j < n; j++) {
    for (int i = 0; i < n-1; i+=2) {
        val1 = val1 + input->color[plane][row+i-1][col+j-1] * filter->get(i  ,j);
        val2 = val2 + input->color[plane][row+i  ][col+j-1] * filter->get(i+1,j);
    }
    if (n%2 !=0) {
        val1 = val1 + input->color[plane][row+n-2][col+j-1] * filter->get(n-1,j);
    }
}
value = (val1 + val2) / a;

ループをさらに展開したい場合、より一般的な方法は次のようになります (例: 4 の場合):

int value = 0;
int val1=0, val2=0, val3=0, val4=0;
for (int j = 0; j < n; j++) {

    for (int i = 0; i < n-3; i+=4) {
        val1 = val1 + input->color[plane][row+i-1][col+j-1] * filter->get(i  ,j);
        val2 = val2 + input->color[plane][row+i  ][col+j-1] * filter->get(i+1,j);
        val3 = val3 + input->color[plane][row+i+1][col+j-1] * filter->get(i+2,j);
        val4 = val4 + input->color[plane][row+i+2][col+j-1] * filter->get(i+3,j);
    }
    switch (n % 4) {
        case 3: val1+=input->color[plane][row+n-4][col+j-1] * filter->get(i+n-3,j);
        case 2: val1+=input->color[plane][row+n-3][col+j-1] * filter->get(i+n-2,j);
        case 1: val1+=input->color[plane][row+n-2][col+j-1] * filter->get(i+n-1,j);
    }
    value = (val1 + val2 + val3 + val4) / a;
}

注:
フィルターのサイズ、使用されているコンパイラとコンパイラ オプション、およびシステムによっては、上記の解決策ではコードが高速化されず、速度が低下することさえあることに注意してください。また、必要に応じて、通常はコンパイラがループ展開を実行できることにも注意してください (たとえば、gcc の-funroll-loopsオプションを使用)。

于 2013-11-16T01:05:55.140 に答える