Qt generates Grayscale Images

Source: Internet
Author: User

A large amount of basic image processing knowledge is used in the project. The generation of grayscale images is an important part.

First, add some basic knowledge:

Bytes ----------------------------------------------------------------------------------------------------------------------------

I. Grayscale Images

A grayscale image is a black-and-white image. The entire image is only black and white to varying degrees. Gray scale can also be considered as brightness. In short, it is the intensity of colors!

 

1: If we use eight bits to store grayscale images. There are a total of 256 combinations. That is equivalent to dividing from: pure black to pure white into 256 gray levels. This corresponds to 256 gray-scale values! If a number is used, each number 0-corresponds to a gray scale!

 

2: grayscale means no color, and its RGB color components are all equal. For example, RGB (20, 20, 20 ). In this case, we can replace the actual gray value with the value of a certain component in RGB! For example, in the above example, we can use 20 instead of the gray scale! This is a one-to-one ing relationship!

Bytes --------------------------------------------------------------------------------------------------------------

Ii. Color Table

 

 

A color table is a table that represents all colors currently. We know that any color can be represented by an RGB value. In this way, we can design a table where each element is an RGB value, so that all colors are represented by an RGB value!

Bytes --------------------------------------------------------------------------------------------------------------

Iii. Color index mode

 

 

Each pixel of an image can directly store its RGB value! Of course: We can also store an index value. We can use this index value to search for the RGB values of the corresponding color in the corresponding color table. In this mode, the index value is stored in pixels instead of the actual RGB value. In QT, qimage: format_indexed8 is used to store an index value in 8 bits.

Bytes --------------------------------------------------------------------------------------------------------------

Iv. grayscale index chart:

 

For an 8-level grayscale image: Since there are a total of 256 gray-scale images, we can design a color table to store 256 RGB values in it! The three components of each RGB value are equal. This table can be used to indicate all gray levels!

The gray index graph stores the index values of each integer. The data in these images cannot be displayed because they do not have the actual RGB value. In the actual graph, the index value is used to search for the color table created above, find the corresponding gray-level RGB value, and then perform actual plotting and other operations.

With the foundation of the last two steps, the key to the problem is: how to obtain the gray index value?

 

========================================================== ========================================================== ========================================

 

With the above basic knowledge, we can next look at how to convert from a color image to a grayscale image.

I. Overall process:

1. According to an algorithm: Convert the RGB values in each pixel of the rgb32 color image into an integer gray value.

2: Use the calculated value to replace each RGB in the source image.

In this way, you can convert a color image into a grayscale image.


However, this process has the following problems:

1: how to calculate the gray value from the original RGB value of the color chart?

2: The RGB values of each pixel are replaced by the gray value, which is the same. This can be replaced by a gray value instead of three. Using three is a waste of space.

Bytes --------------------------------------------------------------------------------------------------------------

For Question 1: The gray value calculation methods are as follows:

1. Floating Point Algorithm: Gray = r * 0.3 + G * 0.59 + B * 0.11

2. Integer method: Gray = (R * 30 + G * 59 + + B * 11)/100

3. Shift Method: Gray = (R * 28 + G * 151 + B * 77)> 8;

4. Average Method: Gray = (R + G + B)/3;

5. Green only: Gray = g;

 

The difference between various methods is: accuracy! That is, the effects of the obtained grayscale images are different! Floating-point calculation has the best effect, but green only has the worst effect. It depends on your actual needs!

 

Bytes --------------------------------------------------------------------------------------------------------------

For Question 2: We can store only one grayscale value in each pixel, and then design a color table to find its true color through this grayscale value. In this way, you can use the gray index graph above. That is to say, what is stored in the figure is not the actual RGB value, but the gray value. Then, a color table is designed. In this way, the two can represent the entire grayscale image. In addition, the total space of the two is much smaller than that of the non-index mode, and the search speed is faster.

 

Therefore, it is best to store Grayscale Images in index mode. In QT, the format of qimage is index8. That is, an 8-bit index can be used to specify 256 gray-scale values.

So: the overall process from a color chart to a gray index chart is:

 

(For example, convert rgb32 to index8)

1: Create an empty index8 image.

2: according to a certain algorithm: Convert the RGB values in each pixel of the rgb32 color image into an integer, which is stored in each pixel of the index8 grayscale image.

3: Build a color table.

4: Based on the integer value stored in the index image, go to the grayscale table to find the corresponding grayscale for the actual diagram!

 

========================================================== ========================================================== ========================================

 

OK. Now the method is available. We can implement it below.

 

1: the simplest method:


