Python version of the image processing learning 4- histogram, histogram equalization and optimization

Image histogram:
histogram is a graphic expression of the pixel value distribution of the image. It counts the number of each color value that appears in the image.
Statistics histogram is used to one-dimensional data, it is generally converted into a grayscale image should YUV view taken or a histogram calculation display elements.
In the application, the histogram generally used to see how the image exposure appropriately adjusted; or can be relatively close to the color region of an image to enhance the contrast of the image content (histogram equalization) by stretching the color range
histogram implementation:
mode 1: Using tools provided to realize the function matplotlib

import cv2
import dlib
from matplotlib import pyplot as plt
import numpy as np

image = cv2.imread("lena.tiff");
gray1 = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.hist(gray1.ravel(), 256, [0, 256])
plt.show()

Embodiment 2: cv2 implemented by the function

image = cv2.imread("lena.tiff");
gray1 = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

hist = cv2.calcHist([gray1], [0], None, [256], [0, 256])
plt.figure()
plt.plot(hist)
plt.show()

Mode 3: custom implementation

import cv2
import dlib
from matplotlib import pyplot as plt
import numpy as np

def calHist(img):
    # hist = np.arange(256)
    # for i in hist:
    #     hist[i] = 0
    hist = np.zeros(256)

    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    for i in range(0,width):
        for j in range(0,height):
            r = img[i,j]
            hist[r] = hist[r]+1

    return hist;

image = cv2.imread("lena.tiff");
gray1 = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

histData = calHist(gray1);
plt.figure()
plt.plot(histData)
plt.show()

FIG Effect:
Here Insert Picture Description
back cumulative histogram involves renderings:
increment, a maximum of 1 ...
Here Insert Picture Description
quietly introduce the concept of histogram equalization:
histogram equalization action: such an image across a wider gray level gray scale range, thereby improving the image contrast;
another advantage is that the histogram equalization: no additional parameters, the entire process is automated;
histogram equalization is a disadvantage: after stretching some gray level may not be mapped to cause image perception the grainy;
histogram equalization process:
a request grayscale histogram, the result is an array of 256 elements.
B request frequency of each color value occurring in the image (frequency), and then normalized. (divided by the total number of pixels)
C. find the number of cumulative distribution function for each color value
Here Insert Picture Description
d. obtaining histogram equalization is calculated, the mapping table to obtain the color lut
Here Insert Picture Description
E. d obtained according to the original grayscale lut do color mapping, drawn renderings after the histogram equalization

equ = cv2.equalizeHist(img)
cv2.imshow('equalization', np.hstack((img, equ)))  # 并排显示
cv2.waitKey(0)

Annex algorithm code:

import cv2
import dlib
from matplotlib import pyplot as plt
import numpy as np
#note:参数必须为灰度图,未对其他格式图做兼容处理
def histogram_equalization(grayImage):
    hist = np.zeros(256)
    histLut = np.zeros(256)
    imgInfo = grayImage.shape
    height = imgInfo[0]
    width = imgInfo[1]
    for i in range(0, width):
        for j in range(0, height):
            r = grayImage[i, j]
            hist[r] = hist[r] + 1

    index = 0
    while hist[index] < 0:
        ++index;
    pixelCount = width * height
    scale = (256 - 1.) / (pixelCount - hist[index]);
    sum = 0
    for i in range(index, hist.size):
        sum = sum + hist[i]
        histLut[i] = (sum * scale + 0.5)

    dst = np.zeros((height, width, 3), np.uint8)
    for u in range(0, width):
        for v in range(0, height):
            c = histLut[grayImage[u, v]]
            dst[u, v] = [c, c, c]
    return dst

image = cv2.imread("lena.tiff");
gray1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',histogram_equalization(gray1))

FIG effect:
grayscale:
Here Insert Picture Description
after equalization grayscale:
Here Insert Picture Description

Histogram equalization method has a drawback known, equalization would make global image brightness, contrast adjustment, this will cause the original brighter place brighter, higher brightness loss of details. The following diagram, the statue has become the face of the whole Bai after equalization. This time you need to do is optimized.
Reference: https://www.jianshu.com/p/19ff65ac3844
adaptive equalization is used to solve this problem: it is a small area in each of the (default 8 × 8) histogram equalization. Of course, if there is noise, then the noise will be amplified, it is necessary to contrast the small area has been restricted, so the algorithm full name: contrast limited adaptive histogram equalization CLAHE Contrast Limited Adaptive Histogram Equalization

# 自适应均衡化,参数可选
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl1 = clahe.apply(img)

Adaptive equalization renderings :( wide to find the statue of a human face can see the details, and other places have increased contrast, and excellent algorithm?)
Here Insert Picture Description

Here Insert Picture Description
Summary comparison chart:
Here Insert Picture Description
Reference links:
https://baike.baidu.com/item/ probability density function / 5021996
? Https://baike.baidu.com/item/ cumulative distribution function / 7763383 fr = aladdin
HTTPS: // the WWW .cnblogs.com / YOYO-Sincerely / P / 6159101.html
https://blog.csdn.net/xuan_zizizi/article/details/82747897
https://blog.csdn.net/fengbingchun/article/details/79188021
HTTPS: //blog.csdn.net/piaoxuezhong/article/details/78269439
https://www.jianshu.com/p/19ff65ac3844

Histograms Camera related presentations
https://baijiahao.baidu.com/s?id=1621984130964189747&wfr=spider&for=pc

Published 90 original articles · won praise 26 · Views 100,000 +

Guess you like

Origin blog.csdn.net/sky_person/article/details/93524091