moliu 2024-06-26 09:30 采纳率: 57.1%
浏览 0
已结题

切片是视图不是能影响原数据吗

切片,是 view.可是下面的代码,赋值前后,均为空。不知何故。
以下为正常。

img = np.zeros((5, 5, 3), dtype=np.uint8)
img[0:3, 1:4] = 55
img
array([[[ 0,  0,  0],
        [55, 55, 55],
        [55, 55, 55],
        [55, 55, 55],
        [ 0,  0,  0]],

       [[ 0,  0,  0],
        [55, 55, 55],
        [55, 55, 55],
        [55, 55, 55],
        [ 0,  0,  0]],

       [[ 0,  0,  0],
        [55, 55, 55],
        [55, 55, 55],
        [55, 55, 55],
        [ 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)

可下面,不正常了。请教这是为什么

import numpy as np

# Example parameters (replace with your actual values)
NSQUARES = 5
N = 100
centers = np.array([[20, 20], [40, 50], [70, 30], [80, 70], [10, 80]])
radii = np.array([10, 15, 8, 12, 7])
colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [0, 255, 255]])

# Create a blank image
img = np.zeros((N, N, 3), dtype=np.uint8)
**print("no modify:",img)**
# Iterate over each square
num = 0
for i in range(NSQUARES):
    num = num + 1
    x = centers[i][0]
    y = centers[i][1]
    r = radii[i]
    color = colors[i]

    # Calculate the bounding box of the square
    x_min = max(x - r, 0)
    x_max = min(x + r + 1, N)
    y_min = max(y - r, 0)
    y_max = min(y + r + 1, N)

    # Assign color to the square region in the image
    img[x_min:x_max, y_min:y_max] = color
    
    **print(num,img)**

显示结果:

no modify: [[[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 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 0]
  [0 0 0]]]
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]]

 [[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 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  ...
  [0 0 0]
  [0 0 0]
  [0 0 0]]]
2 [[[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 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 0]
  [0 0 0]]]
3 [[[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 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 0]
  [0 0 0]]]
4 [[[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 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 0]
  [0 0 0]]]
5 [[[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 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 0]
  [0 0 0]]]

  • 写回答

4条回答 默认 最新

  • moliu 2024-06-26 10:04
    关注

    你想看的,它给省略了。哈哈
    在使用 NumPy 中的 ndarray 时,有时候数组内容太长会被省略显示。你可以通过设置 np.set_printoptions 函数来控制输出的格式,以便完整显示数组内容。在你的代码中,你已经使用了 threshold=np.inf 来设置阈值为无穷大,这样就能够确保完整显示数组的所有元素。这里是你可以使用的代码示例:

    import numpy as np
    
    # 设置打印选项,显示所有元素
    np.set_printoptions(threshold=np.inf)
    
    # 在这之后的所有 ndarray 输出将会完整显示
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月4日
  • 已采纳回答 6月26日
  • 创建了问题 6月26日

悬赏问题

  • ¥20 ic卡dump文件校检码解密
  • ¥15 关于:接收到的数据不是有效的JSON格式
  • ¥15 apdl语言如何增加受力分析
  • ¥15 算法对比:学校优化算法与蚁群算法对比
  • ¥15 机电一体化系统设计说明书
  • ¥20 sgy数据提取地震波速,有人能回答吗小馋
  • ¥20 c#实现打开word的功能,并且需要安装成windows服务,word打不开怎么办
  • ¥15 python用ARIMA时间预测模型预测数据出错,急!
  • ¥30 为什么后端传给前端vue的河流json数据不在地图中显示出来
  • ¥50 关于弹性波动方程求解的问题: