【发布时间】:2017-09-19 18:55:10
【问题描述】:
我正在尝试使用 python 中的图像切片器分割图像,然后对它们中的每一个应用直方图均衡化并将它们组合回来。我能够将图像拆分成更小的块,并且可以看到它们正在更新,但是在将它们拼接在一起之后,我最终得到了与原始图像相同的图像。有人可以指出我做错了什么。文件名为 watch.png
import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.misc import imsave
# import scipy
from scipy import ndimage
from scipy import misc
import scipy.misc
import scipy
import sys
import argparse
import image_slicer
from image_slicer import join
img = 'watch.png'
num_tiles = 64
tiles = image_slicer.slice(img, num_tiles)
file = "watch"
k = 0
filelist =[]
for i in range(1,9):
for j in range(1,9):
filelist.insert(k, file+"_"+str(i).zfill(2)+"_"+str(j).zfill(2)+".png")
k=k+1
for i in range(0,num_tiles):
img = scipy.misc.imread(filelist[i])
hist,bins = np.histogram(img.flatten(),256,[0,256])
cdf = hist.cumsum()
cdf_normalized = cdf *hist.max()/ cdf.max()
plt.plot(cdf_normalized, color = 'g')
plt.hist(img.flatten(),256,[0,256], color = 'g')
plt.xlim([0,256])
plt.legend(('cdf','histogram'), loc = 'upper left')
cdf_m = np.ma.masked_equal(cdf,0)
cdf_o = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
cdf = np.ma.filled(cdf_o,0).astype('uint8')
img3 = cdf[img]
cv2.imwrite(filelist[i],img3)
image = join(tiles)
image.save('watch-join.png')
【问题讨论】:
-
您的示例似乎不完整,因为您从未将切片存储在插入
filelist的文件中。但是,从代码中猜测我确实认为您会这样做,因为您似乎能够阅读不同的图像。但我的猜测是你忘记更新tilesso 最后,你只是join原始的,未修改(因此,未更新)的图块,这当然会再次为您提供原始图像。 -
@JohanL 我正在将修改后的图像块写回相同的文件名,所以我认为它不应该有所作为。我尝试打印瓷砖,它给了我它引用的文件名列表,它们指向正确的文件名。在我缝合图像之前,您有什么其他方法可以建议更新图块
-
@user2808264-像您的问题一样,在完整图像上应用直方图均衡和在图像的每个部分上应用其他方面有什么区别吗?
标签: python image-processing scipy histogram