Building PS-like Filters (and more) with Python

This post documented how we recreate Photoshop-like filter from scratch with Python. Including fundamental concepts from image processing: Histogram Equalisation, Laplcian Pyramid Blending, Possion Blending etc. (As the work is still under evaluation in university, code has not been published yet~)

Histogram Equalisation with numpy.bincount()



Histogram equalisation helps enhance the image to be more vivid. It improves the global contrast of the images by allowing area with low local contrast to gain a higher contrast. Without using .equalize_hist() functions, it can be implemented with:

  • Step 1: Compute an image histogram with numpy.bincount()
  • Step 2: Compute a normalized cumulative histogram
  • Step 3: Use the cumulative histogram to map pixels to the new values as a lookup table

  • lang: python
    1
    #tbc calculating histogram

    Comparing the resultant images above, it is noticeable that different processing techniques have their own features. Firstly, “Processing each channel separately” will in general correct the overall colour of the images. Secondly, “Process value channel in HSV colour space” will results in a slightly darker foreground compare to “Grey-scaled + colour ratio”. The front building and the wall with the graffiti is bright for the “Grey-scaled + colour ratio” image than that of “Process value channel in HSV colour space” image.





    Alpha Blending



    The objective is to combine two images into one so that the content is “stitched” together in a possibly seamless manner. The alpha mask (for the left-hand side image) is:

    which essential states that within the window, we would like the value of the mask gradually decrease from 1 to 0. For the right-hand side image, the mask would just be (1 – α1).

    lang: python
    1
    #tbc alpha mask

    Pyramid Blending



    Although alpha blending is simple, designing an ideal alpha blending mask for any kind of image is difficult. The transition region in the blending mask should be relatively large when blending smooth regions to avoid visible seams. But when blending small structures, you want to use a small transition region to avoid ghosting. For a robust method that can handle both smooth regions and a high-frequency structure, Laplacian pyramid is used. Both images are decomposed into Laplacian pyramids and the levels from both pyramids are blended one-by-one, using a different alpha mask. As lower frequencies have a larger spatial extend, the transition for those frequencies should be smoother. Higher frequencies have a smaller spatial extend and they need a sharper transition. Above shows how a cat images is being decomposed into different levels of Laplacian pyramids. And below are the code for decomposing images into different levels, following with the function that expands them.

    lang: python
    1
    2
    #tbc gausspyr_reduce 
    #tbc gausspyr_expand

    The difference between alpha blended image and pyramid blending images may not be apparent at first glance, but we can see that alpha blending has a gradual transition at all time across the boundary. While for pyramid blending, the background has a more gradual transition, but the objects part has a sharper transition. And this allow us to retain the details of the original object as much as possible, while having a smooth blending for the background. The window size was intentionally set to quite narrow to produce a more distinguish blending results.







    Gradient Domain Image Enhancement

    The general motivation behind the gradient-based methods is that the human visual system is more sensitive to pixel differences (gradients) than absolute pixel values. Therefore, operations performed on image gradients should focus on visually important information and potentially produce more plausible results. Thus, we would like to perform image enhancement in the gradient domain and reconstruct the image from the modified gradient field.

  • Step 1: Extract gradient field from input images.
  • lang: python
    1
    #tbc 
  • Step 2: Modifying the gradient field


  • The gradient field is modified based on the function shown above

    lang: python
    1
    #tbc 
  • Step 3: Reconstruct image from gradient field
  • lang: python
    1
    #tbc 




    Gradient domain Copy & Paste - Possion Blending


    The gradient domain copy & paste operations (seamless cloning) from the paper by Pérez et al "Poisson Image Editing" is being implemented. A similar method is also used for the healing brush tool in Adobe Photoshop. The basic idea behind this method is to copy the portion of the gradient field from the source image to the target image and then solve for the pasted pixels. However, to ensure that the pasted region seamlessly merges into the target, the values of the pixels that belong to the edge of the pasted region need to be (soft-)constrained so that their values are close to the pixel values in the target image.
    lang: python
    1
    #tbc 

    Key Skills developed: