本文介绍了以编程方式将加载的位图中的颜色交换为红色,绿色,蓝色或灰色,逐像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此下载源代码:有什么关系?



其次,代码

  function RGBToRed(RGBColor:TColor):TColor; 
var
红色:Byte;
begin
//不确定该颜色的算法
结果:= RGB(红,红,红);
end;

无意义。返回值未定义。 RGB 函数采用0到255范围内的红色,绿色和蓝色强度,并返回具有这些组件的 TColor 。例如, RGB(255,0,0)是纯红色的,即255红色,0绿色和0蓝色。然而,在上面的代码中, Red 未初始化,因此结果可以是任何灰色颜色code> Red 恰好是)。 [你可能知道,如果你混合等量的红,绿和蓝,就像你一样。]例如, Red 一次你运行应用程序,然后结果将是灰色 RGB(45,45,45)。下一次它可能是163。



也许你想要一个接受 TColor 的函数,它?然后 GetRValue 会做,当然还有 GetGValue GetBValue



或者,也许你想从给定的 TColor 创建一个灰色您在使用Photoshop从彩色图像创建灰度图像时执行。然后,如果 Color TColor ,则相应的灰色值为

  gray:=(GetRValue(Color)+ GetGValue(Color)+ GetBValue(Color))div 3; 

还有其他方法(导致细微不同的灰度图像,但这是最简单的和最快))。 (例如,您可以将颜色从RGB转换为HSV或HSL,然后将饱和度设置为零。)



更新 p>

我想我看到你想做什么。也许你想提取红色,绿色和蓝色通道。也就是说,您要将每个像素 RGB(R,G,B)转换为 RGB(R,0,0)在红色的情况下。如果这是你想做的,那么你应该做

  function RGBToRed(RGBColor:TColor):TColor; 
begin
结果:= RGB(GetRValue(RGBColor),0,0);
end;

,对于其他组件也是如此,

  function RGBToGreen(RGBColor:TColor):TColor; 
begin
结果:= RGB(0,GetGValue(RGBColor),0);
end;

  function RGBToBlue(RGBColor:TColor):TColor; 
begin
结果:= RGB(0,0,GetBValue(RGBColor));
end;

或者



或者你只是想让图像灰度,然后将其红色(或绿色或蓝色)。那么你需要重型机械。理论上,您希望将 HSV(h,s,v)中的每个像素替换为 HSV(0,s,v) HSV(120,s,v),以及 HSV(240,s,v) code>在蓝色的情况下。或者,也许你还希望将饱和度 s 设置为1。



我使用以下的玫瑰花RGB和HSV:

  function RGBToHSV(const Color:TRGB):THSV; 
var
cmax,cmin,cdiff:real;
begin
cmax:= MaxComponent(Color);
cmin:= MinComponent(Color);
cdiff:= cmax - cmin;

with Color,result do
begin

// Hue
如果cmax = cmin then
hsvHue:= 0
否则如果cmax = rgbRed,则
hsvHue:=(60 *(rgbBlue - rgbRed)/ cdiff)
else if cmax = cdiff)+ 120
else
hsvHue:=(60 *(rgbRed-rgbGreen)/ cdiff)+ 240;

hsvHue:= Fix360(hsvHue);

//饱和度
如果cmax = 0那么
hsvSaturation:= 0
else
hsvSaturation:= 1 - cmin /

//价值
hsvValue:= cmax;

end;

end;

  function HSVToRGB(const Color:THSV):TRGB; 
var
hi:integer;
f,q,p,t:real;
begin

with Color do
begin

hi:= floor(hsvHue / 60)mod 6;
f:= hsvHue / 60 - floor(hsvHue / 60);
p:= hsvValue *(1 - hsvSaturation);
q:= hsvValue *(1 - f * hsvSaturation);
t:= hsvValue *(1 - (1 - f)* hsvSaturation);

case hi
0:result:= RGB(hsvValue,t,p);
1:result:= RGB(q,hsvValue,p);
2:result:= RGB(p,hsvValue,t);
3:result:= RGB(p,q,hsvValue);
4:result:= RGB(t,p,hsvValue);
5:result:= RGB(hsvValue,p,q);
end;

end;

end;

其中

  type 
TRGB = record
rgbRed,//红色强度在0和1之间
rgbGreen,//绿色强度在0和1之间
rgbBlue:double; // blue intensity between 0 and 1
end;

THSV = record
hsvHue,// 0和360之间的色相角
hsv饱和度,// 0(灰色)和1(全色)之间的饱和度
hsvValue :double; // value between 0(dark)and 1(normal)
end;

在绿色的情况下,最终结果将类似于





在第一种情况下(色调固定为120)和





1)。



或者



