# Linear Algebra Application of Matrices in Image Processing Universidad Nacional Autonoma de Mexico Faculty of Engineering Basic Sciences Division

Prof. Norman Guevara Erick Corona Hector Zarate Rea Héctor Zárate - Final Work 1 Linear Algebra Application of Matrices in Image Processing Index: Basics: Introduction: 6. Multiplication 5. Adding Multiplication: 4. Operations withby a scale: Definition:Matrices:Matrices: Matrix April 3 Images and Matrices: 11.Invert colors 10. Representation of 9. Grayscale: Channels: 8.BrightnessAdjustment:Digital 7. Setting theAdjustment: 6.Filters implementedathrough Image operations representing an image: 6. RGB: Digital Imaging: (Negative) matrix through a matrix: Contrast 6 Bibliography: Conclusions: About the Software: 13 14 15 Héctor Zárate - Final Work 2 Linear Algebra Application of Matrices in Image Processing Introduction: This project aims to briefly explain how it is possible the handling or processi ng of images through the computer and she uses some of the concepts learned in t he course of Linear Algebra, particularly the matrix operations. Héctor Zárate - Final Work 3 Linear Algebra Application of Matrices in Image Processing Basics: What is a parent? "We can say that a matrix is a table or rectangular array of e lements that are usually real or complex numbers. The matrix concept however can

be generalized to the case where the elements are polynomials, functions, opera tors or any other mathematical entities. "1 Definition: An nxm matrix with elements in C is an array of the form:

a11 a21. . . am1 a12 a22. . . am2 · · · · · · .. . · · · a2n A1n. . . amn

Where a11, a12, amn belong to C m, n belong to Z. In abbreviated form, the matri x of the above definition can be expressed as [aij] where i = 1, 2, 3, ..., myj = 1, 2, 3, ..., n Operations with Matrices: Adding Matrices: The first of matrix operations is the addition, this could be where the parents are of the same order, and the result is obtained su1 SPEZIAL de Guzman, Leda, et al. Linear Algebra Notes. 4 Héctor Zárate - Final Work Linear Algebra Application of Matrices in Image Processing control the corresponding elements of both arrays in accordance with the followi ng definition: Let A = [aij] and B = [bij] two nxm matrices with elements in C. The sum A + B i s a matrix S = [Sikhs] of mxn defined by: sij = aij + bij For i = 1, 2, 3, ..., m and j = 1, 2, 3, ..., n. Multiplication by a scale: This operation is formally defined as: Let A = [aij] an nxm matrix with elements in C and ß belonging to C. The product ßA is a matrix E = [eij] of mxn defined by: eij = βaij For i = 1, 2, 3, ..., m and j = 1, 2, 3, ..., n. Héctor Zárate - Final Work 5

Linear Alge ra Application of Matrices in Image Processing Matrix Multiplication: Formally we have the following definition for matrix multiplication: Let A = [aij] and B = [ ij] two matrices respectively nxq mxny of elements in C. The product AB is a matrix P = [PIJ] m x q. Defined y: n k = 1 BKJ aik For i = 1, 2, 3, ..., m and j = 1, 2, 3, ..., q. Images and Matrices: Digital Imaging: A digital image is a cell composed of some elements called pixels, which are the smallest components of a digital image. Each pixel is a space in the computer m emory that stores a num er and this num er represents the definition of the colo r and rightness of a portion of the image. Each pixel can only define a color a nd the num er of pixels defines the amount of information that contains an image . RGB: Any color can e represented y com ining red, green and lue, each in different proportions. The com ination indicates standard RGB 256 levels per channel, ie for each red, green or lue. To represent the value of 256, we require 8 its fo r each color channel. This allows us to order a com ination of 2563 which allows us a com ination of almost 17 million colors. (16,777,216). Other representatio ns such as CMYK, ut not the approach in this work. Since only work with RGB. Héctor Zárate - Final Work 6 Linear Alge ra Application of Matrices in Image Processing Example: This image is 3 pixels wide y 3 tall. (It has een enlarged for demonstration p urposes).The matrix for this image is a matrix of order 3 such:

