gpt4 book ai didi

python - 如何将多个图像填充到包含所有图像的最小形状?

转载 作者:行者123 更新时间:2023-12-01 00:31:51 34 4
gpt4 key购买 nike

我正在尝试编写一个函数来加载和处理 NN 的数据。作为输入,我有一组不同尺寸的图片。图片应表示为具有 RGB channel 的 3D numpy 数组。我需要它们具有相同的尺寸(最大图片的尺寸)。

我已经尝试过np.pad,但似乎我不知道它应该如何工作。实际上,即使我有填充,我也不知道如何根据图片的大小来改变它。代码如下:

from PIL import Image
import numpy as np
import cv2
import os


def load_data(path):
aminoacids = ['Ala','Arg','Asn','Asp','Cys','Gln','Glu','Gly','His','Ile', 'Ini', 'Leu','Lys','Met','Phe','Pro','Pyr', 'Sec','Ser','Thr','Trp','Tyr','Val']
matrix = []
answer_labeled = []
names = os.listdir(path)

for i in names:
matrix = cv2.imread(path + i, 1)
matrix = np.pad(matrix, (0, 1), 'constant', constant_values=[255,255,255])
for y in aminoacids:
if y + "-" in i:
a = [matrix, y]
answer_labeled.append(a)
return answer_labeled
data_processed = load_data("/content/drive/My Drive/Thesis/dl/img/ans/")

我收到此错误:

ValueErrorTraceback (most recent call last)
<ipython-input-50-e021738e59ea> in <module>()
20 return answer_labeled
21
---> 22 data_processed = load_data("/content/drive/My Drive/Thesis/dl/img/ans/")
23
24 # print(len(os.listdir("/content/drive/My Drive/Thesis/dl/img/ans/")))

<ipython-input-50-e021738e59ea> in load_data(path)
13 for i in names:
14 matrix = cv2.imread(path + i, 1)
---> 15 matrix = np.pad(matrix, (0, 1), 'constant', constant_values=[255,255,255])
16 for y in aminoacids:
17 if y + "-" in i:

/usr/local/lib/python2.7/dist-packages/numpy/lib/arraypad.pyc in pad(array, pad_width, mode, **kwargs)
1208 kwargs[i] = _as_pairs(kwargs[i], narray.ndim, as_index=True)
1209 if i in ['end_values', 'constant_values']:
-> 1210 kwargs[i] = _as_pairs(kwargs[i], narray.ndim)
1211 else:
1212 # Drop back to old, slower np.apply_along_axis mode for user-supplied

/usr/local/lib/python2.7/dist-packages/numpy/lib/arraypad.pyc in _as_pairs(x, ndim, as_index)
951 # Converting the array with `tolist` seems to improve performance
952 # when iterating and indexing the result (see usage in `pad`)
--> 953 return np.broadcast_to(x, (ndim, 2)).tolist()
954
955

/usr/local/lib/python2.7/dist-packages/numpy/lib/stride_tricks.pyc in broadcast_to(array, shape, subok)
180 [1, 2, 3]])
181 """
--> 182 return _broadcast_to(array, shape, subok=subok, readonly=True)
183
184

/usr/local/lib/python2.7/dist-packages/numpy/lib/stride_tricks.pyc in _broadcast_to(array, shape, subok, readonly)
127 it = np.nditer(
128 (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
--> 129 op_flags=[op_flag], itershape=shape, order='C')
130 with it:
131 # never really has writebackifcopy semantics

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,) and requested shape (3,2)

当然,我尝试用谷歌搜索这个错误,但没有找到对我有用或可以理解的东西(因为我在编程方面确实是新手)。我将不胜感激任何帮助和想法。

最佳答案

我曾经必须解决类似的任务,所以我为其创建了以下函数。它允许指定每个维度的大小差异的分数,应在之前和之后填充(类似于 np.pad )。例如,如果您有两个形状为 (3,)(5,) 的数组,则 before=1 将填充整个差异 (在本例中,2) 位于左侧,而 before=0.5 则在左侧填充一个元素,在右侧填充一个元素。与np.pad类似,这些因素也可以为每个维度指定。这是实现:

import numpy as np


def pad_max_shape(arrays, before=None, after=1, value=0, tie_break=np.floor):
"""Pad the given arrays with a constant values such that their new shapes fit the biggest array.

Parameters
----------
arrays : sequence of arrays of the same rank
before, after : {float, sequence, array_like}
Similar to `np.pad -> pad_width` but specifies the fraction of values to be padded before
and after respectively for each of the arrays. Must be between 0 and 1.
If `before` is given then `after` is ignored.
value : scalar
The pad value.
tie_break : ufunc
The actual number of items to be padded _before_ is computed as the total number of elements
to be padded times the `before` fraction and the actual number of items to be padded _after_
is the remainder. This function determines how the fractional part of the `before` pad width
is treated. The actual `before` pad with is computed as ``tie_break(N * before).astype(int)``
where ``N`` is the total pad width. By default `tie_break` just takes the `np.floor` (i.e.
attributing the fraction part to the `after` pad width). The after pad width is computed as
``total_pad_width - before_pad_width``.

Returns
-------
padded_arrays : list of arrays

Notes
-----
By default the `before` pad width is computed as the floor of the `before` fraction times the number
of missing items for each axis. This is done regardless of whether `before` or `after` is provided
as a function input. For that reason the fractional part of the `before` pad width is attributed
to the `after` pad width (e.g. if the total pad width is 3 and the left fraction is 0.5 then the
`before` pad width is 1 and the `after` pad width is 2; in order to f). This behavior can be controlled
with the `tie_break` parameter.
"""
shapes = np.array([x.shape for x in arrays])
if before is not None:
before = np.zeros_like(shapes) + before
else:
before = np.ones_like(shapes) - after
max_size = shapes.max(axis=0, keepdims=True)
margin = (max_size - shapes)
pad_before = tie_break(margin * before.astype(float)).astype(int)
pad_after = margin - pad_before
pad = np.stack([pad_before, pad_after], axis=2)
return [np.pad(x, w, mode='constant', constant_values=value) for x, w in zip(arrays, pad)]

对于您的示例,您可以按如下方式使用它:

test = [np.ones(shape=(i, i, 3)) for i in range(5, 10)]
result = pad_max_shape(test, before=0.5, value=255)

print([x.shape for x in result])
print(result[0][:, :, 0])

这会产生以下输出:

[(9, 9, 3), (9, 9, 3), (9, 9, 3), (9, 9, 3), (9, 9, 3)]
[[255. 255. 255. 255. 255. 255. 255. 255. 255.]
[255. 255. 255. 255. 255. 255. 255. 255. 255.]
[255. 255. 1. 1. 1. 1. 1. 255. 255.]
[255. 255. 1. 1. 1. 1. 1. 255. 255.]
[255. 255. 1. 1. 1. 1. 1. 255. 255.]
[255. 255. 1. 1. 1. 1. 1. 255. 255.]
[255. 255. 1. 1. 1. 1. 1. 255. 255.]
[255. 255. 255. 255. 255. 255. 255. 255. 255.]
[255. 255. 255. 255. 255. 255. 255. 255. 255.]]

所以我们可以看到每个数组都被对称地填充到最大数组(9, 9, 3)的形状。

关于python - 如何将多个图像填充到包含所有图像的最小形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58115013/

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