与编辑。你想将大部分蓝色转换为大多是绿色的东西。但是有一千种方法这样做!一个很简单的方法,当然是交换蓝色和绿色通道,也就是用替换 RGB(r,g,b) RGB(r,b,g)或者显式地,

  function SwapBlueGreen ):TColor; 
begin
result:= RGB(GetRValue(Color),GetBValue(Color),GetGValue(Color));
end;

然后你会得到类似



img src =http://english.rejbrand.se/algosim/manual/pmproc/swapRG.jpgalt =R和G频道互换/>



其中红色和绿色通道已被交换。注意红色的棍子现在是绿色的,绿色的草现在是红色的。



欢迎来到pixmap操作的世界!有很多容易和有趣的事情要做。我的一些例子:


Download source code here: http://www.eyeClaxton.com/download/delphi/ColorSwap.zip

Yes, I want to convert something "mostly blue" to something "mostly green".

I would like to take a original bitmap (light blue) and change the colors (Pixel by Pixel) to the red, green, blue and gray equivalence relation. To get an idea of what I mean, I have include the source code and a screen shot. Any help would be greatly appreciated. If more information is needed, please feel free to ask.

If you could take a look at the code below, I have three functions that I'm looking for help on. The functions "RGBToRed, RGBToGreen and RGBToRed" I can't seem to come up with the right formulas.

unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TMainFrm = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    Label2: TLabel;
    Button1: TButton;
    BeforeImage1: TImage;
    AfterImage1: TImage;
    RadioGroup1: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainFrm: TMainFrm;

implementation

{$R *.DFM}
function RGBToGray(RGBColor: TColor): TColor;
var
  Gray: Byte;
begin
  Gray := Round(
    (0.90 * GetRValue(RGBColor)) +
    (0.88 * GetGValue(RGBColor)) +
    (0.33 * GetBValue(RGBColor)));

  Result := RGB(Gray, Gray, Gray);
end;

function RGBToRed(RGBColor: TColor): TColor;
var
  Red: Byte;
begin
  // Not sure of the algorithm for this color
  Result := RGB(Red, Red, Red);
end;

function RGBToGreen(RGBColor: TColor): TColor;
var
  Green: Byte;
begin
  // Not sure of the algorithm for this color
  Result := RGB(Green, Green, Green);
end;

function RGBToBlue(RGBColor: TColor): TColor;
var
  Blue: Byte;
begin
  // Not sure of the algorithm for this color
  Result := RGB(Blue, Blue, Blue);
end;

procedure TMainFrm.FormCreate(Sender: TObject);
begin
  BeforeImage1.Picture.LoadFromFile('Images\RightCenter.bmp');
end;

procedure TMainFrm.Button1Click(Sender: TObject);
var
  Bitmap: TBitmap;
  I, X: Integer;
  Color: Integer;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('Images\RightCenter.bmp');

    for X := 0 to Bitmap.Height do
    begin
      for I := 0 to Bitmap.Width do
      begin
        Color := ColorToRGB(Bitmap.Canvas.Pixels[I, X]);

        case Color of
          $00000000: ;   // Skip any Color Here!
        else
          case RadioGroup1.ItemIndex of
            0: Bitmap.Canvas.Pixels[I, X] := RGBToBlue(Color);
            1: Bitmap.Canvas.Pixels[I, X] := RGBToRed(Color);
            2: Bitmap.Canvas.Pixels[I, X] := RGBToGreen(Color);
            3: Bitmap.Canvas.Pixels[I, X] := RGBToGray(Color);
          end;
        end;
      end;
    end;
    AfterImage1.Picture.Graphic := Bitmap;
  finally
    Bitmap.Free;
  end;
end;

end.

Okay, I apologize for not making it clearer. I'm trying to take a bitmap (blue in color) and swap the blue pixels with another color. Like the shots below.

解决方案

I have no idea what you are trying to achieve. First, what does the question have to do with equivalence relations?

Secondly, the code

function RGBToRed(RGBColor: TColor): TColor;
var
  Red: Byte;
begin
  // Not sure of the algorithm for this color
  Result := RGB(Red, Red, Red);
end;

is meaningless. The return value is undefined. The RGB functions takes red, green, and blue intensities in the range 0 to 255 and returns the TColor having these components. For instance, RGB(255, 0, 0) is pure red, that is, 255 red, 0 green, and 0 blue. In the code above, however, Red is not initialized, so Result can be any grey colour (depending on what Red happens to be). [And, as you probably know, if you mix equal amounts of red, green, and blue, as you do, you get grey.] For instance, Red might happen to be 45 one time you run the application, and then the result will be the grey colour RGB(45, 45, 45). Next time it might be 163.

Maybe you want a function that accepts a TColor and returns the red component of it? Then GetRValue will do, and - of course - there is also GetGValue and GetBValue.

