Image Processing Basics 2 - Image Scaling
Scaling algorithms are used to scale the image i.e. to change the size of image as per our requirement. Since it involves re-sizing the image normally there is a loss of smoothness and sharpness.
Some of the Scaling Methods are :-
Nearest – Neighbour Interpolation :-
In this technique to enlarge the pixel, value of nearest pixel is copied i.e the color of pixel in new image is the color of the nearest pixel of the original image. If you enlarge the pixel by 400% then each pixel will be enlarged 4x4 therby forming 16 pixels with same color.
Advantage of this technique is that it will not introduce any bias in the image as the image itself is scaled without loss of data
Disadvantage is that it will introduce aliasing and jaggies.
Aliasing :- It refers to an effect which causes the pixel to be aliases i.e. it will be copy of the other. Problem arises because it introduces jaggies which means stairlike lines that appear where there should be smooth straight lines or curves. For example, when a nominally straight, un-aliased line steps across one pixel, a dogleg occurs halfway through the line, where it crosses the threshold from one pixel to the other.
Example Image : -
As you can see in above image , individual pixel is a square type and you can notice edges of squares or rectangles which should appear smooth .
Bilinear Interpolation :-
In Bilinear Interpolation, linear interpolation is used to estimate the color of the pixel. Linear interpolation deals with estimation of an arbitrary points with respect to two other points.
Example :- Consider two points P & Q.
Consider an arbitrary point S. Now how to determine the value of S if value of P is 255 and value of q is 0 given the length between P & Q is 100 and distance between S and P is 25.
S-P/d = Q-S/(D-d)
=> S-255/25=0-S/75
=> S-255=-(S/3)
=> 4S=255x3
=> S=191.25
Bicubic Interpolation :-
Images re-sampled with bicubic interpolation are smoother and have fewer interpolation artifacts. It is computational intensive and includes algorithms such as Lagrange Polynomials, cubic spines, cubic convolution algorithms etc.We will re-visit these complex topics after we finish basics.
Supersampling :-
Aliasing occurs because real-world objects have continuous, smooth curves and lines, whereas monitors can only display discrete points of light called pixels. Since pixels are uniformly colored and always of the same shape, lines become jagged.Supersampling is one of the ways of solving this problem. Samples are taken at several instances inside the pixel (not just at the center as default) and an average color value is calculated. This is achieved by rendering the image at a much higher resolution than the one being displayed, then downsampling it to the desired size, using the extra pixels for calculation. The result is smoother transitions from one line of pixels to another along the edges of objects.
Now coming to programming perspective, lets take a small example where i will demonstrate how to scale a image.
Only changes it involves compared to my previous programs is getScaledInstance.
getScaledInstance :- Creates a scaled version of this image. A new
Image
object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used. public Image getScaledInstance(int width,int height,int hints)
Hints has several inbuilt values. You can each of them.
1) SCALE_AREA_AVERAGING :- Use the Area Averaging image scaling algorithm. The image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the image infrastructure supplied by the toolkit.
2) SCALE_DEFALUT :- Use the default image-scaling algorithm
3) SCALE_FAST :- Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.
4) SCALE_REPLICATE :- Use the image scaling algorithm embodied in the
ReplicateScaleFilter
class. The Image
object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the imaging infrastructure supplied by the toolkit. 5) SCALE_SMOOTH :- Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.
Program :-
LoadImage Interface :-
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public interface LoadImage {
public void loadImage();
}
Actual Class :-
import java.awt.*;
import java.awt.event.*;
class ImageProcessScale extends Frame implements LoadImage{
Image image;
public ImageProcessScale (){
loadImage();
}
public void paint(Graphics g){
g.translate( this.getInsets().left,this.getInsets().top);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 1);
try{
tracker.waitForAll();
}catch(InterruptedException e)
{
System.out.println(e);
}
g.drawImage(image,10,150,this);
}
@Override
public void loadImage() {
image = Toolkit.getDefaultToolkit().getImage("c:\\Sunset.jpg");
image=image.getScaledInstance(432, 432, Image.SCALE_AREA_AVERAGING);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public static void main(String[] args){
ImageProcessScale imageprocessscale = new ImageProcessScale();
}
Comments
Post a Comment