Image processing-salt and pepper noise (noisy effect)

Source: Internet
Author: User
Conversion from: Image Processing: Pretzels (Miscellaneous) posted on Parker read (2794) Comments (0) EDIT favorites

1 .:

2. Implementation principle:
When processing each pixel point, obtain a random value and the value of the probability of occurrence of the specified random point to determine whether to modify the color value of the current pixel, set a random color value.

3. Implementation Code:


1 /// <summary>
2 // Add a miscellaneous
3 /// </Summary>
4 /// <Param name = "IMG"> original image </param>
5 // <Param name = "degree"> specify the probability of occurrence of a noise </param>
6 /// <returns> </returns>
7 public static image saltnoise (image IMG, int degree)
8 {
9 // set the probability to 0--100
10 if (degree & gt; 100) Degree = 100;
11 if (degree <0) Degree = 0;
12
13 int width = IMG. width;
14 int Height = IMG. height;
15
16 rectangle rect = new rectangle (0, 0, width, height );
17 pixelformat pF = pixelformat. format32bppargb;
18
19 // lock bitmap in memory
20 bitmap BMP = new Bitmap (IMG );
21 bitmapdata DATA = BMP. lockbits (rect, imagelockmode. readwrite, Pf );
22
23 unsafe // insecure Mode
24 {
25 // the address of the first pixel data in the image
26 byte * P = (byte *) data. scan0;
27
28 random Rand = new random ();
29
30 For (INT I = 0; I 31 {
32 For (Int J = 0; j <width; j ++)
33 {
34 if (RAND. Next (0,100) <degree)
35 {
36 p [2] = (byte) Rand. Next (0,255 );
37 p [1] = (byte) Rand. Next (0,255 );
38 P [0] = (byte) Rand. Next (0,255 );
39}
40 p + = 4; // see the description below
41}
42}
43
44}
45 // unlock this bitmap from the system memory
46 BMP. unlockbits (data );
47 // return the Modified Image
48 return (image) BMP;
49}

 


1 /// <summary>
2 // Add a miscellaneous
3 /// </Summary>
4 /// <Param name = "IMG"> original image </param>
5 // <Param name = "degree"> specify the probability of occurrence of a noise </param>
6 /// <returns> </returns>
7 public static image saltnoisemirco (image IMG, int degree)
8 {
9 // set the probability to 0---100
10 if (degree <0) Degree = 0;
11 if (degree> 100) Degree = 100;
12
13 // obtain the height and width of the original image
14 int width = IMG. width;
15 int Height = IMG. height;
16
17 // a bitmap of the Instance
18 bitmap BMP = new Bitmap (IMG );
19
20 rectangle rect = new rectangle (0, 0, width, height );
21 pixelformat format = pixelformat. format32bppargb;
22
23 bitmapdata DATA = BMP. lockbits (rect, imagelockmode. readwrite, format );
24
25 intptr = data. scan0;
26
27 // declare an array of fixed lengths to store image data
28 int numbytes = width * height * 4;
29 byte [] rgbvalues = new byte [numbytes];
30
31 // copy image data from an unmanaged memory pointer to a hosted Array
32 marshal. Copy (PTR, rgbvalues, 0, numbytes );
33
34 random Rand = new random ();
35
36 // modify the color value that meets the condition
37 For (INT I = 0; I <numbytes; I + = 4)
38 {
39 if (RAND. Next (0,100) <degree)
40 {
41 rgbvalues [I] = (byte) Rand. Next (0,255 );
42 rgbvalues [I + 1] = (byte) Rand. Next (0,255 );
43 rgbvalues [I + 2] = (byte) Rand. Next (0,255 );
44}
45}
46
47 // copy image data from a hosted array to an unmanaged memory pointer
48 marshal. Copy (rgbvalues, 0, PTR, numbytes );
49 BMP. unlockbits (data );
50
51 return (image) BMP;
52
53}

 

4. program description:
A. Using the lockbits method for image processing is much more efficient than using the getpixel method and setpixel Method for comparison and softening (smoothing) processing.
B. About "method 1"
(1) During compilation, an error may occur, indicating that "Unsafe code only appears when/unsafe compilation is used". The process is as follows:
Right-click the project and select "properties" --> "generate" --> select "allow Insecure code" --> "save ".
(2) P + = 4:
Because pixelformat format = pixelformat. format32bppargb;
Specify the color data format of each pixel in the image to 32 bits per pixel. The Alpha, red, green, and blue components use 8 bits each.
Therefore, the storage of pixel color values of an image uses four bytes to indicate B, G, R, and a, respectively. Each occupies one byte,
P [0]: B, P [1]: g, P [2]: R, P [3]:, P + = 4 indicates the starting position of the next pixel of the current pixel.
C. About "method 2"
(1) Marshal. Copy () Copies image data from an unmanaged memory pointer to a hosted array to avoid Insecure code.
(2) int numbytes = width * height * 4;
Length of the array for storing image pixel data = Total number of pixels * The number of bytes occupied by each pixel.

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.