2017-11-21 4 views
0

私はImage Blurという問題に取り組んでいます。私は自分のコードを2D配列からアイテム(1sか0s)を取って、1つごとに隣接する0を1に変更する必要があります。これまでのところ、私のコードはこれを最初の1に渡していますが、何らかの理由で他のものよりもループしません。このループをどのようにして各項目に適用するのですか?

class Image 
    def initialize(image) 
    @values = image 
    end 

    def find_ones 
    ones = [] 
    @values.each_with_index do |row, row_index| 
     row.each_with_index do |pixel, column_index| 
     if pixel == 1 
      coord = [row_index, column_index] 
      ones << coord 
     end 
     puts "#{pixel} #{row_index} #{column_index}" if pixel == 1 
     end 
    end 
    ones 
    end 

    def transform 
    ones = find_ones 
    ri = ones[0][0] 
    ci = ones[0][1] 
    ones.each do 
     @values[ri + 1][ci] = 1 if (ri + 1) <= 3 
     @values[ri - 1][ci] = 1 if (ri - 1) >= 0 
     @values[ri][ci + 1] = 1 if (ci + 1) <= 3 
     @values[ri][ci - 1] = 1 if (ci - 1) >= 0 
    end 
    end 


def output_image 
    @values.each do |row| 
     puts row.join 
    end 
    end 
end 


image = Image.new([ 
    [0, 0, 0, 1], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 1, 0, 0] 
]) 

image.transform 
image.output_image 

あらかじめご協力いただきありがとうございます。

答えて

0

よくお使いのones.each doonesを通過しますが、ブロックにはパラメータがないため無視します。ブロックを実行する頻度をとすると、基本的にはonesを使用してを決定します。

あなた

ri = ones[0][0] 
ci = ones[0][1] 
ones.each do 

ones.each do |ri, ci| 

私はあなたが(あなたが似た何かを)find_ones権利を得た驚いているが、transformにとても奇妙な何かを(そのようなriciを設定する必要があります)。

関連する問題