Why Java??

Overview of the tutorial:
This tutorial is not to teach you to do things in Java particularly. It's just a tool that I'm using. I always prefer C to learn how actually computers work. But, there are really many references for programming image processing algorithms in C or Python.
I'm going to focus on algorithmic ideas mainly.
Importing Packages:
These are some basic in-built packages required to be imported:
1.java.awt.*;
2.java.awt.image.*;
3.java.io.*;
4.javax.imageio.*;
Making life a little easier!
IDE s are a really important part of a programmer's life. For Java, I always prefer NetBeans.
And if you are using NetBeans too, then I must say that to directly access an image by its name only,
keep the image in the Project level, that is, one level above the src folder.
keep the image in the Project level, that is, one level above the src folder.
Loading an Image:
Code:
BufferedImage OrigImg,EditImg;
BufferedImage OrigImg,EditImg;
public void loadImage(String fileName){
try{
OrigImg=ImageIO.read(new File(fileName));
}catch(IOException e){System.out.println("could not load the image");}
EditImg=OrigImg;
}
Explanation:
As you can see, Java stores images as a class named BufferedImage. Here, "fileName" is the name of the image(with its extension), that is to be loaded. As soon as we read the original file, we copy it, so that we can use the original image data even after manipulating the original buffered image, "EditImg".
Saving an Image:
Code:
public void saveImage(BufferedImage img,String ref){
processImage(img);
try{
String format=(ref.endsWith("png"))?"png":"jpg";
ImageIO.write(img, format,new File(ref));
}catch(IOException e){e.printStackTrace();}
}
Explanation:
First of all, you can now ignore the "processImage(img);" statement for now. Here, "ref " is the name of the image file in which you want it to be saved. The code suggests that ref must include the extension of the file too(for example, "image1.jpg").
Getting Image Parameters:
Code:
img.getWidth();
img.getHeight();
Remember: The parameters are returned as pixels.
Getting the Pixel Array:
If you are already familiar with Image Processing, you know that we need a pixel array of an image to process it. Normally, its a 2D array provided by most APIs(Exception: Some python API, as in Blender3D, pixel array is 1D).
Code:
int[][] result=new int[width][height];
for(int row=0;row<height;row++){
for(int col=0;col<width;col++){
result[col][row]=img.getRGB(col, row);
}
}
Here, "result" is the pixel array. "img.getRGB(col, row)" extracts the RGB values of the pixel defined by the address [col][row].
Remember: First col, then row!
for(int row=0;row<height;row++){
for(int col=0;col<width;col++){
result[col][row]=img.getRGB(col, row);
}
}
Here, "result" is the pixel array. "img.getRGB(col, row)" extracts the RGB values of the pixel defined by the address [col][row].
Remember: First col, then row!
Extracting red,blue,green values from RGB value:
Code:
result[col][row]=img.getRGB(col, row);
Color c=new Color(result[col][row]);
int red=c.getRed();
int blue=c.getBlue();
int green=c.getGreen();
The Color class can be initialized with either an RGB value or a set of three parameters(ie, red, blue, green). Here, since we had the RGB value, so we did the first case.
Setting the new value of a pixel:
Now, after manipulation of the red, blue, green values, we need to set them to the each respective pixels of the image to be saved. The code is as follows:
c=new Color(red,green,blue);
img.setRGB(col,row,c.getRGB());