What would be a good TRUE black and white colormatrix?

后端 未结 4 1423
星月不相逢
星月不相逢 2020-12-05 03:50

I want to convert an image from color to B/W (i.e. no grayscale, just black and white). Does anyone have a good colormatrix to achieve this?

4条回答
  •  一向
    一向 (楼主)
    2020-12-05 04:14

    VB.NET version:

    Using gr As Graphics = Graphics.FromImage(SourceImage) 'SourceImage is a Bitmap object'
      Dim gray_matrix As Single()() = {
        New Single() {0.299F, 0.299F, 0.299F, 0, 0},
        New Single() {0.587F, 0.587F, 0.587F, 0, 0},
        New Single() {0.114F, 0.114F, 0.114F, 0, 0},
        New Single() {0, 0, 0, 1, 0},
        New Single() {0, 0, 0, 0, 1}
      }
      Dim ia As New System.Drawing.Imaging.ImageAttributes
      ia.SetColorMatrix(New System.Drawing.Imaging.ColorMatrix(gray_matrix))
      ia.SetThreshold(0.8)
      Dim rc As New Rectangle(0, 0, SourceImage.Width, SourceImage.Height)
      gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia)
    End Using
    

提交回复
热议问题