你如何总结和平均序列?

时间:2014-04-02 22:23:13

标签: f#

假设我有一个坐标(x, y)及其邻居序列序列(-1, 1)(0, 1)(1, 1)(-1, 0)(0, 0)(1, 0)(-1, -1)(0, -1)(1, -1)

let n = [1 .. -1 .. -1]
|> Seq.collect (fun j -> [-1 .. 1] |> Seq.map(fun i -> [i, j]))
n |> Seq.iter(printf "%A")
  1. 我正在尝试将x和y分别添加到序列中的每个元素
  2. 然后为序列中的每个元素获取Color p = GetPixel(x + i,y + j),将(R,G,B)的(x,y)求和并求平均值
  3. 所以我们有9个红色,9个绿色,9个蓝色到Ave(红色),Ave(蓝色),Ave(绿色)

2 个答案:

答案 0 :(得分:1)

let offsets = seq { for i in -1 .. 1 do for j in -1 .. 1 do yield (i, j) }
let neighbourhood (x, y) = Seq.map (fun (i, j) -> (x + i, y + j)) offsets
let avgColours (cs : System.Drawing.Color seq) =
        let ((r, g, b), c) = cs |> Seq.fold (fun ((r, g, b), c) col -> ((r + int col.R, g + int col.G, b + int col.B), c + 1)) ((0, 0, 0), 0)
        System.Drawing.Color.FromArgb(r / c, g / c, b / c)

let avgNeighbours p = p |> neighbourhood |> Seq.map (fun (x, y) -> GetPixel(x, y)) |> avgColours

答案 1 :(得分:1)

这样的东西?

let f x y = 
    let n = [1 .. -1 .. -1] |> Seq.collect (fun j -> [-1 .. 1] |> Seq.map(fun i -> (i, j)))
    n |> Seq.map (fun (i,j) -> x+i,y+j)
      |> Seq.map bitmapobject.GetPixel
      |> Seq.map (fun c -> float c.R, float c.G, float c.B)
      |> Seq.fold (fun (R,G,B) (r,g,b) -> (R+r, G+g, B+b)) (0.0, 0.0, 0.0)
      |> (fun (r,g,b) -> (r/9.0, g/9.0, b/9.0))