gpt4 book ai didi

python - 如何均衡图像并将其绘制为带有 openCV 和 numpy 的直方图

转载 作者:太空狗 更新时间:2023-10-30 00:09:07 25 4
gpt4 key购买 nike

我正在尝试遍历包含像素数据的 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))

最佳答案

我认为您误解了对比度拉伸(stretch)算法。

该算法的目标是线性缩放像素值,以便您的图像使用可用的完整动态范围,即 min(I) = 0max(I) = 255

为此,您必须找到当前的 min(I)max(I) before 遍历像素并缩放它们.只需循环遍历整个图像,同时跟踪每个 channel 的最大值和最小值(RGB 图像有 3 个 channel )。然后使用公式 newValue = 255 * (oldValue - minimum)/(maximum - minimum) 使用这些值来缩放像素。 独立处理每个 R、G 和 B channel

关于python - 如何均衡图像并将其绘制为带有 openCV 和 numpy 的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com