How Instagram filters work

I am going to assume that you are familiar with Instagram. If not, I both envy and pity you; but here's the gist of Instagram: it's a photo sharing service that has a selling point of allowing users to apply filters to their images. The filters would change the color of the images, often to enhance the subject.

How do those filters work? Convolutions!

For example, let's define a filter:

To convolve, we simply slide the filter across the following diagram (it's a very famous artwork by an artist called Piet Chew):

Applying the preceding filter would yield something such as the following:

Yes, the filter blurs images!

Here's an example written in Go to emphasize the idea:

func main() {
kb := []float64{
1 / 16.0, 1 / 8.0, 1 / 16.0,
1 / 8.0, 1 / 4.0, 1 / 8.0,
1 / 16.0, 1 / 8.0, 1 / 16.0,
}
k := tensor.New(tensor.WithShape(3,3), tensor.WithBacking(kb))

for _, row := range imgIt {
for j, px := range row {
var acc float64

for _, krow := range kIt {
for _, kpx := range krow {
acc += px * kpx
}
}
row[j] = acc
}
}
}

The function is quite slow and inefficient, of course. Gorgonia itself comes with a much more sophisticated algorithm

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.228.88