【问题标题】:cannot arrange numbers into shape (n,n) matrix无法将数字排列成形状 (n,n) 矩阵
【发布时间】:2020-07-05 20:38:34
【问题描述】:

我有以下代码生成分形图像,问题是如何将数字重建为矩阵。

from PIL import Image
from pylab import *
from numpy import NaN
import numpy as np
import matplotlib.pyplot as plt


def julia(C):
    X = arange(-1.5, 1.5, 0.05)
    Y = arange(-1.5, 1.5, 0.05)
    pixel = zeros((len(Y), len(X)))
    plt.axis('off')

    for x_iter, x in enumerate(X):
        for y_iter, y in enumerate(Y):
            z = x + 1j * y
            intensity = NaN
            r = np.empty((100, 100))
            for n in range(1, 1024):
                if abs(z) > 2:
                    intensity = n
                    break
                z = z**2 + C
            pixel[y_iter, x_iter] = intensity

            r.fill(intensity)
            print("intensity_matrix : ",r)

julia(-0.7 + 0.27015j)

我想打印 r,但是所有相同强度的元素都挤在一起看起来

[4. 4. 4. ... 4. 4. 4.]
 [4. 4. 4. ... 4. 4. 4.]]
intensity_matrix :  [[5. 5. 5. ... 5. 5. 5.]
 [5. 5. 5. ... 5. 5. 5.]
 [5. 5. 5. ... 5. 5. 5.]
 ...
 [5. 5. 5. ... 5. 5. 5.]
 [5. 5. 5. ... 5. 5. 5.]
 [5. 5. 5. ... 5. 5. 5.]]
intensity_matrix :  [[7. 7. 7. ... 7. 7. 7.]
 [7. 7. 7. ... 7. 7. 7.]
 [7. 7. 7. ... 7. 7. 7.]
 ...
 [7. 7. 7. ... 7. 7. 7.]
 [7. 7. 7. ... 7. 7. 7.]
 [7. 7. 7. ... 7. 7. 7.]]
intensity_matrix :  [[965. 965. 965. ... 965. 965. 965.]
 [965. 965. 965. ... 965. 965. 965.]
 [965. 965. 965. ... 965. 965. 965.]
 ...
 [965. 965. 965. ... 965. 965. 965.]
 [965. 965. 965. ... 965. 965. 965.]
 [965. 965. 965. ... 965. 965. 965.]]