Or, maybe you want to create a grey colour from a given TColor, as you do when you use Photoshop to create a greyscale image from a colour image. Then, if Color is the TColor, the appropriate grey value is

grey := (GetRValue(Color) + GetGValue(Color) + GetBValue(Color)) div 3;

There are other ways of doing this (resulting in subtly different greyscale images, but this is the easiest (and fastest)). (For instance, you can convert the colour from RGB to HSV or HSL and then set the saturation equal to zero.)

Update

I think I see what you want to do. Maybe you want to extract the red, green, and blue channels. That is, you want to transform each pixel RGB(R, G, B) to RGB(R, 0, 0) in the red case. If this is what you want to do, then you should do

function RGBToRed(RGBColor: TColor): TColor;
begin
  Result := RGB(GetRValue(RGBColor), 0, 0);
end;

and similarly for the other components, that is

function RGBToGreen(RGBColor: TColor): TColor;
begin
  Result := RGB(0, GetGValue(RGBColor), 0);
end;

and

function RGBToBlue(RGBColor: TColor): TColor;
begin
  Result := RGB(0, 0, GetBValue(RGBColor));
end;

Or, maybe

Or maybe you just want to make the image greyscale, and then "colour" it red (or green, or blue). Then you need the heavy machinery. Theoretically, you wish to replace each pixel from HSV(h, s, v) to HSV(0, s, v) in the red case, HSV(120, s, v) in the green case, and HSV(240, s, v) in the blue case. Or, maybe you also wish to set the saturation s to 1.

I use the following rutines to convert between RGB and HSV:

function RGBToHSV(const Color: TRGB): THSV;
var
  cmax, cmin, cdiff: real;
begin
  cmax := MaxComponent(Color);
  cmin := MinComponent(Color);
  cdiff := cmax - cmin;

  with Color, result do
  begin

    // Hue
    if cmax = cmin then
      hsvHue := 0
    else if cmax = rgbRed then
      hsvHue := (60 * (rgbGreen - rgbBlue) / cdiff)
    else if cmax = rgbGreen then
      hsvHue := (60 * (rgbBlue - rgbRed) / cdiff) + 120
    else
      hsvHue := (60 * (rgbRed - rgbGreen) / cdiff) + 240;

    hsvHue := Fix360(hsvHue);

    // Saturation
    if cmax = 0 then
      hsvSaturation := 0
    else
      hsvSaturation := 1 - cmin / cmax;

    // Value
    hsvValue := cmax;

  end;

end;

and

function HSVToRGB(const Color: THSV): TRGB;
var
  hi: integer;
  f, q, p, t: real;
begin

  with Color do
  begin

    hi := floor(hsvHue / 60) mod 6;
    f := hsvHue / 60 - floor(hsvHue / 60);
    p := hsvValue * (1 - hsvSaturation);
    q := hsvValue * (1 - f * hsvSaturation);
    t := hsvValue * (1 - (1 - f) * hsvSaturation);

    case hi of
      0: result := RGB(hsvValue, t, p);
      1: result := RGB(q, hsvValue, p);
      2: result := RGB(p, hsvValue, t);
      3: result := RGB(p, q, hsvValue);
      4: result := RGB(t, p, hsvValue);
      5: result := RGB(hsvValue, p, q);
    end;

  end;

end;

where

type
  TRGB = record
    rgbRed,            // red intensity between 0 and 1
    rgbGreen,          // green intensity between 0 and 1
    rgbBlue: double;   // blue intensity between 0 and 1
  end;

  THSV = record
    hsvHue,            // hue angle between 0 and 360
    hsvSaturation,     // saturation between 0 (grey) and 1 (full colour)
    hsvValue: double;  // value between 0 (dark) and 1 ("normal")
  end;

The end result, in the green case, will be like

in the first case (hue fixed to 120) and

in the latter case (hue fixed to 120, saturation fixed to 1).

Or, possibly

Your question is very vauge, even with the edit. You want to convert something "mostly blue" to something "mostly green". But there are a thousand ways of doing that! A very simple way, of course, is just to swap the blue and green channels, that is, replace RGB(r, g, b) with RGB(r, b, g), or, explicitly,

function SwapBlueGreen(Color: TColor): TColor;
begin
  result := RGB(GetRValue(Color), GetBValue(Color), GetGValue(Color));
end;

Then you get something like

where the red and green channels have been swapped. Notice that the red stick is now green, and the green grass is now red. What's white remains white.

Finally

Welcome to the world of pixmap manipulation! There is a lot of easy and fun things to do. Some more of my examples: http://english.rejbrand.se/algosim/manual/pmproc/pmproc.html

这篇关于以编程方式将加载的位图中的颜色交换为红色,绿色,蓝色或灰色,逐像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:15