This matlab code aims to percieve the depth of an image and blur the object that it detects as the background .
This program uses only single image as input unlike other smartphone algos and works on clustering and equation optimzation methods rather than Neural Networks.
this is an implementation of an idea of another person's research paper
https://ieeexplore.ieee.org/document/6909756 - contains the weighting method
https://ieeexplore.ieee.org/document/8103371 - contains a better method for blurring along with weighting
The input image is converted to LAB space and clustered with inbuilt matlab function for further processing.
Representation of the clustered picture:
w_bgr represents the probability of each superpixel belonging to the background , whiter the superpixel , more is the chance of it belonging to the background.
This uses a connected graph method to compute the probabilities. First an adjacency matrix was constructed and a weighted graph was created from this.

The constructed graph was then used in determining the w_bgr values . the computation involves finding shortest distance between two superpixels as well, which is again taken care of by an inbuilt matlab function.
Representation of w_bgr:

w_Ctr represents the probability of each superpixel belonging to the foreground , whiter the superpixel , more is the chance of it belonging to the foreground.
This is constructed from a combination of the produced background weights and a superpixel contrast map (in colorspace).
Representation of w_Ctr:

w_smooth is used in the optimzation equation to promote smoother gradient of superpixels, rather than abrupt changes.This is again dependant on colorspace distance between to superpixels.
Saliency basically means the importance of a pixel (lies b/w 0-1), closer it is to 1 , the higher is its chance of belonging to the foreground.
The optimized output is produced by the minimization of an eqn with w_bgr , w_Ctr & w_smooth as constants and saliency as the variable parameter.
I have implemented this optimization by deploying s as a set of random numbers b/w [0 1]. Then I computed the error (Gradient here) of the equation with respect to saliency of the ith superpixel.
this gradient was subtracted from the I'th superpixel's value after being multiplied with a learning rate 'alpha'.
The same process was repeated for all superpixels in one iteration.
500 such iterations resulted in a very optimized solution
Representation of optimized saliency :
we can see our saliency map is basically a depth map
the optimal way would be to blur the superpixels based on their saliency values i.e:
if saliency closer to 0 , blur the portion harder
if saliency closer to 1 , sharpen the portion more
if saliency is around 0.5 , the portion can be left as is
just run main.m with MATLAB.
It has parameters for no.of iterations, learning rate, no.of clusters and img_path
smaller parameters related to weight calculation can be modified in WeightGen.m


