您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

用Python分割和合并图像

用Python分割和合并图像

查看image_slicer代码后,我可以看到混乱。主要问题是每个Tile对象都包含图像数据和元数据,例如文件名和最终图像中的位置。但是,当指向的文件被更新时,图像数据不会被更新。

因此,当更新由元数据指向的文件时,也需要更新图块的图像对象。我想最简单的方法是在磁盘上的文件更改时重新打开图块中的图像。这很可能会达到目的:

import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.misc import imsave
from scipy import ndimage
from scipy import misc
import scipy.misc
import scipy
import image_slicer
from image_slicer import join
from PIL import Image

img = 'watch.png'
num_tiles = 64
tiles = image_slicer.slice(img, num_tiles)

for tile in tiles:
    img = scipy.misc.imread(tile.filename)
    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(tile.filename,img3)
    tile.image = Image.open(tile.filename)

image = join(tiles)
image.save('watch-join.png')

因此,主要的更改是tile.image = Image.open(tile.filename)在循环末尾添加。还要注意,我已经通过删除生成文件名的第一个循环来稍微更新了您的代码,相反,第二个循环直接位于磁贴上,因为它们包含了所有所需的信息。

python 2022/1/1 18:32:39 有226人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