python image processing: histogram of predetermined (histogram matching)

EDITORIAL
because I digital image processing job is treated with the requirements of the provisions of VB to do, after I write python wanted to see what libraries can achieve, after all, like equalization operation and the like are available through the cv2 py library in functions to solve, but when queried on CSDN found that we are all to do with openCV or matlab, python and no relevant function to the provisions of, the only provisions of the code, but is required to pay, so I I wrote a grayscale image codes can be done using histogram matching python.
(A) describes the principle of histogram matching
histogram specification, also known as histogram matching, for converting the image for a particular distribution of gray, which is the object of the histogram is known. This in fact is very similar to the equalization, the equalized histogram gradation is also known, is a uniformly distributed histogram; of the predetermined histogram can freely specify, that is performed when a predetermined operation, You must first know the gray histogram after conversion, so as to determine the transformation function. A predetermined operation section is capable of enhancing a gradation object, as compared to, equalization operation, a plurality of predetermined input, but also converts the result of a more flexible.
(B) implement a histogram matching
Histogram Specification implementation can be divided into about four steps:

  1. Original image cumulative histogram is calculated
  2. Computing requirements cumulative histogram histogram
  3. Calculating an absolute value of a difference of two cumulative histogram
  4. Establishing a mapping according to the gray level difference between the cumulative histogram

(C) Coding

import cv2

import numpy as np

import matplotlib.pyplot as plt

img0=cv2.imread(r'C:\Users\58381\Desktop\lena.bmp')#读取原图片

scr=cv2.imread(r'C:\Users\58381\Desktop\circle.bmp')#读取目标图片

Provision of transfer pictures to grayscale images after the completion of the reading, only do the current grayscale images

#把两张图片转成真正的灰度图片,因为自己只会做灰度图片的规定化

img0=cv2.cvtColor(img0,cv2.COLOR_BGR2GRAY)

img=img0.copy()#用于之后做对比图

scr=cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)

Mapping structure do list

mHist1=[]

mNum1=[]

inhist1=[]

mHist2=[]

mNum2=[]

inhist2=[]

Next, the two images separately equalization

#对原图像进行均衡化
for i in range(256):

    mHist1.append(0)

row,col=img.shape#获取原图像像素点的宽度和高度



for i in range(row):

    for j in range(col):

        mHist1[img[i,j]]= mHist1[img[i,j]]+1#统计灰度值的个数

mNum1.append(mHist1[0]/img.size)

for i in range(0,255):

    mNum1.append(mNum1[i]+mHist1[i+1]/img.size)

for i in range(256):

    inhist1.append(round(255*mNum1[i]))
    
 #对目标图像进行均衡化

for i in range(256):

    mHist2.append(0)

rows,cols=scr.shape#获取目标图像像素点的宽度和高度

for i in range(rows):

    for j in range(cols):

        mHist2[scr[i,j]]= mHist2[scr[i,j]]+1#统计灰度值的个数

mNum2.append(mHist2[0]/scr.size)

for i in range(0,255):

    mNum2.append(mNum2[i]+mHist2[i+1]/scr.size)

for i in range(256):

    inhist2.append(round(255*mNum2[i]))

After two images are equalized good, performs predetermined processing by mapping between arrays

#进行规定化

g=[]#用于放入规定化后的图片像素

for i in range(256):

    a=inhist1[i]

    flag=True

    for j in range(256):

        if inhist2[j]==a:

            g.append(j)

            flag=False

            break

    if flag==True:

        minp=255

        for j in range(256):

            b=abs(inhist2[j]-a)
            if b<minp:                
                                    
                minp=b

                jmin=j

        g.append(jmin)

for i in range(row):

    for j in range(col):

        img[i,j]=g[img[i,j]]

Here provision of complete, we have to see the effect.
Take a look at the original image and its histogram

cv2.imshow("原图",img0)
cv2.waitKey(0)
plt.hist(img0.ravel(),256)
plt.show()

Original and histograms
Then the target picture and the histogram of the original image

cv2.imshow("目标图",scr)
cv2.waitKey(0)
plt.hist(scr.ravel(),256)
plt.show()

Here Insert Picture Description
Then look at the picture and after the provisions of its histogram

cv2.imshow("规定化",img)
cv2.waitKey(0)
plt.hist(img.ravel(),256)
plt.show()

Here Insert Picture Description
Finally, the show together two pictures.
Here Insert Picture Description
Written in the last
to color pictures of the provisions of the Code after good measure I will finish work as soon as possible due to time constraints, the author of the code is not particularly streamlined, there are many places you can optimize, we make do with a look.

Guess you like

Origin blog.csdn.net/qq_44171096/article/details/89232757