向现有图像添加零填充nd数组会返回已修改的图像

问题描述 投票:1回答:2

因此,对于我正在做的一些ml工作,我需要能够生成随机图像并将它们添加到现有图像中。为了测试,我生成这些随机图像并将它们乘以零,然后将它们添加到我现有的图像中。我希望收到一张与原始图像相同的新图像,但我会收到原始图像的蓝色版本:

Kazkhsvpoi c。 kazhsvpoi

我现在已经反对这个问题几个小时了,似乎无法得出导致这种差异的原因。这是相关代码:

Original

为了完全公开,我正在迭代几千个图像,并且unit_img对于每个图像是不同的。运行一个简单的测试程序,加载上面显示的猫图像并打印出来,我看到:

Generated

很明显,图像在数值上是相似的,但沿途一些轴变成了倒置。我到目前为止尝试的是检查我的unit_img帖子

# unit_img is an ndarray with random entries, then normalized 
# so that the sum of the squares of all the elements is 1

# min_dist is our scalar we multiply our unit image by, since 
# it's zero we don't care about the unit image

min_dist = 0
...
unit_img = np.load(path_to_unit_img)
unit_img = min_dist * unit_img

# check if our unit img and our original image are the same size
if unit_img.size != checked_img.size:
    continue
# "move" our new image to the solution space of the original img
addition = unit_img + checked_img

result_img = Image.fromarray(addition.astype('uint8')).convert('RGB')

# now we save our generated image
result_img.save(save_path + extension + img[:-4] + "_" + str(x) + ".jpg")

通过执行np.count_nonzero实际上是零,并且它总是有0个非零元素,这意味着我真的在添加一个充满零的ndarray。这意味着我以某种方式错误地保存图像,或者可能是错误的数据类型。任何帮助都将不胜感激!

python numpy python-imaging-library
2个回答
0
投票

你是如何打开图像的?可能是因为它不是以RGB格式开始的,而且你的转换正在搞乱吗?如果您使用的是openCV,则图像为# original image [[[164 159 160] [164 159 160] [164 159 160] ... [152 152 152] [152 152 152] [152 152 152]] [[165 160 161] [165 160 161] [165 160 161] ... [152 152 152] [152 152 152] [152 152 152]] [[162 160 160] [162 160 160] [162 160 160] ... [152 152 152] [152 152 152] [152 152 152]] ... [[151 143 136] [151 143 136] [151 143 136] ... [ 81 81 81] [ 83 83 83] [ 85 85 85]] [[152 144 137] [152 144 137] [152 144 137] ... [ 86 86 86] [ 83 83 83] [ 82 82 82]] [[152 144 137] [152 144 137] [152 144 137] ... [ 89 89 89] [ 82 82 82] [ 78 78 78]]] =========================================== # Resultant from adding an array of zeros [[[160 159 163] [160 159 163] [160 159 163] ... [152 152 152] [152 152 152] [152 152 152]] [[161 160 164] [161 160 164] [161 160 164] ... [152 152 152] [152 152 152] [152 152 152]] [[160 159 161] [160 159 161] [160 159 161] ... [152 152 152] [152 152 152] [152 152 152]] ... [[137 143 150] [137 143 150] [137 143 150] ... [ 80 80 80] [ 83 83 83] [ 87 87 87]] [[138 144 151] [138 144 151] [138 144 151] ... [ 86 86 86] [ 83 83 83] [ 82 82 82]] [[138 144 151] [138 144 151] [138 144 151] ... [ 89 89 89] [ 82 82 82] [ 77 77 77]]]

另外,你说unit_img = min_dist * unit_img 是检查图像大小是否相等,但这实际上是检查它们是否相等。无论哪种方式,代码的其余部分将运行,因为它是未缩进的,因此它不是逻辑的一部分


0
投票

我只需要添加:

may start out in BGR format

在任何时候我使用cv2.imread()加载图像。这是因为imread将文件读取为BGR而不是RGB,因此轴将被反转。感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.