Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a rectangle inside a 2D numpy array

I have a 2D numpy array containing the individual data from each pixel of a sensor. The image is displayed in a GUI with a live feed from the camera. I want to be able to draw a rectangle over the image in order to distinguish an area of the screen. It seems pretty simple to draw a rectangle which is parallel to the side of the image but I eventually want to be able to rotate the rectangle. How will I know which pixels the rectangle covers when it is rotated?

like image 563
user1696811 Avatar asked Sep 28 '12 11:09

user1696811


People also ask

How do I slice a NumPy 2D array?

Slice Two-dimensional Numpy Arrays To slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index] . For example, you can use the index [1,2] to query the element at the second row, third column in precip_2002_2013 .

How do you access the elements of a 2D NumPy array?

Indexing a Two-dimensional Array To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value '10,' use the index '3' for the row and index '1' for the column.

Do NumPy arrays have to be rectangular?

@pnodbnda Numpy only supports rectangular (NxM) arrays. Yours is not rectangular as the lower-right element is missing. also, the data in an array must be homogeneous. if your element types are list and int , that's not homogeneous.


1 Answers

You can use the Python Imaging Library, if you don't mind the dependency. Given a 2D numpy array data, and an array poly of polygon coordinates (with shape (n, 2)), this will draw a polygon filled with the value 0 in the array:

img = Image.fromarray(data)
draw = ImageDraw.Draw(img)
draw.polygon([tuple(p) for p in poly], fill=0)
new_data = np.asarray(img)

Here's a self-contained demo:

import numpy as np
import matplotlib.pyplot as plt

# Python Imaging Library imports
import Image
import ImageDraw


def get_rect(x, y, width, height, angle):
    rect = np.array([(0, 0), (width, 0), (width, height), (0, height), (0, 0)])
    theta = (np.pi / 180.0) * angle
    R = np.array([[np.cos(theta), -np.sin(theta)],
                  [np.sin(theta), np.cos(theta)]])
    offset = np.array([x, y])
    transformed_rect = np.dot(rect, R) + offset
    return transformed_rect


def get_data():
    """Make an array for the demonstration."""
    X, Y = np.meshgrid(np.linspace(0, np.pi, 512), np.linspace(0, 2, 512))
    z = (np.sin(X) + np.cos(Y)) ** 2 + 0.25
    data = (255 * (z / z.max())).astype(int)
    return data


if __name__ == "__main__":
    data = get_data()

    # Convert the numpy array to an Image object.
    img = Image.fromarray(data)

    # Draw a rotated rectangle on the image.
    draw = ImageDraw.Draw(img)
    rect = get_rect(x=120, y=80, width=100, height=40, angle=30.0)
    draw.polygon([tuple(p) for p in rect], fill=0)
    # Convert the Image data to a numpy array.
    new_data = np.asarray(img)

    # Display the result using matplotlib.  (`img.show()` could also be used.)
    plt.imshow(new_data, cmap=plt.cm.gray)
    plt.show()

This script generates this plot:

enter image description here

like image 200
Warren Weckesser Avatar answered Sep 25 '22 06:09

Warren Weckesser