将图像分成规则的块。

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

how to scramble an image into regular patches?

问题

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

import numpy as np
from PIL import Image
from patchify import patchify, unpatchify
import matplotlib.pyplot as plt
from random import shuffle
import torch
import torchvision
import torchvision.transforms as transforms
import requests

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Resize((224,224)),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

url = 'https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg'
image = Image.open(requests.get(url, stream=True).raw)

plt.imshow(transform(image).permute(1,2,0))

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

将图像分成规则的块。

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

错误信息:

AttributeError                            Traceback (most recent call last)
<ipython-input-34-0b9963e642b8> in <cell line: 1>()
----> 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)

/usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
     52     """
     53 
---> 54     assert len(patches.shape) / 2 == len(
     55         imsize
     56     ), "The patches dimension is not equal to the original image size"

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.

import numpy as np
from PIL import Image
from patchify import patchify, unpatchify
import matplotlib.pyplot as plt
from random import shuffle
import torch
import torchvision
import torchvision.transforms as transforms
import requests


transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Resize((224,224)),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

url = &#39;https://hips.hearstapps.com/hmg-prod/images/bright-forget-me-nots-royalty-free-image-1677788394.jpg&#39;
image = Image.open(requests.get(url, stream=True).raw)

plt.imshow(transform(image).permute(1,2,0))


I looks like the following before shuffling or patchfying

将图像分成规则的块。

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

The error message:

AttributeError                            Traceback (most recent call last)
&lt;ipython-input-34-0b9963e642b8&gt; in &lt;cell line: 1&gt;()
----&gt; 1 output_image = unpatchify(shuffled_patches, np.array(image).shape)

/usr/local/lib/python3.10/dist-packages/patchify/__init__.py in unpatchify(patches, imsize)
     52     &quot;&quot;&quot;
     53 
---&gt; 54     assert len(patches.shape) / 2 == len(
     55         imsize
     56     ), &quot;The patches dimension is not equal to the original image size&quot;

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:

确定