gg1711
gg1711

Reputation: 35

Plotting HeatMap using Python

I am working on a localisation problem of object detection. I got the coordinates of rectangular boxes in different frames of video. So my numpy array looks like this--

[[403 172 614 326]
[345 153 652 383]
[345 172 537 326]
...
[134 115 326 307]
[153 57 403 307]
[191 19 479 230]]

Here 4 values in each column are x1, y1, x2, y2 which are basically coordinates of rectangular box defined as--

    __________________(x2,y2)
    |                |
    |                |
    |                |
    _(x1,y1)__________

(x1,y1) and (x2,y2) are coordinates of the rectangular localised box as shown.
Frame size (taken from a video) is constant. It is 480 * 850.
I need to plot heat map for these values, saying pixels which are occupied by more no. of boxes need to be more bright.

Sample Heat Map
Basically, this is not a normal heatmap (plotting of 2d array based on its value).
Can anyone suggest how to obtain heatmaps in this way?

Upvotes: 2

Views: 2456

Answers (1)

Arthur D.
Arthur D.

Reputation: 410

If I understand correctly, you have an array M of length n. Each element of M, say the first element M[0], is a four-element array [x_1, y_1, x_2, y_2] that defines a box in a larger space. Then, these boxes can be overlapping, and you want the heat map that is produced by the total combination/layering of all these boxes.

I'll start by generating some random data:

import numpy as np
M = np.random.randint(0, high=500, size=(50,4))

Then, we initialize an empty matrix (I am assuming here it the resulting heat map has dimensions 500x500 based on the sample data you provided, but you can adjust as appropriate):

R = np.zeros((500,500))

Then, for each entry in the input array of arrays, we fill-in the corresponding square by adding 1 to each "pixel" that is covered by the square's dimensions:

for row in M:
    x1, y1, x2, y2 = row
    for x in range(x1,x2+1):
        for y in range(y1,y2+1):
            R[x,y] += 1

Finally, we can plot the resulting heatmap:

import matplotlib.pyplot as plt
import seaborn as sns

sns.heatmap(R)
plt.show()

Which will give us a heat map with overlapping boxes, as desired: Resulting heatmap

Upvotes: 1

Related Questions