在深度学习中加载图像时,如何快速连接图像的 4 个角?
How can I concatenate the 4 corners of the image quickly when loading image in deep learning?
连接 4 个角的最有效方法是什么,如 this photo 所示?
(在 getitem() 中进行)
left_img = Image.open('image.jpg')
...
output = right_img
这就是我要做的。
首先我会暂时将图像转换为张量图像
from torchvision import transforms
tensor_image = transforms.ToTensor()(image)
现在假设你有一个 3 通道图像(虽然类似的原则适用于任何数量通道的任何矩阵,包括 1 通道灰度图像)。
您可以找到 tensor_image[0]
的红色通道,tensor_image[1]
的绿色通道和 tensor_image[2]
的蓝色通道
你可以像
一样创建一个循环遍历每个通道的for循环
for i in tensor_image.size(0):
curr_channel = tensor_image[i]
现在在每个通道的 for 循环中,您可以提取
- 带浮点数的第一个角像素(curr_channel[0][0])
- 带浮动的最后一个顶角像素(curr_channel[0][-1])
- 带浮点数的底部第一个像素(curr_channel[-1][0])
- 带浮动的底部和最后一个像素(curr_channel[-1][-1])
确保在下一个附加步骤之前将所有像素值转换为浮点值或双精度值
现在你有四个值对应于每个通道的角像素
然后你可以创建一个名为 new_image = []
的列表
然后,您可以使用附加上述像素值
new_image.append([[curr_channel[0][0], curr_channel[0][-1]], [curr_channel[-1][0], curr_channel[-1][-1]]])
现在,在遍历每个通道后,您应该有一个包含三个(或 tensor_image.size(0))个列表列表的大列表。
下一步是通过 运行ning
将此列表列表转换为 torch.tensor
new_image = torch.tensor(new_image)
为了确保一切正确 new_image.size() 应该 return torch.Size([3, 2, 2])
如果是这种情况,您现在拥有了想要的图像,但它是张量格式。
将其转换回 PIL 的方法是 运行
final_pil_image = transforms.ToPILImage()(new_image)
如果一切顺利,你应该有一个 pil 图像来完成你的任务。它使用的唯一代码是巧妙的索引和一个 for 循环。
有一种可能,但是如果你看得比我多,那么你可以避免使用 for 循环并在没有循环的情况下对所有通道执行操作。
萨萨克·耆那教
我不知道这有多快,但是在这里:
import numpy as np
img = np.array(Image.open('image.jpg'))
w, h = img.shape[0], image.shape[1]
# the window size:
r = 4
upper_left = img[:r, :r]
lower_left = img[h-r:, :r]
upper_right = img[:r, w-r:]
lower_right = img[h-r:, w-r:]
upper_half = np.concatenate((upper_left, upper_right), axis=1)
lower_half = np.concatenate((lower_left, lower_right), axis=1)
img = np.concatenate((upper_half, lower_half))
或简称:
upper_half = np.concatenate((img[:r, :r], img[:r, w-r:]), axis=1)
lower_half = np.concatenate((img[h-r:, :r], img[h-r:, w-r:]), axis=1)
img = np.concatenate((upper_half, lower_half))
连接 4 个角的最有效方法是什么,如 this photo 所示? (在 getitem() 中进行)
left_img = Image.open('image.jpg')
...
output = right_img
这就是我要做的。 首先我会暂时将图像转换为张量图像
from torchvision import transforms
tensor_image = transforms.ToTensor()(image)
现在假设你有一个 3 通道图像(虽然类似的原则适用于任何数量通道的任何矩阵,包括 1 通道灰度图像)。
您可以找到 tensor_image[0]
的红色通道,tensor_image[1]
的绿色通道和 tensor_image[2]
你可以像
一样创建一个循环遍历每个通道的for循环for i in tensor_image.size(0):
curr_channel = tensor_image[i]
现在在每个通道的 for 循环中,您可以提取
- 带浮点数的第一个角像素(curr_channel[0][0])
- 带浮动的最后一个顶角像素(curr_channel[0][-1])
- 带浮点数的底部第一个像素(curr_channel[-1][0])
- 带浮动的底部和最后一个像素(curr_channel[-1][-1])
确保在下一个附加步骤之前将所有像素值转换为浮点值或双精度值
现在你有四个值对应于每个通道的角像素
然后你可以创建一个名为 new_image = []
的列表
然后,您可以使用附加上述像素值
new_image.append([[curr_channel[0][0], curr_channel[0][-1]], [curr_channel[-1][0], curr_channel[-1][-1]]])
现在,在遍历每个通道后,您应该有一个包含三个(或 tensor_image.size(0))个列表列表的大列表。
下一步是通过 运行ning
将此列表列表转换为 torch.tensornew_image = torch.tensor(new_image)
为了确保一切正确 new_image.size() 应该 return torch.Size([3, 2, 2])
如果是这种情况,您现在拥有了想要的图像,但它是张量格式。 将其转换回 PIL 的方法是 运行
final_pil_image = transforms.ToPILImage()(new_image)
如果一切顺利,你应该有一个 pil 图像来完成你的任务。它使用的唯一代码是巧妙的索引和一个 for 循环。 有一种可能,但是如果你看得比我多,那么你可以避免使用 for 循环并在没有循环的情况下对所有通道执行操作。
萨萨克·耆那教
我不知道这有多快,但是在这里:
import numpy as np
img = np.array(Image.open('image.jpg'))
w, h = img.shape[0], image.shape[1]
# the window size:
r = 4
upper_left = img[:r, :r]
lower_left = img[h-r:, :r]
upper_right = img[:r, w-r:]
lower_right = img[h-r:, w-r:]
upper_half = np.concatenate((upper_left, upper_right), axis=1)
lower_half = np.concatenate((lower_left, lower_right), axis=1)
img = np.concatenate((upper_half, lower_half))
或简称:
upper_half = np.concatenate((img[:r, :r], img[:r, w-r:]), axis=1)
lower_half = np.concatenate((img[h-r:, :r], img[h-r:, w-r:]), axis=1)
img = np.concatenate((upper_half, lower_half))