M and Q>N, then we call this magnification.
Example:
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 pixels to get a value for each
pixel in the new smaller image. 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 corresponding pixel in the new smaller
image. /*SHRINKING BY FACTOR of 2*/
/*Visit each pixel of the new smaller*/
/* image and calculate its value*/
S= 2; /*the scale factor*/
for(r=0; r<M/S; r++)
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;
}
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
Consider the following
- What if the scale is not 2?
- What if the scale factor is not the same in the row and column directions?
What will happen to the image?
- What would be the algorithm if you were magnifying the image?
Rotation
Suppose that you
wanted to rotate and image. What does it mean to say you want to rotate an image
by 45 degrees? You must apply the laws of trigonometry to rotate an image around
its center point by the angle desired. The following equations govern the
relationship between the old position (r,c) and the new position (r',c'): r' = r*cos(angle) + c*sin(angle)
c' = c*cos(angle) - r*sin(angle)
Consider the following:
- What will you do if the r',c' calculated are not integer values?
- How would you implement this algorithm in a discrete MxN image?
- What happens to the corners of an image when you rotate it 45 degrees?
Translation
When performing
translation, usually you are moving parts of images to different locations
within the same image. To specify a translation, you specify the portion of the
image and the destination in row, column values of this portion. This
destination is usually signified as the new location of the upper left corner
the rectangular image portion specified. Given this is the case, the following
algorithm will implement the translation operation. Note that really a copying
of the portion is what is achieved. You may choose to set the original portion
of the image not overlapping with the coppied version to black as is done by
programs such as Photoshop. Rd = Row value of destination;
Cd = Column value of destination;
Rs,Cs = Upper left hand corner of image portion want to translate;
Re,Ce = Lower right hand corner of image portion.
for(r=Rs; r<Re; r++)
for(c=CS; c<Ce; c++)
{
Pnew[Rd + r, Cd + c] = P[r,c];
}
Consider the following:
- What would you do if the designated destination region was either partly
or wholey outside of the image boundaries?
Mirroring
The process of
mirroring is that of "reflecting" the image values around a horizontal or
vertical line going through the center of the image. Below is the algorithm for
doing a vertical mirroring. /*Vertical Mirroring*/
for(r=0; r<M; r++)
for(c=0; c<N; c++)
{
Pnew[r,c] = P[r, N-c];
}
Consider the following:
- How would you write a program to mirror the image around an arbitrary line
specified by two endpoints?