The conversion between color image and grayscale image

Source: Internet
Author: User
Tags scale image

I. Introduction to COLOR images

In the RGB color space, the image depth and color mapping relationship mainly has true color, pseudo-color and color blending.

True color refers to the composition of a color image of each pixel value, there are r,g,b three primary color components, each base color component directly determines the display device's base color intensity, so that the resulting color is called true color, is the real original color.

Each pixel value of a pseudo-color image is actually an index value or code that is used as the entry address for an entry in the Color lookup table Clut, which can be used to find the strength value that contains the actual r,g,b. The color generated by this method of lookup mapping is called pseudo color. Color lookup Table Clut is a well-prepared table, and the entry address of a table entry is also known as an index number. There is a transformation relationship between the pixel values of the color image itself and the index number of the color lookup table, which can be either system-defined or a user-defined transformation relationship. The color displayed using the found value is true, but not the color of the image itself because it does not fully reflect the color of the original, so it is called a pseudo-color.

The blending color is transformed by the r,g,b component of each pixel as a separate index value, and the corresponding color lookup table is used to find the intensity of the primary color, and the color is produced by the transformed R,g,b intensity value.

Two. Introduction to grayscale Images

Grayscale is the most direct visual feature describing the content of grayscale images. It refers to the color depth of the midpoint of the black and white image, the range is generally from 0 to 255, White is 255, black is 0, so black and white image is also called grayscale image. Gray-scale image matrix elements are usually [0,255], so their data types are generally 8-bit unsigned integers, which is commonly referred to as level 256 grayscale.

Three. Color image conversion to grayscale image

When converting a color image to a grayscale image, you need to calculate the luminance value that is valid for each pixel in the image, and the formula is:

Y = 0.3R + 0.59G + 0.11B

The code is as follows:

Clear All;close all;i = Imread (' lenna.png '); [M N H] = size (I); I2 = Zeros (M, N); for x = 1:m for   y = 1:n      A = Double ([I (X, y, 1) I (x, Y, 2) I (x, Y, 3)]); 
   
    B = [0.3; 0.59; 0.11];      [Gray] = A * B;                                           % calculates the grayscale value      I2 (x, y) = Gray;   Endendimshow (Uint8 (I2));
   

Four. Grayscale image conversion to color image

Converting grayscale images to color images is called pseudo-color processing of grayscale images.

Pseudo-color processing technology is implemented in many ways, such as: gray-scale segmentation, gray-color transformation, filtering and so on. The following is a grayscale-color transformation method, which is the gray image from the sensor into three different features of the R, G, b converter, and then the three different output of the converter to the color display to display the technology.

The mapping relationship is as follows, where R (x, y), g (x, y), B (x, y) represent the color values of the R, G, and B channels, and F (x, y) is the grayscale value of the grayscale image for a particular point, and F is the grayscale value of the selected grayscale image.

The code is as follows:

Clear All;close all;i = Imread (' lenna.jpg '); [m N] = size (I); I2 = Zeros (M, N, 3); for x = 1:m for   y = 1:n      if I (x, y) <= 127                                 % R          I2 (x, y, 1) = 0;< C4/>elseif I (x, y) <= 191          I2 (x, y, 1) = 4 * I (x, y)-510;      else          I2 (x, y, 1) = 255;      End            If I (x, y) <=                                  G          I2 (x, y, 2) = 254-4 * I (x, y);      ElseIf I (x, y) <= 127          I2 (x, y, 2) = 4 * I (x, y)-254;      ElseIf I (x, y) <= 191          I2 (x, y, 2) = 255;      else          I2 (x, y, 2) = 1022-4 * I (x, y);      End            If I (x, y) <=                                 percent B          I2 (x, y, 3) = 255;      ElseIf I (x, y) <= 127          I2 (x, y, 3) = 510-4 * I (x, y);      else          I2 (x, y, 3) = 0;      End   Endendimshow (Uint8 (I2));

The conversion between color image and grayscale image

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.