intensity_matrix :  [[6. 6. 6. ... 6. 6. 6.]
 [6. 6. 6. ... 6. 6. 6.]
 [6. 6. 6. ... 6. 6. 6.]


我怎样才能更正这条线

r.fill(intensity)

获得一个正则矩阵?看起来像例子

Out[56]: 
array([0,   0,   0,   0,   0,   0,   0,   0,  24,  88,   3, 121, 121,
        4,  12,  15,   1,  19,  22,   2,   8,  31,  21,  12,  11, 110,
        40,  53,  43,  43,  81,  41, 122,  20,  32,  21, 122,   6,   8,
        18,  40,   4,   4,   2,  45,  45,   5,  46,  86,  20,  19, 119,
        10,  20,  46,  37,  11,  50,  35,   7,  21,   7,   8,   9,  11,
        46,  94,  76,  69,  31,  67,  46,  57,  43,  35,  48,  86, 116,
        32,  20,  40,  46,  14,  52,  37,  11,  11,  10,  50,  26,  83,
        25,   7,   6,   5,   5,  12,  12,  10,  31,  12, 113,   7,   4,
        14, 104,  48,  89,   1,   1,   1,   1,   1,   1,   1,  95,  31,
        18,  46,   4,   1,   0,   0,   0,   0,   0,   0,  34,   1,   2,
         0,   0,   0,   0,   0,   0,   1,  48,  13,  19, 103,   4,  71,
         1,   1,   0,   0,   1,   2,  10,  11,  66,  11,  24,  10,  62,
         1,   1,   0,   0,   0,   0,   1,  11,  10,   6,  55,  19,  34,
        74, 122,  74,  32,   7,  25,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        ], dtype=uint16)

请告诉我如何解决这个问题?完成。

# another question 

这是像素数据

import numpy as np
import matplotlib.pyplot as plt

def julia(C):
    X = np.arange(-1.5, 1.5, 0.2)
    Y = np.arange(-1.5, 1.5, 0.2)
    pixel = np.zeros((len(Y), len(X)))
    
    for x_iter, x in enumerate(X):
        for y_iter, y in enumerate(Y):
            z = x + 1j * y
            intensity = np.nan
            r = np.empty((100, 100)) # Unused at the moment
            for n in range(1, 1024):
                if abs(z) > 2:
                    intensity = n
                    break
                z = z**2 + C
            pixel[y_iter, x_iter] = intensity
            r.fill(intensity) # Unused at the moment
    
    # We return pixel matrix
    return pixel


# Compute Julia set image
pixel = julia(-0.7 + 0.27015j)

# Plotting
print(pixel[:,:])
print(pixel[:,:].shape)

[[  1.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   3.   3.   3.   3.   3.   2.   2.
    2.]
 [  2.   2.   2.   2.   3.   3.   3.   4.   5.   4.   4.   3.   3.   3.
    2.]
 [  2.   2.   3.   3.   3.   4.   4.   7. 209.   6.   5.   4.   4.   3.
    3.]
 [  2.   3.   3.   3.   4.   5.   6.  37.  59. 220.  13.   7.  10.   6.
    4.]
 [  3.   3.   4.  10.   7.   8.   9.  13. 408.  99. 126. 401. 537. 437.
   10.]
 [  3.   4.   6.  23.  40. 112.  68. 685.  48. 591. 567. 290. 117. 353.
   11.]
 [  4.  11. 353. 117. 290. 567. 591.  48. 685.  68. 112.  40.  23.   6.
    4.]
 [  4.  10. 437. 537. 401. 126.  99. 408.  13.   9.   8.   7.  10.   4.
    3.]
 [  3.   4.   6.  10.   7.  13. 220.  59.  37.   6.   5.   4.   3.   3.
    3.]
 [  2.   3.   3.   4.   4.   5.   6. 209.   7.   4.   4.   3.   3.   3.
    2.]
 [  2.   2.   3.   3.   3.   4.   4.   5.   4.   3.   3.   3.   2.   2.
    2.]
 [  2.   2.   2.   2.   3.   3.   3.   3.   3.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]]
(15, 15)

得到图像后我做了plt.savefig(),当我做了image.open()时,数据变成了如下!

array([[[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       ...,

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 255],
        [255, 255, 255, 255]]], dtype=uint16)

形状现在变成(大约 240,大约 280)类似的东西。原始数据维度仅为 (15, 15)。额外的形状我相信它们是对应于所有 255 强度的不需要的空白边界。我什至无法检查像素矩阵的值是否在 1 到 1022 之间且具有(200 多个,200 多个)形状。我需要在图像周围去除空白区域的图像。为了做进一步的图像处理分析,我必须去掉图像周围的空白,你知道如何重写代码吗?博索!

bousof!我检查了

min_value = np.nanmin(pixel)

min_value
Out[4]: 1.0

max_value = np.nanmax(pixel)

max_value
Out[6]: 685.0

他们还好

但是当我检查时

pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)

pixel_int
Out[9]: 
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   1,   1,   2,  77,   1,   1,   1,   1,
          0,   0],
       [  0,   0,   0,   0,   1,   1,   1,  13,  21,  81,   4,   2,   3,
          1,   1],
       [  0,   0,   1,   3,   2,   2,   2,   4, 151,  36,  46, 149, 199,
        162,   3],
       [  0,   1,   1,   8,  14,  41,  24, 255,  17, 219, 211, 107,  43,
        131,   3],
       [  1,   3, 131,  43, 107, 211, 219,  17, 255,  24,  41,  14,   8,
          1,   1],
       [  1,   3, 162, 199, 149,  46,  36, 151,   4,   2,   2,   2,   3,
          1,   0],
       [  0,   1,   1,   3,   2,   4,  81,  21,  13,   1,   1,   1,   0,
          0,   0],
       [  0,   0,   0,   1,   1,   1,   1,  77,   2,   1,   1,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   1,   1,   1,   1,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0]], dtype=uint8)

有问题。对于大于 255 的所有元素将被压缩为 255/(这意味着丢失一些信息)。问题应该是因为使用 astype(np.uint8) 而不是 astype(np.uint16)。因此,我将这一行修改为

pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint16)

pixel_int
Out[11]: 
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   1,   1,   1,   1,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   1,   1,   2,  77,   1,   1,   1,   1,
          0,   0],
       [  0,   0,   0,   0,   1,   1,   1,  13,  21,  81,   4,   2,   3,
          1,   1],
       [  0,   0,   1,   3,   2,   2,   2,   4, 151,  36,  46, 149, 199,
        162,   3],
       [  0,   1,   1,   8,  14,  41,  24, 255,  17, 219, 211, 107,  43,
        131,   3],
       [  1,   3, 131,  43, 107, 211, 219,  17, 255,  24,  41,  14,   8,
          1,   1],
       [  1,   3, 162, 199, 149,  46,  36, 151,   4,   2,   2,   2,   3,
          1,   0],
       [  0,   1,   1,   3,   2,   4,  81,  21,  13,   1,   1,   1,   0,
          0,   0],
       [  0,   0,   0,   1,   1,   1,   1,  77,   2,   1,   1,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   1,   1,   1,   1,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0]], dtype=uint16)

问题依旧!

