OpenCV_algorithm: based_on_Python_and_C++, global histogram equalization

OpenCV algorithm: based on Python and C++, global histogram equalization

ZouJiu1/Opencv_C_algorithm: algorithm of opencv (github.com)

Opencv_C_algorithm/contract/linear_transform.py.py at master · ZouJiu1/Opencv_C_algorithm (github.com)?github.com/ZouJiu1/Opencv_C_algorithm/tree/master/contract/linear_transform.py

The grayscale histogram, mentioned above, counts the number of all grayscale values and draws a curve. It can be seen that the number of pixels for each grayscale value has a very obvious difference. Some grayscales The number of pixels at some grayscale values is very large, and the number of pixels at some grayscale values is very small.

Global histogram equalization performs an average operation on the grayscale histogram to make the grayscale histogram horizontal, and puts the values of certain pixels into other grayscale values, so that the values at each grayscale value are The number of pixels is basically the same. The total number of pixels in a picture is H * W, and the gray value range is [0-255], a total of 256 gray values, so on average, each gray value can be assigned to

H

?

W

256

\frac{H*W}{256}

256H?W? pixels. However, it is actually impossible to allocate completely on average, so there will still be situations where some grayscale values have more pixels after global histogram equalization.

For the initial picture I and the equalized picture O, for any gray value P in I, there is always a certain gray value Q in O, such that

i

=

0

P

H

I

(

i

)

=

o

=

0

Q

H

O

(

o

)

\sum_{i=0}^{P}H_I(i)=\sum_{o=0}^{Q}H_O(o)

∑i=0P?HI?(i)=∑o=0Q?HO?(o)

H

I

(

i

)

H_I(i)

HI?(i) is the number of pixels with a grayscale value equal to i. The left side is the input image and the right side is the output image. That is, the number of accumulated pixels should be equal or similar. Or called a cumulative histogram.

Since the image after global histogram equalization has a similar number of pixels for each gray value, we might as well let the number of pixels for each gray value be

H

?

W

256

\frac{H*W}{256}

256H?W?. So

i

=

0

P

H

I

(

i

)

=

H

?

W

256

?

(

Q

+

1

)

\sum_{i=0}^{P}H_I(i)=\frac{H*W}{256}*(Q + 1)

∑i=0P?HI?(i)=256H?W(Q + 1), P and Q are the grayscale values of the input and output pixels respectively. Then a corresponding term P is formed corresponding to Q, that is, the previous pixel value is P, and the pixel value after global histogram equalization is Q.

Q

=

256

H

?

W

i

=

0

P

H

I

(

i

)

?

1

Q=\frac{256}{H*W}\sum_{i=0}^{P}H_I(i)-1

Q=H?W256?∑i=0P?HI?(i)?1

Therefore, global histogram equalization requires the use of cumulative histograms, and the values before and after each pixel change can be calculated accordingly.

import os
importsys
filepath = os.path.abspath(__file__)
nam = filepath.split(os.sep)[-1]
filepath = filepath.replace(nam, "")
sys.path.append(filepath)

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

def equal_histgray_image():
    # pth = os.path.join(filepath, r'..\shift\sunoray.png')
    # pth = os.path.join(filepath, r'..\an.jpg')
    pth = os.path.join(filepath, r'..\hunan.jpg')
    img = cv2.imread(pth, 0)
    h, w = img.shape
    img = cv2.resize(img, (w//4, h//4))
    h, w = img.shape
    count = np.zeros(256)
    for i in range(h):
        for j in range(w):
            count[img[i, j]] + = 1
    cumsum = np.cumsum(count)
    nimg = np.zeros_like(img)
    areas = h * w
    for i in range(h):
        for j in range(w):
            nimg[i, j] = (256 / areas) * cumsum[img[i, j]] - 1
    nimg[nimg > 255] = 255
    nimg[nimg < 0] = 0
    # plt.plot(range(len(count)), count)
    # plt.plot(range(len(count)), np.cumsum(count))
    # plt.show()
    # plt.close()
    img = np.concatenate([img, nimg], axis = 1)
    cv2.imwrite(os.path.join(filepath, r'equalhist.jpg'), img)

if __name__ == "__main__":
    equal_histgray_image()


The grayscale histogram-picture after global histogram equalization, has changed a bit
https://zhuanlan.zhihu.com/p/662160122