Recreating the Photoshop filters with JAVA & Spatial domain processing — Signal Processing 1

Mannar Amuthan
6 min readSep 15, 2019

--

Signal processing is a really old domain, in the human era. When we play with mirrors, and lights the domain evolves with ourselves and mathematical algorithms as so much. Now we have the unimaginable amount processing power for computation. So it is the time for simulating our ancestor’s methods and algorithms in real-time. In this post, we shall learn some basic processing of images in the spatial domain. So to check the reliability of our methods and also for proof of our ability, we will check our results with the Adobe photoshop’s results.

Understanding Image:

Image is an array, it is the conventional definition of the digital image. Generally, it is a 2-dimensional array where each element represents each pixel and position. The value represents the magnitude of the amplitude of the pixel. 0 -white, 255- Black. (in 8-bit convention). And in the 3 channel image, 3 arrays are naturally presented one for red intensity and another two for blue and green.

Normal color filters:

In java image pixel’s can be edited by converting the image file to BufferedImage object. The code will like below,

File in=new File(“D:\\blog\\blog\\inceptionDemo.jpg”);
BufferedImage raw=ImageIO.read(in);

BufferedImage image=new BufferedImage(raw.getWidth(),raw.getHeight(),BufferedImage.TYPE_INT_ARGB);
int width=raw.getWidth();
int height=raw.getHeight();
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
Color color=new Color(raw.getRGB(i, j));
int red=color.getRed();
int blue=color.getBlue();
int green=color.getGreen();
int alpha=color.getAlpha();
Color newColor=new Color(red,green,blue,alpha);
image.setRGB(i, j, newColor.getRGB());
}
}

ImageIO.write(image, “png”, “D:\\blog\\blog\\result.jpg”);
}

(Input Image — Image courtesy Inception)

The above code will produce the same input image because we didn’t change any channel’s intensity on that. For example, if I change red=0 in the code. Then red channel’s value will be less for that pixel. So it will be more blue or green based on the other channel’s weight. If I want to convert the image to grayscale, then the output image only needs the one channel. So we apply the same color for all three channels. So what color it will be?. If we average all channels then it will be our gray,(typically you can imagine)

(Grayscale)

Algorithm for graysacle image :

int gray=(red+green+blue)/3;
Color newColor=new Color(gray,gray,gray,alpha);

alpha represents the opacity of the image so don’t need to worry about it.

Inverting the image:

We all heard about the negative image, it was also is known as image inversion. The basic idea behind the image is mapping negatively the pixel values. 255 for 0, 254 for 1 …………..0 for 255. That’s all.
We can get a negative image.

(Inverted image- photoshop)
Our result- By below method

red=(255-red);
if(red<0)red=-1*red;

green=(255-green);
if(green<0)green= -1*green;

blue=(255-blue);
if(blue<0)blue= -1*blue;

Other color filters:

There are a huge set of filters available in Adobe photoshop. But for understanding the concepts we can only demonstrate some filters. You can demonstrate others on your own. There are slightly some variations between our output and photoshop’s output. Because we didn’t know the actual color amplification and attenuation graph.

(deep red — photoshop)
(deep red filter — our result)

Deep red: = reducing blue and green’s intensity to 37% .
green=(int)((37.0/100)*green);
blue=(int)((37.0/100)*blue);

(deep blue — photoshop)
(deep blue — our result)

Deep blue: = reducing red and green’s intensity to 13 and 27%
red=(int)((13.0/100)*red);
green=(int)((27.0/100)*green);

you can do it for any colors. If you want your image, very reddish, then you only need to reduce other’s colors intensity right?!.

Spatial domain filters:

In signal processing, we can represent any signal in 2 perspectives, 1. Time domain, 2. Frequency Domain. Audio, waves signals change their magnitude concerning time but the image changes its magnitude for pixels. So in digital image processing, we said it as “Spatial domain”. In spatial domain processing, we change the magnitude of our target pixel depending on the neighbor pixel. We use various kernels, to do filter in an input image. For example, let us see the simple blur operation. We use the average filter for doing box blur(simple blur). We simply average the pixel values to get the average value. So If we choose 3x3 kernel, we sum and divide by 9.

(Box filter — image averaging methods)

/// from opencv it will put 1/9 in every pixel in kernal matrix
int num=3;
Mat m=Mat.ones(num, num, CvType.CV_32F);
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
m.put(i,j,1.0/(num*num));
}
}

When kernel size increases, the image get more and more blur.

Left- photoshop 5 pixel averaging…Right- Our result

Gaussian Blur: The Gaussian blur is a type of image-blurring filter that uses a Gaussian function (which also expresses the normal distribution in statistics) for calculating the transformation to apply to each pixel in the image. Apart from mathematical derivations, let us see the operational kernel
The idea of Gaussian smoothing is to use this 2-D distribution as a `point-spread’ function, and this is achieved by convolution.

Gaussian filter

Here our result, and photoshopped blur


Motion Blur: If we need a dynamic look in image. We can just average the horizontal neighbors (motion in x direction), or we can just average the vertical neighbors of image.(motion in y direction).

top left- vertocal motion photoshop….top right our result…bottom left horizontal motion photoshop..bottom right our result

Median Blur: It is vastly used filter in every noise reduction image processing methods. Best in removing the salt and pepper noise. What we do is “We select the median value of the neighbor pixels, and replace that value with a targetted image.”

After median filter applied-left and image with noise right

We can sharpen image by laplacian kernal and for detecting edges, we can use Sobel kernel and laplacian kernel. These are apart from photoshop filters, we will see those edge detection algorithms, kernels and image segmentation in the next article.

--

--