以下代码可以使图片变成灰白,但我要的是黑白,请问怎么做?
procedure   TForm1.Button3Click(Sender:   TObject);   
  var   
      P:   PByteArray;   
      x,   y:   Integer;   
      Gray:   Integer;   
      Bmp:   TBitmap;   
  begin   
      Bmp   :=   TBitmap.Create;   
      Bmp.Assign(Image1.Picture.Bitmap);   
      Bmp.PixelFormat   :=   pf24bit;   
      for   y   :=   0   to   Bmp.Height   -   1   do   
      begin   
          P   :=   Bmp.ScanLine[y];   
          for   x   :=   0   to   Bmp.Width   -   1   do   
          begin   
              Gray   :=   Max(P[3*x+2],   P[3*x+1]);   
              Gray   :=   Max(Gray,   P[3*x]);   
              P[3*x+2]   :=   Byte(Gray);   
              P[3*x+1]   :=   Byte(Gray);   
              P[3*x]   :=   Byte(Gray);   
          end;   
      end;   
      Canvas.Draw(0,   0,   Bmp);   
      Bmp.Free;   
  end;   

解决方案 »

  1.   

    if Gray>127 then Gray:=255 else Gray:=0;
      

  2.   


    因为你只要黑白2种颜色,黑白的RGB是这样的;黑:RGB(0,0,0) 白:RGB(255,255,255)
    而255/2=127.5,所以判断某个像素值大于127就当成白色(255),否则黑色(0)即可了
    但对一张彩色图片来说只要黑白,效果会很丑,LZ的例子加上下面红色的代码...
    Gray := Max(Gray, P[3*x]);   
    if Gray>127 then
      Gray:=255 
    else
      Gray:=0;

    P[3*x+2] := Gray;
    P[3*x+1] := Gray;   
    P[3*x] := Gray;
      

  3.   

    Image1.Picture.Bitmap.Monochrome := True
      

  4.   

    是灰度图呢,还是黑白图?
    黑白图直接设定一个阈值,比如128,当图像的灰度大于128时,设为255,小于128时,设为0
    图像的灰度值计算有三种方式
    1.gray = 0.299R+0587G+0.114B
    2.gray = RGB中的最大值
    3.gray = RGB的平均值