For (INT I = 0; I <width; I ++) <br/> {<br/> for (Int J = 0; j <peight; j ++) <br/>{< br/> qrgb pixel = iimage-> pixel (I, j); <br/> int r = qred (pixel ); <br/> int G = qgreen (pixel); <br/> int B = qblue (pixel); <br/> igray-> setpixel (I, j, qgray (R, G, B); <br/>}< br/>}

 

That is, use pixel () to obtain the qrgb value of each pixel. Then, qred (), qgreen (), and qblue () are used to obtain the values of RGB components. Then calculate the gray value. And then assign the pixel again.

 

But when you run the function, you will find that for an image of 600*800 size, this function must run for 30 s +, which is obviously intolerable.

 

Bytes --------------------------------------------------------------------------------------------------------------

The above method is simple and straightforward, but the efficiency is too high, so we need to optimize the efficiency, mainly from the following two aspects:

1: Use pixel (), qred (), qgreen (), and qblue () to traverse and obtain the RGB component of each pixel. Function implementation: it must be a search process, especially the pixel () function that obtains RGB values based on Pixel positions (). Every time you perform a complete search, the efficiency is obviously relatively low, especially the constant search! Since the data is continuously stored in the memory, we can retrieve the RGB component by moving the pointer. Only one pointer can be moved at a time. This method of moving pointer is obviously faster than full search!

 

 

Implementation of this Code: it directly uses the green component value as the gray index value!

The Optimization of the previous method is that it does not use system functions, but uses a moving pointer to obtain RGB components. It stores all gray index values in the index8 grayscale image. At last, an 8-level grayscale chart is created and assigned to it. In this way, the system will automatically (no manual search is required) use the gray index value stored in the image to search for the corresponding gray RGB value in the color table!

 

Bytes --------------------------------------------------------------------------------------------------------------

 

2: Calculate the gray index value function qgray (). According to the QT document, the calculation process is: (R * 11 + G * 16 + 5*5)/32. multiplication and division are a large amount of computing. It is best to replace them with shift operations!

 

(This involves a problem: I thought that multiplication and division should be converted to shift operations in the register, so multiplication and division should be no different in efficiency. However, this is not the case, because: If you use multiplication and division, the transformed multiplication command consumes a lot of CPU cycles at runtime, which virtually reduces the efficiency! Therefore, when performing high-performance computing, it is best to replace multiplication and division with shift operations)

 

========================================================== ========================================================== ================

After knowing how to optimize it, let's take a look at how to implement it in QT. On the Internet, I found a method, which is excerpted as follows:

Qimage colourimg ("colourimage.bmp"); <br/> qsize colourimgsize = colourimg. size (); <br/> int width = colourimgsize. rwidth (); <br/> int Height = colourimgsize. rheight (); <br/> unsigned char * colourimgdataptr = colourimg. BITs (); <br/> qimage grayimg (colourimgsize, qimage: format_indexed8); <br/> unsigned char * grayimgdataptr = grayimg. BITs (); </P> <p> // The for loop below directly uses the green component value as the gray index value <br/> for (INT I = 0; I <peight; I ++) <br/>{< br/> for (Int J = 0; j <width; j ++) <br/>{< br/> * grayimgdataptr = * (colourimgdataptr + 1); colourimgdataptr + = 4; grayimgdataptr ++; <br/>}</P> <p> qvector <qrgb> graycolourtable; <br/> unsigned int RGB = 0; <br/> for (INT I = 0; I <256; I ++) <br/>{< br/> graycolourtable. append (RGB); <br/> RGB + = 0x00010101; <br/>}</P> <p> grayimg. setcolourtable (raycolourtable); <br/> graimg. save ("grayimage.bmp", "BMP ");

 

Implementation of this Code: it directly uses the green component value as the gray index value!

The Optimization of the previous method is that it does not use system functions, but uses a moving pointer to obtain RGB components. It stores all gray index values in the index8 grayscale image. At last, an 8-level grayscale chart is created and assigned to it. In this way, the system will automatically (no manual search is required) use the gray index value stored in the image to search for the corresponding gray RGB value in the color table!

 

Bytes --------------------------------------------------------------------------------------------------------------

Based on the above method, we need to pay attention to whether the effect can meet the requirements, so we may use a gray index value calculation method with higher accuracy. This involves a lot of operations. Remember to replace multiplication and division operations with shift operations.

Qbyte rgbtogray (qbyte R, qbyte g, qbyte B) <br/>{< br/> return (qbyte) (quint32) (r <5) + (r <2) + (r <1) + (quint32) (g <6) + (G <3) + (G <1) + G) <br/> + (quint32) (B <4)-B)> 7); <br/>}

This is the optimal real-time gray index value calculation algorithm found on the Internet. Instead of multiplication and division, a large number of shift operations are used.

Bytes --------------------------------------------------------------------------------------------------------------

 

Now, the above Code has been tested and there is no problem.

 

 

 

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.