如何使用OpenCV和NumPy将图像均衡化并绘制其直方图

6

我将尝试循环遍历一个包含像素数据的nparray。 我希望对每个像素值执行均衡化并将其作为直方图显示。

我已经通过以下方式达成了我的目标:

def stratch_contrast(img): 

    hist,bins = np.histogram(img.flatten(),256,[0,256])
    cdf = hist.cumsum()
    cdf_normalized = cdf * hist.max()/ cdf.max()

    cdf_m = np.ma.masked_equal(cdf,0)
    cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
    cdf = np.ma.filled(cdf_m,0).astype('uint8')
    img = cdf[img]

    plt.hist(img.flatten(),256,[0,256], color = 'black')
    plt.xlim([0,256])
    plt.legend(('cdf','histogram'), loc = 'upper left')
    plt.show()

    img = cv2.imread(name,0)
    equ = cv2.equalizeHist(img)
    res = np.hstack((img,equ)) #stacking images side-by-side
    cv2.imwrite('res.png',res)

    return

但出于学习目的,我真的希望能够在不使用预定义函数的情况下完成这项任务。

因此,我尝试了以下方法:

 def stratch_contrast(img, darkestValue, whitestValue):

     newImgPixelList = []

     h = img.shape[0] #number of pixels in the hight
     w = img.shape[1] #number of piexels in the weight

     darkestValueStratch = 256 #opposite so it can get darker while loop
     whitestValueStratch = 0 #opposite so it can get lighter while loop

     for y in range(0, w):
         for x in range(0, h):
              newImg[x][y] = (img[x][y]-darkestValue)*256/(whitestValue-darkestValue)
              pxStratch = newImg[x][y]
              newImgPixelList.append(pxStratch)
              if darkestValueStratch > pxStratch:
                  darkestValueStratch = pxStratch
              if whitestValueStratch < pxStratch:
                  whitestValueStratch = pxStratch   

      return newImgPixelList, darkestValueStratch, whitestValueStratch

但是当我调用绘图函数时,像这样:
plot(newImgPixelList, int(darkestValueStratch), int(whitestValueStratch))

绘制的直方图完全没有被均衡化。它看起来几乎和我未均衡化的直方图完全相同,因此一定有什么问题。

如果有人能帮我解决这个问题,我将非常感激!

我的完整代码:

import matplotlib.pyplot as plt
import numpy as np
import cv2
np.seterr(over='ignore')

name = 'puppy.jpg'

img = cv2.imread(name, cv2.IMREAD_GRAYSCALE) #import image
newImg = np.zeros((img.shape))

def get_histo_scope(img):

    imgPixelList = [] #array which later can save the pixel values of the image

    h = img.shape[0] #number of pixels in the hight
    w = img.shape[1] #number of piexels in the weight

    darkestValue = 256 #opposite so it can get darker while loop
    whitestValue = 0 #opposite so it can get lighter while loop

    for y in range(0, w):
        for x in range(0, h):       
            px = img[x][y] #reads the pixel which is a npndarray [][][]
            imgPixelList.append(px) #saves the pixel data of every pixel we loop so we can use it later to plot the histogram
            if darkestValue > px: #identifies the darkest pixel value
                darkestValue = px
            if whitestValue < px: #identifies the whitest pixel value
                whitestValue = px 

    return darkestValue, whitestValue, imgPixelList

def plot(imgPixelList, darkestValue, whitestValue):
    values = range(darkestValue, whitestValue, 1) #creates and array with all data from whitesValue to darkestValue
    bin_edges = values

    plt.hist(imgPixelList, bins=bin_edges, color='black')
    plt.xlabel('Color Values')
    plt.ylabel('Number of Poxels')
    plt.show()  

    return     

