Anda di halaman 1dari 2

Computer Graphics Lab # 8

The Negative Image Filter


We can implement an image filter that resembles film negatives by inverting every pixels color components. The negative image filter subtracts 1 from each color using ColorMatrix. The transform being applied can also be expressed as implementing the bitwise compliment operator on each pixel. Images are drawn on the window Form using the Picturebox tool, but we use DrawImage(..) method only to manipulate the image within the memory before drawing it into Form. The following example image shows two images drawn on the form with two Pictureboxes and one button to adjust the right image into the inversion of the first image.

Design a window Form as shown above, and perform the following actions:

Design a form with two Picturebox tools and one button and load an image into the left picture box. Using the suitable values to fill the ColorMatrix. . Creates and sets the attributes of the adjusted image within the memory. Draws the adjusted image to the right picture box. ColorMatrix colorMatrix = new ColorMatrix(newfloat[ ][ ] { newfloat[] {-1, -1, -1, 0, 0}, newfloat[] { -1, -1, -1, 0, 0}, newfloat[] { -1, -1, -1, 0, 0}, newfloat[] {0, 0, 0, 1, 0}, newfloat[] {1, 1, 1, 1, 1} };

The color matrix is applied to every pixel in the image as it is drawn into the graphics buffer. The following figure shows how a single source pixel is converted to a single destination pixel.
Dr. Mohammed Fadhl Computer Graphic Lab Page 1

Source

* Rr Rg

Matrix Gr Gg Gb Ga Go Br Bg Bb Ba Bo

= Ar Ag Ab Aa Ao 0 0

Destination

Rs

Gs

Bs

As

* R b

0 = Rd 0 0

Gd

Bd

Ad

Ra Ro

Rd = (Rr * Rs) + (Rg + Gs) + (Rb * Bs) + (Ra * As) + Ro Gd = (Gr * Rs) + (Gg + Gs) + (Gb * Bs) + (Ga * As) + Go Bd = (Br * Rs) + (Bg + Gs) + (Bb * Bs) + (Ba * As) + Bo Ad = (Ar * Rs) + (Ag + Gs) + (Ab * Bs) + (Aa * As) + Ao Complete the code: // 1# DO Create image. private void button1_Click(object sender, EventArgs e) { // 2# DO Draw the image in picturebox1 // 3# DO suitable values to fill the ColorMatrix image. float[][] dataArray ; // . . . . . . // 4# DONE

set the ColorMatrix


ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(dataArray));

// 5# DONE Take space in memory to set the attribute of adjusted image Graphics g = Graphics.FromImage(image1); Rectangle rect = new Rectangle(0, 0, image1.Width, image1.Height); g.DrawImage(image1, rect, 0, 0, image1.Width, image1.Height, GraphicsUnit.Pixel, imageAttributes); this.Invalidate(); } } private void Form1_Paint(object sender, PaintEventArgs e) { // 6# Draws the adjusted image to the right picturebox. } Assignment: Do the negative image with setpixel/getpixel. Save the inverted image. Invert only the second half of the image (or the right half of it).
Dr. Mohammed Fadhl Computer Graphic Lab Page 2

Anda mungkin juga menyukai