Basic Image Manipulation
Raed S. Rasheed2012
Agenda• Region of Interest (ROI) • Basic geometric manipulation.
– Enlarge – shrink– Reflection
• Arithmetic and logical combination of images.– Addition and averaging.– Subtraction. – Division.– AND & OR.
• Transformation and Rotation
Region of Interest (ROI)
• A region of interest (ROI) is a rectangular area within the image, defined either by the coordinates of the pixels at its upper-left and lower-right corners or by the coordinates of its upper-left corner and its dimensions.
Region of Interest (ROI)
Basic Geometric Manipulation.
Enlarging or shrinking an image can be accomplished by replicating or skipping pixels. These techniques can be used to magnify small details in an image, or reduce a large image in size so that it fits on the screen. They have the advantage of being fast, but can only resize an image by an integer factor.
Basic Geometric Manipulation.
Enlarge:To enlarge an image by an integer factor n, we must replicate pixels such that each pixel in the input image becomes an n x n block of identical pixels in the output image. The most straightforward implementation of this involves iterating over the pixels in the larger output image and computing the coordinates of the input image pixel from which a value must be taken. For a pixel (x, y) in the output image, the corresponding pixel in the input image is at (x/n, y/n). Calculation of the coordinates is
done using integer arithmetic.
Basic Geometric Manipulation.
ALGORITHM Image_Enlarge 3.101 INPUT Image, n02 OUTPUT EnlargeImage03 BEGIN04 Create new image EnlargeImage; // with multiple size (n);05 For each row in EnlargeImage06 For each column in EnlargeImage07 SET EnlargeImage(column,row) = Image(column/n,row/n) 08 END For09 END For10 RETURN EnlargeImage;11 END
Basic Geometric Manipulation.
Basic Geometric Manipulation.
Shrink:To shrink an image by an integer factor n, we must sample every nth pixel in the horizontal and vertical dimensions and ignore the others. Again, this technique is most easily implemented by iterating over pixels in the output image and computing the
coordinates of the corresponding input image pixel.
Basic Geometric Manipulation.
ALGORITHM Image_Shrink 3.201 INPUT Image, n02 OUTPUT ShrinkImage03 BEGIN04 Create new image ShrinkImage; // with divided size (n)05 For each row in ShrinkImage06 For each column in ShrinkImage07 SET ShrinkImage(column,row) = Image(column*n, row*n); 08 END For09 END For10 RETURN ShrinkImage;11 END
Basic Geometric Manipulation.
Basic Geometric Manipulation.
Reflection:Reflection along either of the image axes can also be performed in place. This simply involves reversing the ordering of pixels in the rows or
columns of the image.
Basic Geometric Manipulation.ALGORITHM Image_Vertical_ Refliction 3.301 INPUT Image02 OUTPUT ReflectionImage03 BEGIN04 Create new image ReflectionImage;05 SET W = Image Width;06 For each row in Image07 For each column in Image08 SET ReflectionImage(column,row) = Image(W – column – 1,row); 09 END For10 END For11 RETURN ReflectionImage;12 END
Basic Geometric Manipulation.
Arithmetic and logical combination of images.
Addition and averaging:I f we add two 8-bit greyscale images, then pixels in the resulting image can have values in the range0 - 510. We should therefore choose a 16-bit representation for the output image or divide every pixel's value by two. If we do the latter, then we are
computing an average of the two images.g(x,y) = αf1(x,y) + (1 - α)f2(x,y)
Arithmetic and logical combination of images.
α = 0%
α = 0%
α = 0%
α = 0%
α = 0%α = 0%
α = 0%
α = 100%
α = 50%
α = 100%
α = 0%
α = 50%
Arithmetic and logical combination of images.
ALGORITHM Image_Addition_Averaging 3.401 INPUT Image1, Image2, α02 OUTPUT AddintionImage03 BEGIN04 Create new image AddintionImage;06 For each row in Image1 and Image207 For each column in Image1 and Image208 SET AddintionImage(column,row) = α * Image1(column,row) + ( 1 - α ) * Image2(column,row); 09 END For10 END For11 RETURN AddintionImage;12 END
Arithmetic and logical combination of images.
Subtraction:Subtracting two 8-bit greyscale images can produce values between -255 and +255. This necessitates the use of 16-bit signed integers in the output image unless sign is unimportant, in which case we can simply take the modulus of the result and store it
using 8-bit integers :g(x,y) = |f1(x,y) - f2(x,y)|
Arithmetic and logical combination of images.
ALGORITHM Image_Subtraction 3.501 INPUT Image1, Image202 OUTPUT SubtractionImage03 BEGIN04 Create new image SubtractionImage;06 For each row in Image1 and Image207 For each column in Image1 and Image208 SET SubtractionImage(column,row) = | Image1(column,row) - Image2(column,row) |; 09 END For10 END For11 RETURN SubtractionImage;12 END
Arithmetic and logical combination of images.
Subtraction:
Arithmetic and logical combination of images.
Division:For division of images to produce meaningful results, floating-point arithmetic must be used. The ratio image can be of the floating-point type, or we can rescale and round pixel values to be in a
more convenient 0-255 range.
Arithmetic and logical combination of images.
ALGORITHM Image_Division 3.501 INPUT Image1, Image202 OUTPUT DivisionImage03 BEGIN04 Create new image DivisionImage;06 For each row in Image1 and Image207 For each column in Image1 and Image208 SET DivisionImage(column,row) = Rescale(Image1(column,row) / Image2(column,row)); 09 END For10 END For11 RETURN DivisionImage;12 END
Arithmetic and logical combination of images.
Division:
Arithmetic and logical combination of images.
AND & OR:Logical AND and OR operations are useful for the masking and compositing of images. For example, if we compute the AND of a binary image with some other image, then pixels for which the corresponding value in the binary image is 1 will be preserved, but pixels for which the corresponding binary value is 0 will be set to 0 themselves. Thus the binary image acts as a 'mask' that removes
information from certain parts of the image.
Arithmetic and logical combination of images.
ALGORITHM Image_AND 3.601 INPUT Image1, Image202 OUTPUT ANDImage03 BEGIN04 Create new image ANDImage;06 For each row in Image1 and Image207 For each column in Image1 and Image208 SET ANDImage(column,row) = Image1(column,row) AND Image2(column,row); 09 END For10 END For11 RETURN ANDImage;12 END
Arithmetic and logical combination of images.
ALGORITHM Image_OR 3.701 INPUT Image1, Image202 OUTPUT ORImage03 BEGIN04 Create new image ORImage;06 For each row in Image1 and Image207 For each column in Image1 and Image208 SET ORImage(column,row) = Image1(column,row) OR Image2(column,row); 09 END For10 END For11 RETURN ORImage;12 END
Arithmetic and logical combination of images.
AND & OR:
Arithmetic and logical combination of images.
AND & OR:
Transformation and Rotation
Transformation and RotationALGORITHM Image_Transformation 3.801 INPUT Image, Tx ,Ty
02 OUTPUT TransformationImage03 BEGIN04 Create new image TransformationImage;06 For each row in Image07 For each column in Image08 SET NewCol = column + Tx;09 SET NewRow = row + Ty;10 SET TransformationImage(NewCol,NewRow) = Image(column,row); 11 END For12 END For13 RETURN TransformationImage;14 END
Transformation and Rotation
Transformation and Rotation
1. The pixel at (0, 100) after a 90° rotation 2. The pixel at (50, 0) after a 35° rotation
x' = x cos θ - y sin θ = 50 cos(35o ) = 40.96, y' = x sin θ + y cos θ = 50 sin(35o ) = 28.68.
In case 1, cos 90' is 0 and sin 90' is 1, so the pixel moves to coordinates (-100,0). This is clearly a problem, since pixels cannot have negative coordinates. Case 2 illustrates a different problem.
Transformation and Rotation
The first problem can be solved by testing coordinates to check that they lie within the bounds of the output image before attempting to copy pixel values. A simple solution to the second problem is to find the nearest integers to x' and y' and use these as the coordinates of the transformed pixel.
Transformation and RotationALGORITHM Image_Rotation 3.901 INPUT Image, θ02 OUTPUT RotationImage03 BEGIN04 Create new image RotationImage;05 SET a0= Cos(θ), b0= Sin(θ), a1= - b0, b1= a0; 06 For each row in Image07 For each column in Image08 SET NewCol = Round(a0 * column + a1 * row);09 SET NewRow = Round(b0 * column + b1 * row);10 IF NewCol AND NewRow inside RotationImage THEN11 SET RotationImage(NewCol,NewRow) = Image(column,row);12 END IF 13 END For14 END For15 RETURN RotationImage;16 END
Transformation and Rotation