def stratch_contrast(img, darkestValue, whitestValue): 

    #hist,bins = np.histogram(img.flatten(),256,[0,256])
    #cdf = hist.cumsum()
    #cdf_normalized = cdf * hist.max()/ cdf.max()

    #Comment out to remove Equalization 
    #cdf_m = np.ma.masked_equal(cdf,0)
    #cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
    #cdf = np.ma.filled(cdf_m,0).astype('uint8')
    #img = cdf[img]

    #plt.hist(img.flatten(),256,[0,256], color = 'black')
    #plt.xlim([0,256])
    #plt.legend(('cdf','histogram'), loc = 'upper left')
    #plt.show()

    #img = cv2.imread(name,0)
    #equ = cv2.equalizeHist(img)
    #res = np.hstack((img,equ)) #stacking images side-by-side
    #cv2.imwrite('res.png',res)

    newImgPixelList = []

    h = img.shape[0] #number of pixels in the hight
    w = img.shape[1] #number of piexels in the weight

    darkestValueStratch = 256 #oposite so it can get darker while loop
    whitestValueStratch = 0 #oposite so it can get lighter while loop

    for y in range(0, w):
       for x in range(0, h):
            newImg[x][y] = (img[x][y]-darkestValue)*256/(whitestValue-darkestValue)
            pxStratch = newImg[x][y]
            newImgPixelList.append(pxStratch)
            if darkestValueStratch > pxStratch: #identifies the darkest pixel value
                darkestValueStratch = pxStratch
            if whitestValueStratch < pxStratch: #identifies the whitest pixel value
                whitestValueStratch = pxStratch   

    return newImgPixelList, darkestValueStratch, whitestValueStratch

darkestValue, whitestValue, imgPixelList = get_histo_scope(img) #get scope and pixel values from the img data

plot(imgPixelList, darkestValue, whitestValue) #plot the collected pixel values

newImgPixelList, darkestValueStratch, whitestValueStratch = stratch_contrast(img, darkestValue, whitestValue)

plot(newImgPixelList, int(darkestValueStratch), int(whitestValueStratch))

stratch_contrast 函数存在问题。公式中使用的 darkestValuewhitestValue 是从哪里得来的?它们与 darkestValueStratchwhitestValueStratch 相同吗? - Sunreef
第一个函数应用cv.equalizeHist,它应用直方图均衡化。第二个函数应用线性映射,并剪切范围外的值。如果你认为这两个操作除了有点关联之外没有任何关系,那么你需要回到你的教科书里看看。 - Cris Luengo
这个问题的答案解释了一下算法:https://stackoverflow.com/q/11341440/7328782 - Cris Luengo
谢谢你的回答,Chris。但不幸的是,我不想使用预定义的函数,就像我在问题中所说的那样。我想使用自己的函数(参见 stratch_contrast)并且我想知道为什么这不起作用。因此,我的问题不是重复的。 - Leonard Michalas
那个答案展示了如何使用“histogram”、“cumsum”和“interp”实现直方图均衡化。这些都是相当基本的函数,而且很容易自己实现。我建议你从那里开始。将其标记为重复旨在提供帮助,而不是表示不屑一顾。毕竟,我正在给你一个答案。--顺便说一句:请在回复评论时输入“@CrisLuengo”(或任何用户名),以确保您的回复被阅读。 - Cris Luengo
显示剩余5条评论
1个回答

3
我觉得你误解了对比度拉伸算法。
该算法的目标是线性地缩放像素值,以便您的图像使用完整的可用动态范围,即 min(I)= 0max(I)= 255
为此,在循环遍历像素并对其进行缩放之前,必须找到当前的 min(I)max(I)。只需遍历整个图像,同时跟踪每个通道(RGB图像的3个通道)的最大和最小值。然后使用这些值使用公式newValue = 255 *(oldValue-minimum)/(maximum-minimum)来缩放像素。独立处理R、G 和 B三个通道。

这就是我在get_histo_scope()中正在做的事情。我只是再次跟踪darkestValueStratch和whitestValueStratch。但是我使用了在get_histo_scope中获取的darkestValue和whitestValue来执行计算((img[x][y] - darkestValue)* 256 /(whitestValue-darkestValue))。 - Leonard Michalas
但是你没有将值传递给stratch_contrast函数。而且你没有独立处理通道。 - Sunreef
即使我传递了值,错误仍然存在。我刚刚编辑了代码。由于目前我只处理灰度图片,所以不需要处理不同的通道,对吗? - Leonard Michalas
还有什么其他的想法我可以尝试吗? - Leonard Michalas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接