(151, 198, 255), (167, 202, 250), (178, 207, 249) (176, 220, 255), (190, 223, 254), (197, 220, 253) ( 209, 224, 245), (216, 229, 247), (217, 228, 246) Representation of a Digital Image through a matrix: You know how we can render an image (RGB) form a matrix of dimensions mxn, with elements of vectors where each vector will e composed or made up of three compo nents (RGB channels), with values contained in the integers from 0 to 255 in a c losed interval . Filters implemented through matrix operations representing an image: In image processing there are some asic filters that work y frequent use matri x operations, these filters are used primarily in digital photography, we have p lanned in this project are:

Setting the Channels: Is to manipulate the intensity of only one channel at a time, either red, green, lue or whatever. Héctor Zárate - Final Work 7 Linear Alge ra Application of Matrices in Image Processing As an example we define the green channel setting: M + A = C (This equation is to adjust the green channel) *

m11 m21. . . mm1 m12 m22. . . mm2 · · · · · · .. . · · · M2N m1n. . . mmn + (0, p, 0) (0, p, 0) · · ° (0, p, 0) (0, p, 0) · · ·. . .. . . . . . (0, p, 0) (0 , p, 0) · · · (0, p, 0) (0, p, 0). . . (0, p, 0)

+

= C

Where mij corresponds to a vector (rij, gij, ij) for the image. For i = 1, 2, 3 , ..., myj = 1, 2, 3, ..., n C correspond to the adjusted image of M. * Note: To adjust the red channel matrix each element of the adjustment will (p, 0.0) and the lue channel will e of elements: (0,0, p).

Brightness Adjustment: The rightness setting is one of the most asic settings within the image adjust ment and one of the most widely used. Brightness is the percentage of luminescen ce or darkness of a color. You can go from 0% which means lack, up to 100% whic h means white. Héctor Zárate - Final Work 8 Linear Alge ra Application of Matrices in Image Processing

e

The operation corresponding to the M + B = C

m11 m21. . . mm1 m12 m22. . . mm2 · · · · · · .. . · · · M2N m1n. . . mmn + (P, p, p) (p, p, p) · · ° (p, p, p) (p, p, p) · · ·. . .. . . . . . (P, p, p) (p , p, p) · · · (P, p, p) (p, p, p). . . (P, p, p)

Where mij corresponds to a vector (rij, gij, = C

For i = 1, 2, 3, ..., myj = 1, 2, 3, ..., n where M corresponds to the image mat rix and C correspond to the adjusted image of M. And p is the parameter of righ tness adjustment, which is standard from -100 to 100. Invert colors (Negative) This operation is to invest each RGB channel to its negative (Yes, it sounds red undant). For example, a film which is reflected in the colors reversed real imag e. This is the white ecomes lack, yellow, lue, green to red to magenta and cy an. The usefulness of this filter is in the digitization of photographic film. Héctor Zárate - Final Work 9 Linear Alge ra Application of Matrices in Image Processing The operation that corresponds to this setting is: N-M = C

(255, 255, 255) (255, 255, 255) · · · (255, 255, 255) (255, 255, 255) · · ·. . . . . . . . . (255, 255, 255) (255, 255, 255) · · · (255, 255, 255) (255, 255, 255). . . (255, 255, 255) m11 m21. . . mm1

ij) for the image.

m12 m22. . . mm2 · · · · · · .. . · · · M2N m1n. . . mmn

= C Where mij corresponds to a vector (rij, gij, ij) for the image. For i = 1, 2, 3 , ..., myj = 1, 2, 3, ..., n C correspond to the negative image of M. Grayscale: Conversions etween color images and grayscale images are not entirely straightf orward. The gray scaling consists in multiplying each component y three constan ts defined: Alpha, Beta and Gamma. Then we average the intensity o tained in eac h channel. This process su tracts all color information contained in each pixel and gives us a separation of 255 levels etween white and lack. These three con stants are o tained as the channel separation and lack RGB: Alpha: Separation etween red and lack. (0299) Beta: Separation etween green and lack. (0599) 10 Héctor Zárate - Final Work Linear Alge ra Application of Matrices in Image Processing Gamma: The separation etween lue and lack. (0111)

The corresponding operation is as follows: G = Where:

(GM1, GM1, GM1) (G11, g11, g11) (G21, G21, G21). . . (Gm2, gm2, gm2) · · · (G12, g12, g12) (G22, G22, G22). . . · · · · · · .. . (Gmn, gmn, gmn) (G1n, g1n, g1n) (g2n, g2n, g2n). . .

