1 cs6825: simple geometric operations 2scaling this is what happens when you resize an image. if you...

5
1 CS6825: Simple Geometric CS6825: Simple Geometric Operations Operations

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS6825: Simple Geometric Operations 2Scaling This is what happens when you resize an image. If you transform the image from an MxN to a PxQ image where

11

CS6825: Simple Geometric CS6825: Simple Geometric OperationsOperations

Page 2: 1 CS6825: Simple Geometric Operations 2Scaling This is what happens when you resize an image. If you transform the image from an MxN to a PxQ image where

22

ScalingScaling This is what happens when you resize an image. If you transform the This is what happens when you resize an image. If you transform the

image from an MxN to a PxQ image where P>M and Q>N, then we call this image from an MxN to a PxQ image where P>M and Q>N, then we call this magnification.magnification.

Example:Example:Suppose we want to shrink our image from MxN to M/2xN/2. Suppose we want to shrink our image from MxN to M/2xN/2. The following algorithm will do this, notice that we simply average 2x2 blocks of The following algorithm will do this, notice that we simply average 2x2 blocks of pixels to get a value for each pixel in the new smaller image. pixels to get a value for each pixel in the new smaller image.

/*SHRINKING BY FACTOR of 2*/ /*SHRINKING BY FACTOR of 2*/ S= 2; /*the scale factor*/S= 2; /*the scale factor*/

/*Visit each pixel of the new smaller*/ /*Visit each pixel of the new smaller*/ /* image and calculate its value*/ /* image and calculate its value*/ for(r=0; r<M/S; r++)for(r=0; r<M/S; r++) for(c=0; c<N/S; c++) for(c=0; c<N/S; c++) { Pnew[r,c] = (P[r*S, c*S] + P[r*S+1, c*S] + P[r*S, c*S+1] + P[r*S+1, c*S+1])/4; } { Pnew[r,c] = (P[r*S, c*S] + P[r*S+1, c*S] + P[r*S, c*S+1] + P[r*S+1, c*S+1])/4; }

The above algorithm, averages or interpolates the values in a 2x2 block to get a The above algorithm, averages or interpolates the values in a 2x2 block to get a single value. This kind of interpolation is called linear interpolation single value. This kind of interpolation is called linear interpolation

What if the scale is not 2? What if the scale is not 2? What if the scale factor is not the same in the row and column directions? What will What if the scale factor is not the same in the row and column directions? What will

happen to the image? happen to the image? What would be the algorithm if you were magnifying the image? What would be the algorithm if you were magnifying the image? Instead, a faster, but, arguably not as good in quality, would be to simply choose Instead, a faster, but, arguably not as good in quality, would be to simply choose

one of the pixels in the 2x2 block of the original image as the pixel value for the one of the pixels in the 2x2 block of the original image as the pixel value for the corresponding pixel in the new smaller image. corresponding pixel in the new smaller image.

Page 3: 1 CS6825: Simple Geometric Operations 2Scaling This is what happens when you resize an image. If you transform the image from an MxN to a PxQ image where

33

RotationRotation Suppose that you wanted to rotate and image. Suppose that you wanted to rotate and image.

What does it mean to say you want to rotate an What does it mean to say you want to rotate an image by 45 degrees? You must apply the laws of image by 45 degrees? You must apply the laws of trigonometry to rotate an image around its trigonometry to rotate an image around its center point by the angle desired. The following center point by the angle desired. The following equations govern the relationship between the equations govern the relationship between the old position (r,c) and the new position (r',c'):old position (r,c) and the new position (r',c'):

r' = r*cos(angle) + c*sin(angle)r' = r*cos(angle) + c*sin(angle) c' = c*cos(angle) - r*sin(angle)c' = c*cos(angle) - r*sin(angle)

Consider the following:Consider the following:

What will you do if the r',c' calculated are not What will you do if the r',c' calculated are not integer values? integer values?

How would you implement this algorithm in a How would you implement this algorithm in a discrete MxN image? discrete MxN image?

What happens to the corners of an image when What happens to the corners of an image when you rotate it 45 degrees?you rotate it 45 degrees?

Page 4: 1 CS6825: Simple Geometric Operations 2Scaling This is what happens when you resize an image. If you transform the image from an MxN to a PxQ image where

44

TranslationTranslation When performing translation, usually you are moving parts of When performing translation, usually you are moving parts of

images to different locations within the same image. To specify a images to different locations within the same image. To specify a translation, you specify the portion of the image and the translation, you specify the portion of the image and the destination in row, column values of this portion. This destination destination in row, column values of this portion. This destination is usually signified as the new location of the upper left corner the is usually signified as the new location of the upper left corner the rectangular image portion specified. Given this is the case, the rectangular image portion specified. Given this is the case, the following algorithm will implement the translation operation. Note following algorithm will implement the translation operation. Note that really a copying of the portion is what is achieved. You may that really a copying of the portion is what is achieved. You may choose to set the original portion of the image not overlapping choose to set the original portion of the image not overlapping with the coppied version to black as is done by programs such as with the coppied version to black as is done by programs such as Photoshop.Photoshop.

Rd = Row value of destination; Rd = Row value of destination;

Cd = Column value of destination; Cd = Column value of destination; Rs,Cs = Upper left hand corner of image portion want to Rs,Cs = Upper left hand corner of image portion want to translate; translate; Re,Ce = Lower right hand corner of image portion. Re,Ce = Lower right hand corner of image portion.

for(r=Rs; r<Re; r++) for(r=Rs; r<Re; r++) for(c=CS; c<Ce; c++) for(c=CS; c<Ce; c++)

{ Pnew[Rd + r, Cd + c] = P[r,c]; } { Pnew[Rd + r, Cd + c] = P[r,c]; }

Consider the following:Consider the following:

What would you do if the designated destination region was What would you do if the designated destination region was either partly or wholey outside of the image boundaries?either partly or wholey outside of the image boundaries?

Page 5: 1 CS6825: Simple Geometric Operations 2Scaling This is what happens when you resize an image. If you transform the image from an MxN to a PxQ image where

55

MirroringMirroring The process of mirroring is that of "reflecting" the The process of mirroring is that of "reflecting" the

image values around a horizontal or vertical line going image values around a horizontal or vertical line going through the center of the image. Below is the algorithm through the center of the image. Below is the algorithm for doing a vertical mirroring. for doing a vertical mirroring.

/*Vertical Mirroring*//*Vertical Mirroring*/

for(r=0; r<M; r++)for(r=0; r<M; r++)

for(c=0; c<N; c++) for(c=0; c<N; c++)

{ Pnew[r,c] = P[r, N-c]; }{ Pnew[r,c] = P[r, N-c]; }

Consider the following:Consider the following:

How would you write a program to mirror the image How would you write a program to mirror the image around an arbitrary line specified by two endpoints? around an arbitrary line specified by two endpoints?