Skip to content

numpy array dimensions for imshow in matplotlib #3

@mohammad-albarham

Description

@mohammad-albarham

As shown in this line, it says the following:

The imshow commands expects a `numpy array` with shape (3, width, height)

Reference:
https://github.com/dml-cth/deep-machine-learning/blame/0f8a0f70fbbfe06d83698f696388b19833ecadc3/home-assignments/HA1/HA1.ipynb#L913

This description seems uncorrect, since imshow expects (height, width, 3) and not (3, width, height), Reference for imshow, which has been written correctly in the code.

Also,
Reference for read_image from torchvision that is in the supported code.

Supported code for reference:

import torch
import torchvision
from torchvision.io import read_image
import matplotlib.pyplot as plt

# Read the image as a tensor (C, H, W)
img_tensor = read_image('cat.5077.jpg') # Tensor[3, image_height, image_width]

# Convert to (H, W, C) for matplotlib imshow function
img_np = img_tensor.permute(1, 2, 0).numpy() # np.array > [image_height, image_width, 3]


plt.imshow(img_np)
plt.axis('off')
plt.show()
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions