Image processing algorithm (6)

Source: Internet
Author: User
Vi. Contrast
Scan the image, find the maximum and minimum values of the image brightness, map Max to smaller values such as 255 or 250, and map min to smaller values such as 0 or 5, and re-calculate the brightness of each point.

1 Public   Static   Bool Contrast (Bitmap B, Int Lavel)
2 {
3 If (Lavel <   - 100 ) Return   False ;
4 If (Lavel > 255 ) Return   False ;
5
6 Bitmapdata bmdata = B. lockbits ( New Rectangle ( 0 , 0 , B. Width, B. Height ),
7 Imagelockmode. readwrite, pixelformat. format24bpprgb );
8
9 Double Pixel =   0 , Contrast = ( 100.0   + Lavel) /   100.0 ;
10 Contrast * = Contrast;
11
12 Int Stride = Bmdata. stride;
13 System. intptr scan0 = Bmdata. scan0;
14
15 Unsafe
16 {
17 Byte   * P = ( Byte   * )( Void   * ) Scan0;
18 Int Red, green, blue;
19 Int Noffset = Stride - B. Width * 3 ;
20
21 For ( Int Y =   0 ; Y < B. height; ++ Y)
22 {
23 For ( Int X =   0 ; X < B. width; ++ X)
24 {
25 Blue = P [ 0 ];
26 Green = P [ 1 ];
27 Red = P [ 2 ];
28
29 Pixel = Blue /   255.0 ;
30 Pixel -=   0.5 ;
31 Pixel * = Contrast;
32 Pixel + =   0.5 ;
33 Pixel * =   255 ;
34 If (Pixel <   0 ) Pixel =   0 ;
35 If (Pixel >   255 ) Pixel =   255 ;
36 P [ 0 ] = ( Byte ) Pixel;
37
38 Pixel = Green /   255.0 ;
39 Pixel -=   0.5 ;
40 Pixel * = Contrast;
41 Pixel + =   0.5 ;
42 Pixel * =   255 ;
43 If (Pixel <   0 ) Pixel =   0 ;
44 If (Pixel >   255 ) Pixel =   255 ;
45 P [ 1 ] = ( Byte ) Pixel;
46  
47 Pixel = Red /   255.0 ;
48 Pixel -=   0.5 ;
49 Pixel * = Contrast;
50 Pixel + =   0.5 ;
51 Pixel * =   255 ;
52 If (Pixel <   0 ) Pixel =   0 ;
53 If (Pixel >   255 ) Pixel =   255 ;
54 P [ 2 ] = ( Byte ) Pixel;
55
56 P + =   3 ;
57 }
58 P + = Noffset;
59 }
60 }
61 B. unlockbits (bmdata );
62 Return   True ;
63 }
Related Article

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.