gpt4 book ai didi

python - 如何有效地用图像的中值填充 RGB numpy 数组?

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

我有已经重新缩放的 RGB 图像,因此较长的边缘变为 256 像素,现在我想用该图像的中值 RGB 值填充边框,因此生成的图像始终为 256x256 像素。

这段代码已经可以工作了,但我相信可以有更简单、更优雅的方法来做到这一点:

img = loadAndFitImage(filePath, maxSideLength=256, upscale=True)
shp = img.shape
#the shp in this case is typically (256,123,3) or (99,256,3)

leftPad = (256 - shp[0]) / 2
rightPad = 256 - shp[0] - leftPad
topPad = (256 - shp[1]) / 2
bottomPad = 256 - shp[1] - topPad

# this part looks like there might be a way to do it with one median call instead of 3:
median = (np.median(img[:, :, 0]),np.median(img[:, :, 1]),np.median(img[:, :, 2]))

img = np.lib.pad(img, ((leftPad,rightPad),(topPad,bottomPad),(0,0)),
'constant',constant_values=0)

if leftPad > 0:
img[:leftPad,:,0].fill(median[0])
img[:leftPad,:,1].fill(median[1])
img[:leftPad,:,2].fill(median[2])
if rightPad > 0:
img[-rightPad:,:,0].fill(median[0])
img[-rightPad:,:,1].fill(median[1])
img[-rightPad:,:,2].fill(median[2])
if topPad > 0:
img[:,:topPad,0].fill(median[0])
img[:,:topPad,1].fill(median[1])
img[:,:topPad,2].fill(median[2])
if bottomPad > 0:
img[:,-bottomPad:,0].fill(median[0])
img[:,-bottomPad:,1].fill(median[1])
img[:,-bottomPad:,2].fill(median[2])

编辑(附加信息):

  • 最终结果应该是这样的:

  • Desired:

  • 它不应该是这样的(每列的中位数):

  • Undesired:

最佳答案

您可以轻松地做到这一点:

import numpy as np

a = np.asarray([[1,2,3,4,5,6],
[8,4,5,6,7,7],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6]])

b = a * 3
c = a * 4
d = (a,b,c)

im = np.asarray([np.pad(x, (2,), 'constant', constant_values=(np.median(x) ,)) for x in d])
print im

输出:

[[[ 4  4  4  4  4  4  4  4  4  4]
[ 4 4 4 4 4 4 4 4 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 8 4 5 6 7 7 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 1 2 3 4 5 6 4 4]
[ 4 4 4 4 4 4 4 4 4 4]
[ 4 4 4 4 4 4 4 4 4 4]]

[[12 12 12 12 12 12 12 12 12 12]
[12 12 12 12 12 12 12 12 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 24 12 15 18 21 21 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 3 6 9 12 15 18 12 12]
[12 12 12 12 12 12 12 12 12 12]
[12 12 12 12 12 12 12 12 12 12]]

[[16 16 16 16 16 16 16 16 16 16]
[16 16 16 16 16 16 16 16 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 32 16 20 24 28 28 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 4 8 12 16 20 24 16 16]
[16 16 16 16 16 16 16 16 16 16]
[16 16 16 16 16 16 16 16 16 16]]]

或者在您的特定情况下:

Original Image

import numpy as np
from PIL import Image

filePath = '/home/george/Desktop/img.jpg'

Img = Image.open(filePath)
img = np.asarray(Img, np.int)
shp = np.shape(img)
img = img.transpose(2,0,1).reshape(3,215,215)

leftPad = round(float((255 - shp[0])) / 2)
rightPad = round(float(255 - shp[0]) - leftPad)
topPad = round(float((255 - shp[1])) / 2)
bottomPad = round(float(255 - shp[1]) - topPad)
pads = ((leftPad,rightPad),(topPad,bottomPad))

img_arr = np.ndarray((3,255,255),np.int)
for i,x in enumerate(img):
cons = np.int(np.median(x))
x_p = np.pad(x,pads,
'constant',
constant_values=cons)
img_arr[i,:,:] = x_p

im_shp = np.shape(img_arr)
ii = np.uint8(img_arr).transpose(1,2,0)

im = Image.fromarray(np.array( (ii) ))
im.show()
im.save((filePath), "JPEG")

输出:

Median Padded Image

关于python - 如何有效地用图像的中值填充 RGB numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635936/

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