gij = 1 (αrij, βgij, γbij) 3 For i = 1, 2, 3, ..., m and j = 1, 2, 3, ..., n. Where the values of alpha, beta and amma are the followin constants: Alfa: 0299 Beta: 0599 Gamma: 0.11 Contrast Adjustment: The contrast, refers to the difference between li ht and dark areas of the ima e . 11 Héctor Zárate - Final Work

¡

¡

¡

¡

Linear Al ebra Application of Matrices in Ima e Processin

This adjustment is more complicated processin m11 m21 1 C = (255 . .. mm1

m12 m22. . . mm2 · · · · · · .. . · · · M2N m1n. . . mmn

a11 a21 a12 a22. . . am2

-

.

. . am1

· · · · · · .. . · · · a2n A1n. . . amn

A21 a11 a12 a22. . . am2

) kc +

.

. . am1

· · · · · · .. . · · · a2n A1n. . . amn

255 Where: aij = 1 (1, 1, 1) 2 mij corresponds to the matrix M, which is the ima e to be adjusted. For i = 1, 2 , 3, ..., m and j = 1, 2, 3, ..., n. And where kc = 100 + p 100 parameter p corresponds to the contrast settin selected by the user. Héctor Zárate - Final Work 12 Linear Al ebra Application of Matrices in Ima e Processin

¡

¡

¡

¡
of all the views above:

¡

¡ ¡

¡ ¡

About the Software: The software was developed in C # within Microsoft Visual Studio 2005 suite. Whi ch consists of more than 300 lines of malicious code, undetectable by antivirus. This code contains about 20 methods implemented. The workflow is as follows: 1. A pro ram is imported bitmap (BMP) oo a compressed ima e format (JPEG). 2. Th e pro ram converts the ima e to a matrix of order 500, whose components are a st ructure containin three fields of inte ers, one for each color channel. 3. From this point the user can choose between 7 different options for the imported ima e processin : • • • • • • • Adjust the Red Ch nnel Green Ch nnel Setting Ch nne l Setting Brightness Adjustment lue contr st to gr ysc le conversion Neg tive i m ge 5. The progr m processes the m trix representing the im ge ccording to the oper tions defined e rlier in the development of the project. 6. Fin lly the rr y i s ck to tr nsform itm p th t is displ yed to the user. 7. Et voil ! Héctor Zár te - Fin l Work 13

Applic tion of M trices in Im ge Processing Conclusions: The development of this project h s llowed us to demonstr te one of the m ny p plic tions of line r lge r nd implement sever l of the su jects le rned in cl ss. This motiv ted us to underst nd line r lge r h s re l nd t ngi le ppl ic tion in sever l re s such s the im ge processing which h s continuous use of digit l photogr phy nd control systems, mong other things, H ve fun! :) Héctor Zár te - Fin l Work 14

Applic tion of M trices in Im ge Processing Bi liogr phy: I. II. III. IV. V. VI. SPEZIAL de Guzm n, Led , et l. Line r Alge r Notes. Edi tori l Limus , Mexico, 1991. LACEY, Joel. The Complete Guide to Digit l Im ging, The Ilex Press Limited, United St tes, 2001. RUSS, John. The Im ge Processing H nd ook. CRC Press, USA, 2002. SZIDAROVSKY, Ferenc. Intoduction to m trix theory . World Scientific Pu lishing Comp ny UK, 2001 Hoenig, Al n. TeX Un ound: L TeX nd TeX Str tegies for Fonts, gr phics nd more. Oxford University Press, USA 19 98. ANG, Tom, Adv nced digit l photogr phy. Octopus Pu lishing Group Ltd, Engl n d, 2003 Héctor Zár te - Fin l Work 15

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

Line r Alge r

¢

¢

¢

¢

¢

¢

¢ ¢ ¢ ¢

¢

¢ ¢ ¢

¢

¢

¢ ¢

¢

¢ ¢

¢

¢

¢

¢ ¢

¢ ¢ ¢ ¢ ¢ ¢

¢ ¢

¢

¢

¢

Line r Alge r

¢

¢ ¢

¢

¢

¢

¢

¢

¡

¢

¢

¢

¢

¢

¢

¡

¢

¢

¢

¡

¢

¢

¡

¢

¢

¢

¢

¡

¢

¢

¢

¢

¢

¢

¡

¢

¢

¡

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¢

¡