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. GrayDegree can also be considered as brightness. In short, it is the depth of the color. !
1: For exampleAs a result, 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: GrayDegree is no color,ItsRGBAll colors are 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 the pixel instead of the actual RGB value. In QT, there are:Qimage: format_indexed8That is, an index value is stored 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: Based onAlgorithm: Convert the RGB value in each pixel of the rgb32 color image to 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:First, create an empty index8 image.
2: According to an 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:
[CPP]View plaincopy
-
- For(IntI = 0; I <width; I ++)
-
- {
- For(IntJ = 0; j
-
- {
-
- Qrgb pixel = iimage-> pixel (I, j );
-
- IntR = qred (pixel );
-
- IntG = qgreen (pixel );
-
- IntB = qblue (pixel );
- Igray-> setpixel (I, j, qgray (R, G, B ));
-
- }
-
- }
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:To usePixel (), qred (), qgreen (), qblue ()To retrieve 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!
This itemCodeImplementation: The Green component value is directly used 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:The qgray () function used to calculate the gray index value. According to the QT documentation, 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:
[CPP] View plaincopy
-
- Qimage colourimg ("Colourimage.bmp");
-
- Qsize colourimgsize = colourimg. Size ();
- IntWidth = colourimgsize. rwidth ();
-
- IntHeight = colourimgsize. rheight ();
-
- UnsignedChar* Colourimgdataptr = colourimg. Bits ();
-
- Qimage grayimg (colourimgsize, qimage: format_indexed8 );
-
- UnsignedChar* Grayimgdataptr = grayimg. Bits ();
-
-
- // The for loop below directly uses the green component value as the gray index value
- For(IntI = 0; I
-
- {
-
- For(IntJ = 0; j <width; j ++)
-
- {
- * Grayimgdataptr = * (colourimgdataptr + 1); colourimgdataptr + = 4; grayimgdataptr ++;
-
- }
-
- }
-
-
- Qvector <qrgb> graycolourtable;
-
- UnsignedIntRGB = 0;
- For(IntI = 0; I <256; I ++)
-
- {
-
- Graycolourtable. append (RGB );
-
- RGB + = 0x00010101;
-
- }
-
-
- Grayimg. setcolourtable (raycolourtable );
-
- 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.
[CPP] View plaincopy
- Qbyte rgbtogray (qbyte R, qbyte g, qbyte B)
- {
- Return(Qbyte) (quint32) (r <5) + (r <2) + (r <1) + (quint32) (G <6) + (G <3) + (G <1) + G)
- + (Quint32) (B <4)-B)> 7 );
- }
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.