OpenCV_algorithm: based_on_Python_and_C++, histogram regularization

OpenCV algorithm: based on Python and C++, histogram regularization

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

Histogram regularization

Mainly by mapping pixels to other intervals, that is, a unified mapping of pixel values, mapping from a certain interval to other intervals

The input image is I, the output image is O, the default is a grayscale image, the RGB image can be divided into channels, r is the input row, and c is the input column.

The formula for normalizing the maximum and minimum values of the image is:

x

=

I

(

r

,

c

)

?

I

m

i

n

I

m

a

x

?

I

m

i

n

x=\frac{I(r,c)-I_{min}}{I_{max}-I_{min}}

x=ImaxImin?I(r,c)?Imin, because the normalized values before and after transformation are equal, so

O

(

r

,

c

)

?

O

m

i

n

O

m

a

x

?

O

m

i

n

=

I

(

r

,

c

)

?

I

m

i

n

I

m

a

x

?

I

m

i

n

\frac{O(r,c)-O_{min}}{O_{max}-O_{min}}=\frac{I(r,c)-I_{min}}{I_{max}-I_{ min}}

OmaxOmin?O(r,c)?Omin=ImaxImin?I(r,c)?Imin , and we can get:

O

(

r

,

c

)

=

I

(

r

,

c

)

?

I

m

i

n

I

m

a

x

?

I

m

i

n

(

O

m

a

x

?

O

m

i

n

)

+

O

m

i

n

O(r,c)=\frac{I(r,c)-I_{min}}{I_{max}-I_{min}}(O_{max}-O_{min}) + O_{min}

O(r,c)=ImaxImin?I(r,c)?Imin(OmaxOmin?) + Omin?

So as long as it is given

O

m

i

n

,

O

m

a

x

O_{min}, O_{max}

Omin?,Omax?, you can do interval mapping of pixel values.

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 histgray_image():
    pth = os.path.join(filepath, r'..\shift\sunoray.png')
    img = cv2.imread(pth, 0)
    h, w, = img.shape
    img = cv2.resize(img, (w//3, h//3))
    Imin = np.min(img, )
    Imax = np.max(img, )
    Omin = 100
    Omax = 255
    nim = ((img - Imin) / (Imax - Imin) ) * (Omax - Omin) + Omin
    img = np.concatenate([img, nim], axis = 1)
    cv2.imwrite(os.path.join(filepath, r'histnormalize.jpg'), img)

if __name__ == "__main__":
    histgray_image()

“OpenCV Algorithm Exquisite Analysis: Based on Python and C++” (edited by Zhang Ping) [Introduction_Book Review_Online Reading] – Dangdang Books (dangdang.com)

https://zhuanlan.zhihu.com/p/659648213