你如何对序列进行求和求平均值

  • 本文关键字:求和 平均值 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. 然后得到颜色 p = GetPixel(x+i, y+j) 对于序列中的每个元素,求和并平均它们的 R、G、B 为 (x,y)
  3. 所以我们有 9 个红色、9 个绿色、9 个蓝色到大道(红色)、大道(
  4. 蓝色)、大道(绿色)
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

像这样的东西?

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)) 

最新更新