문제

I have a sequence of elements [(12, 34); (56, 78); ...] and I want to turn it into [(XXX, XXX, XXX); (XXX, XXX, XXX); ...] where (XXX, XXX, XXX) is the (R, G, B) of the given tuple/coordinates, e.g., (12, 34). According to .NET documentation, System.Drawing.Bitmap.GetPixel returns System.Drawing.Color and you can just call Color.R, Color.B and Color.G to get the colour value as a byte.

This is what I've got so far:

let neighbourColor (img : System.Drawing.Bitmap) (s : seq<int * int>) =
 s |> Seq.collect(fun e -> s |> img.GetPixel(fst(e), snd(e)) |> Seq.map(fun c -> (c.R, c.G, c.B)))

For every element e in sequence s, get the color of e which is a tuple of coordinates on image img, then pass it to Seq.map to transform it into (Red, Green, Blue) and collect it as a new sequence neighbourColor.

Basically I want to turn seq<int*int> into seq<byte*byte*byte>.

도움이 되었습니까?

해결책

let neighbourColor (img:Bitmap) coords =
   coords |> Seq.map (fun (x, y) -> let c = img.GetPixel (x, y) in c.R, c.G, c.B)

In general, it is probably better style to only explicitly specify a parameter's type when it cannot be inferred (in this case coords is already implicitly understood to be a seq<int*int>).

다른 팁

let (|RGB|) (c:Color)=RGB(c.R,c.G,c.B)
let Test (img:Bitmap)=Seq.map (img.GetPixel>>function RGB(r,g,b)->r,g,b)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top