没有丢失任何信息/或没有任何压缩的像素矩阵(原始数据)如下:

[[  1.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   3.   3.   3.   3.   3.   2.   2.
    2.]
 [  2.   2.   2.   2.   3.   3.   3.   4.   5.   4.   4.   3.   3.   3.
    2.]
 [  2.   2.   3.   3.   3.   4.   4.   7. 209.   6.   5.   4.   4.   3.
    3.]
 [  2.   3.   3.   3.   4.   5.   6.  37.  59. 220.  13.   7.  10.   6.
    4.]
 [  3.   3.   4.  10.   7.   8.   9.  13. 408.  99. 126. 401. 537. 437.
   10.]
 [  3.   4.   6.  23.  40. 112.  68. 685.  48. 591. 567. 290. 117. 353.
   11.]
 [  4.  11. 353. 117. 290. 567. 591.  48. 685.  68. 112.  40.  23.   6.
    4.]
 [  4.  10. 437. 537. 401. 126.  99. 408.  13.   9.   8.   7.  10.   4.
    3.]
 [  3.   4.   6.  10.   7.  13. 220.  59.  37.   6.   5.   4.   3.   3.
    3.]
 [  2.   3.   3.   4.   4.   5.   6. 209.   7.   4.   4.   3.   3.   3.
    2.]
 [  2.   2.   3.   3.   3.   4.   4.   5.   4.   3.   3.   3.   2.   2.
    2.]
 [  2.   2.   2.   2.   3.   3.   3.   3.   3.   2.   2.   2.   2.   2.
    2.]
 [  2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.   2.
    2.]]

bousof!你觉得这条线怎么样?

pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint16)

我们应该如何重写它?

【问题讨论】:

  • 你好丹尼尔。您能解释一下代码中的 r 是什么吗?为什么要创建一个 100x100 的数组并填充相同的值?
  • 在您的示例中,您已经构建了一个数组pixel,其中包含看起来像您正在寻找的输出的强度。
  • 感谢 bousof ! r 只是一个空矩阵,它是为以后打算包含所有强度值而创建的。如果我不必分配这样一个空矩阵 r 并且能够制作强度矩阵,我就不会设置它。例如,100x100 可以是 (10,10) 或任何格式的 (n, n) 矩阵。不管怎样,我只想把强度写成一个矩阵,这就是我想要的。你知道怎么写那个代码矩阵[intensity], bousof?
  • 可能是我的代码打印(像素)不正确! bousof,你知道如何正确打印像素吗?
  • print (pixel) 是我正在寻找的,因为我尝试再次打印像素,这一次它可以工作。再次感谢 bousof!

标签: python numpy


【解决方案1】:

您的代码实际上有效,您只是关注错误的数组r 而不是pixel。这是代码,我修改了函数julia 以返回pixel 数组。然后绘制这个数组:

import numpy as np
import matplotlib.pyplot as plt

def julia(C):
    X = np.arange(-1.5, 1.5, 0.05)
    Y = np.arange(-1.5, 1.5, 0.05)
    pixel = np.zeros((len(Y), len(X)))

    for x_iter, x in enumerate(X):
        for y_iter, y in enumerate(Y):
            z = x + 1j * y
            intensity = np.nan
            r = np.empty((100, 100)) # Unused at the moment
            for n in range(1, 1024):
                if abs(z) > 2:
                    intensity = n
                    break
                z = z**2 + C
            pixel[y_iter, x_iter] = intensity
            r.fill(intensity) # Unused at the moment

    # We return pixel matrix
    return pixel

# Compute Julia set image
pixel = julia(-0.7 + 0.27015j)

# Plotting
plt.imshow(pixel)
plt.colorbar()
plt.show()

输出:


编辑


您可以使用此脚本将像素保存和加载为 png(有关更多详细信息,它是 thisthis 之间的混合):

# Small script saving the image as a png
from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
  pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')
img.save('julia.png')
Image.open('julia.png').show()

以下是 viridisjet 颜色图:

【讨论】:

  • 另一个问题要问你!你能告诉我如何将图像保存在 unit16 中吗?这样我以后就可以做 data = np.arrarry(image) ,这将给我返回像素(原始数据)而不会丢失任何信息/或没有任何压缩。
  • bousof!在 uint16 中可以看到矩阵元素,但我必须摆脱图像周围的空白!与 255 对应的空白空间严重影响了我对图像的分析,因为它们不是所需的数据!那么,你知道在编码图像时如何去除这些空白吗?
  • 你好丹尼尔。对不起,我不明白你指的是什么空白? pixel 矩阵的值介于 11022(不包括 NaN)之间。
  • bousof,让我告诉你我在问题区域中指的是哪些空白
  • @DanielL 我不知道 Pillow 包。我编辑了解决方案并添加了一个小脚本,仅将 pixel 数组的内容保存为 png 图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多