Part 1 Article I wrote a method to convert a color image into a black/white image. This method traverses the image color and then uses the formula to convert the color into a black/white image. The efficiency is very low.
I made the following changes today, which has improved the efficiency by about 1000 times.
The test diagram is as follows:
214452542 is the execution time of the previous method.
250169 indicates the execution time of this method.
Figure 5-4 shows the result of generating a graph using this method.
The formula is changed to Y = 0.299 * r + 0.114 * g + 0.587b.
Code : 1 /**/ /// <Summary>
2 /// Quickly turn a color image into a black image or a black image-currently only applicable to JPG Images
3 /// </Summary>
4 /// <Param name = "filepath"> Color Image address </Param>
5 /// <Returns> Returned black and white images </Returns>
6 Public Static Bitmap quickwhiteandblack ( String Filepath)
7 {
8
9 // Create a bitmap object from a file
10 Bitmap BMP = New Bitmap (filepath );
11
12 // Lock bitmap to system memory
13 Rectangle rect = New Rectangle ( 0 , 0 , BMP. Width, BMP. Height );
14 // Obtain bitmapdata
15 System. Drawing. imaging. bitmapdata BMP data =
16 BMP. lockbits (rect, system. Drawing. imaging. imagelockmode. readwrite,
17 BMP. pixelformat );
18
19 // The address of the first pixel in the bitmap. It can also be seen as the first scanned line in the bitmap.
20 Intptr = BMP data. scan0;
21
22 // Store the information of the bitmap object in the byte array.
23 // Assume that a pixel in the bitmap contains 3 bytes, that is, 24bit.
24 Int Bytes = BMP. Width * BMP. Height * 3 ;
25 Byte [] Rgbvalues = New Byte [Bytes];
26
27 // Copy GRB information to byte array
28 System. runtime. interopservices. Marshal. Copy (PTR, rgbvalues, 0 , Bytes );
29
30 // Brightness Based on Y = 0.299 * r + 0.114 * g + 0.587b, y
31 For ( Int Counter = 0 ; Counter < Rgbvalues. length; Counter + = 3 )
32 {
33 Byte Value = ( Byte ) (Rgbvalues [Counter] * 0.299 + Rgbvalues [Counter + 2 ] * 0.114 + Rgbvalues [Counter + 1 ] * 0.587 );
34 Rgbvalues [Counter] = Value;
35 Rgbvalues [Counter + 1 ] = Value;
36 Rgbvalues [Counter + 2 ] = Value;
37 }
38
39 // Copy the modified byte [] to the original bitmap.
40 System. runtime. interopservices. Marshal. Copy (rgbvalues, 0 , PTR, bytes );
41
42 // Unlock bitmap
43 BMP. unlockbits (BMP data );
44 Return BMP;
45
46 }
Address: http://www.cnblogs.com/jillzhang/archive/2006/10/09/524571.html
Special thanks: yaotong
Ahnan
Mu Feng
The three brothers helped me in my previous article!