Skip to main content

This module attempts to enhance contrast of a given image by equalizing its two dimensional histogram.

Project description


This module attempts to enhance contrast of a given image by equalizing its two dimensional histogram. An easy way to enhance quality of a given image is to just equalize its histogram, but despite using minimum resources and a very short process time, there are a lot of drawbacks to it. One of the ways to tackle drawbacks of histogram equalization method is to instead equalize the image's two dimensional histogram, as one dimensional histogram of a given image does not contain the image's contextual information. Tests on a multitude of images has shown, by taking contextual information of an image in addition to the image's histogram into account when attempting to enhance contrast, output images are significantly better in quality in compare to histogram equalizaion and a handful of other known methods.

You can access the article that came up with this method here.

Two Dimensional Histogram

Here is the source code for the im2dhist python library with a short description on how it's done.


Run the following to install:

pip install im2dhisteq


import numpy as np
import cv2
from im2dhisteq import im2dhisteq

def imresize(img, wr=500, hr=None): # This is just for imshow-ing images with titles
    [ h, w] = img.shape
    hr = (h*wr)//w if not hr else hr
    img_resized = cv2.resize(img, dsize=(wr, hr))
    return img_resized

def main():
    fullname = 'Plane.jpg'
    image = cv2.imread(fullname)
    # convert rgb image to gray
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # w_neighboring=6 is generally an adequate value, drived by a lot of experimenting.
    # w_neighboring=6 corresponds to a 13*13 square
    gray_image_2DHisteq = im2dhisteq(gray_image, w_neighboring=6)

    # This is just for imshow-ing images with titles
    gray_Image_resized = imresize(gray_image)
    gray_Image_2DHisteq_resized = imresize(gray_image_2DHisteq)

    cv2.imshow('Original Image', gray_Image_resized)
    cv2.imshow('2DHeq Image', gray_Image_2DHisteq_resized)

if __name__ == '__main__': main()


This is a sample image and its corresponding 2D-Histogram Equalized image. Plane.jpg Image
corresponding 2D-Histogram Equalized image

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

im2dhisteq-0.0.7.tar.gz (552.9 kB view hashes)

Uploaded Source

Built Distribution

im2dhisteq-0.0.7-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page