Solved

Display RGB channels of image

Posted on 2011-02-16
4
443 Views
Last Modified: 2012-05-11
I have an image inside a TImage component, I want to display each channel color inside a different TImage.
0
Comment
Question by:dc_mo
  • 2
4 Comments
 
LVL 14

Expert Comment

by:systan
ID: 34907266
0
 

Author Comment

by:dc_mo
ID: 34907374
Thanks but they're not relevant to my question.
0
 
LVL 12

Accepted Solution

by:
Hypo earned 500 total points
ID: 34907619
Hi,
Check out the sample below... Load an image into Image1, and then press the button...

The Channels are singled out by using the pmMask mode on the Canvas.pen object... This mode perofrms an AND with the Color property of the Pen and Brush, on each pixel of the area it draws to... so a simple way to get RGB channels is just to set this mode along with the color you want to single out, and then just draw a rectangle over the whole image.

(I Attached the .pas and .dfm files as code snippets)

/Hypo


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    ImageRed: TImage;
    ImageGreen: TImage;
    ImageBlue: TImage;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var aColorBmp : TBitmap;
begin
  aColorBmp := TBitmap.Create;
  try
    // Get the picture into a bitmap...
    aColorBmp.PixelFormat := pf32bit;
    aColorBmp.SEtSize(Image1.Picture.Width, Image1.Picture.Height);
    aColorBmp.Canvas.Draw(0, 0, Image1.Picture.Graphic);
    // Mask out red component...
    ImageRed.Picture.Bitmap.Assign(aColorBmp);
    ImageRed.Picture.Bitmap.Canvas.Pen.Mode := pmMask;
    ImageRed.Picture.Bitmap.Canvas.Pen.Color := RGB(255, 0, 0);
    ImageRed.Picture.Bitmap.Canvas.Brush.Color := RGB(255, 0, 0);
    ImageRed.Picture.Bitmap.Canvas.Rectangle(0, 0, ImageRed.Picture.Width, ImageRed.Picture.Height);
    // Mask out red component...
    ImageGreen.Picture.Bitmap.Assign(aColorBmp);
    ImageGreen.Picture.Bitmap.Canvas.Pen.Mode := pmMask;
    ImageGreen.Picture.Bitmap.Canvas.Pen.Color := RGB(0, 255, 0);
    ImageGreen.Picture.Bitmap.Canvas.Brush.Color := RGB(0, 255, 0);
    ImageGreen.Picture.Bitmap.Canvas.Rectangle(0, 0, ImageGreen.Picture.Width, ImageGreen.Picture.Height);
    // Mask out red component...
    ImageBlue.Picture.Bitmap.Assign(aColorBmp);
    ImageBlue.Picture.Bitmap.Canvas.Pen.Mode := pmMask;
    ImageBlue.Picture.Bitmap.Canvas.Pen.Color := RGB(0, 0, 255);
    ImageBlue.Picture.Bitmap.Canvas.Brush.Color := RGB(0, 0, 255);
    ImageBlue.Picture.Bitmap.Canvas.Rectangle(0, 0, ImageBlue.Picture.Width, ImageBlue.Picture.Height);
  finally
    FreeAndNil(aColorBmp);
  end;
end;

end.

Open in new window

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 526
  ClientWidth = 688
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 8
    Top = 8
    Width = 169
    Height = 169
    Stretch = True
  end
  object ImageRed: TImage
    Left = 8
    Top = 214
    Width = 169
    Height = 169
    Stretch = True
  end
  object ImageGreen: TImage
    Left = 183
    Top = 214
    Width = 169
    Height = 169
    Stretch = True
  end
  object ImageBlue: TImage
    Left = 358
    Top = 214
    Width = 169
    Height = 169
    Stretch = True
  end
  object Button1: TButton
    Left = 102
    Top = 183
    Width = 75
    Height = 25
    Caption = 'View RGB'
    TabOrder = 0
    OnClick = Button1Click
  end
end

Open in new window

0
 

Author Comment

by:dc_mo
ID: 34908086
works well, thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 84
HTML text in the body of an email (delphi code) 12 166
Delphi IDE crash without error message ... 7 99
Delphi: Connect to running MS Outlook 4 117
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question