The principle and realization of grayscale of image

Source: Internet
Author: User

First, the basic principle of grayscale processing of images

The process of transforming color images into grayscale images becomes the grayscale processing of images. The color of each pixel in a color image is determined by the R, G, and b three components, and each component has a value of 255, so that a pixel can have a range of more than 16 million (255*255*255) color variations. The Gray image is a special color image with the same three components of R, Gand B , and the range of one pixel varies from 255 to four. Therefore, in the digital image processing species, the image of various formats is transformed into grayscale images so that the subsequent images can be calculated less. The description of grayscale image still reflects the distribution and characteristics of the whole and local chroma and luminance levels of the whole image as well as the color image. Grayscale processing of images can be achieved in two ways.

The first method causes the average of the R, G, and b three components of each pixel to be averaged, and then assigns the average value to the three components of the pixel.

The second method is based on the YUV color space, the physical meaning of Y component is the brightness of the point, reflected by this value brightness level, according to the RGB and YUV color space change relationship can be established brightness Y and R, G, b three color components corresponding: Y=0.3R+0.59G+0.11B, The grayscale value of the image is expressed with this luminance value.

Second, the implementation of image grayscale with Delphi:

Procedure Tform1.bitbtn1click (Sender:tobject);

Var

P:pbytearray;

Pbytearray-defined format

Pbytearray = ^tbytearray;

Tbytearray = array[0..32767] of Byte;

Changedbmp:tbitmap;

Gray,x,y:integer;

Testbmp:tbitmap; Bitmap during processing

Begin

Testbmp:=tbitmap.create;

Changedbmp:=tbitmap.create;

Testbmp.assign (Image1. picture);

For y: = 0 to Testbmp.height-1 do

Begin

Get the pixel information for each row

P: = Testbmp.scanline[y];

For x: = 0 to Testbmp.width-1 do

Begin

The method of changing YUV and RGB color space is used here, i.e. y=0.3r+0.59g+0.11b

Gray: = Round (p[3 * x + 2] * 0.3 + p[3 * x + 1] * 0.59

+ p[3 * x] * 0.11);

Because it is a 24-bit true color, a pixel is three bytes.

P[3 * x + 2]: = Byte (Gray);

P[3 * x + 1]: = Byte (Gray);

P[3 * x]: = Byte (Gray);

The gray value must be between 0~255

End

Changedbmp.assign (testbmp);

Paintbox1.canvas.copymode:=srccopy;

PaintBox1.Canvas.Draw (0,0,changedbmp);//Redraw the image with the Paintbox control;

End

Three, the attention matters:

The program applies for testbmp, willbechangedbmp, so when the program is initialized, be careful to create:

Testbmp:=tbitmap.create;

Changedbmp:=tbitmap.create;

Attention should be paid to testbmp.destory and changedbmp.destory after the procedure is finished.

Four, the effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.