Solved

Display RGB channels of image

Posted on 2011-02-16
4
439 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now