将图像分成规则的块。

huangapple go评论113阅读模式
英文:

how to scramble an image into regular patches?

问题

需要将图像分割成3x3或任何常规的块,然后对它们进行随机排序,最后重新组合图像。我尝试使用patchify,但在unpatchify时出现错误。

  1. import numpy as np
  2. from PIL import Image
  3. from patchify import patchify, unpatchify
  4. import matplotlib.pyplot as plt
  5. from random import shuffle
  6. import torch
  7. import torchvision
  8. import torchvision.transforms as transforms
  9. import requests
  10. transform = transforms.Compose(
  11. [transforms.ToTensor(),
  12. transforms.Resize((224,224)),
  13. transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
  14. url = 'https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg'
  15. image = Image.open(requests.get(url, stream=True).raw)
  16. plt.imshow(transform(image).permute(1,2,0))

在洗牌或拆分成块之前,图像看起来如下所示:

将图像分成规则的块。

  1. patched_image = patchify(np.array(image), (100, 100, 3), 100)
  2. shuffled_patches = np.random.shuffle(patched_image)
  3. output_image = unpatchify(shuffled_patches, np.array(image).shape)

错误信息:

  1. AttributeError Traceback (most recent call last)
  2. <ipython-input-34-0b9963e642b8> in <cell line: 1>()
  3. ----> 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)
  4. /usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
  5. 52 """
  6. 53
  7. ---> 54 assert len(patches.shape) / 2 == len(
  8. 55 imsize
  9. 56 ), "The patches dimension is not equal to the original image size"
  10. AttributeError: 'NoneType' object has no attribute 'shape'
英文:

I need to patch images into let say 3x3 patches, or any regualr patches, then shuffle them and after that reassemble the image. I tried to use patchify but it gives eerror for unpatchify.

  1. import numpy as np
  2. from PIL import Image
  3. from patchify import patchify, unpatchify
  4. import matplotlib.pyplot as plt
  5. from random import shuffle
  6. import torch
  7. import torchvision
  8. import torchvision.transforms as transforms
  9. import requests
  10. transform = transforms.Compose(
  11. [transforms.ToTensor(),
  12. transforms.Resize((224,224)),
  13. transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
  14. url = &#39;https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg&#39;
  15. image = Image.open(requests.get(url, stream=True).raw)
  16. plt.imshow(transform(image).permute(1,2,0))

I looks like the following before shuffling or patchfying

将图像分成规则的块。

  1. patched_image = patchify(np.array(image), (100, 100, 3), 100)
  2. shuffled_patches = np.random.shuffle(patched_image)
  3. output_image = unpatchify(shuffled_patches, np.array(image).shape)

The error message:

  1. AttributeError Traceback (most recent call last)
  2. &lt;ipython-input-34-0b9963e642b8&gt; in &lt;cell line: 1&gt;()
  3. ----&gt; 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)
  4. /usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
  5. 52 &quot;&quot;&quot;
  6. 53
  7. ---&gt; 54 assert len(patches.shape) / 2 == len(
  8. 55 imsize
  9. 56 ), &quot;The patches dimension is not equal to the original image size&quot;
  10. AttributeError: &#39;NoneType&#39; object has no attribute &#39;shape&#39;

答案1

得分: 1

np.random.shuffle 不返回任何内容,它会就地对输入进行洗牌,请参见 numpy 文档。这类似于标准例程 sort()

当尝试在 unpatchify 过程中使用返回的 None 时,会出现这种情况。

如果你想保留先前的状态,也许可以使用 copy 模块来在洗牌之前创建一个克隆,org_image = copy.copy(patched_image) 应该足够了。

英文:

np.random.shuffle does not return anything, it shuffles the given input in place, see numpy documentation. This is similar to the standard routine sort().

This shows up, when attempting to use the returned None during unpatchify.

If you want to keep the previous state as well, maybe the copy module may assist in creating a clone before shuffling, org_image = copy.copy(patched_image) should be sufficient.

huangapple
  • 本文由 发表于 2023年7月6日 19:39:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定